Skip to content

Instantly share code, notes, and snippets.

View r4hulp's full-sized avatar
🎯
Focusing

Rahul P r4hulp

🎯
Focusing
View GitHub Profile
@r4hulp
r4hulp / DialogFactory.cs
Last active December 29, 2017 12:19
Dialog Factory
public interface IDialogFactory
{
//constructor without dynamic params
T Create<T>();
//Constructor requiring multiple dynamic params
T Create<T>(IDictionary<string, object> parameters);
}
public class DialogFactory : IDialogFactory
{
@r4hulp
r4hulp / MessagesController.cs
Last active December 29, 2017 11:17
Bot Framework Messages Controller with DI
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
{
@r4hulp
r4hulp / MessagesController.cs
Created December 29, 2017 11:13
Bot Framework Messages Controller without DI
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
}
@r4hulp
r4hulp / RootDialog.cs
Last active December 29, 2017 11:10
Root Dialog having constructor with parameters
[Serializable]
public class RootDialog : IDialog<object>
{
private IProfileService profileService;
public RootDialog(IProfileService profileService)
{
this.profileService = profileService;
}
//code removed for sanity.
@r4hulp
r4hulp / MyBotModules.cs
Last active December 29, 2017 10:49
Bot Registrations
public class MyBotModules : Module
{
protected override void Load(ContainerBuilder builder)
{
base.Load(builder);
//Register RootDialog as IDialog<object>
builder.RegisterType<RootDialog>()
.As<IDialog<object>>()
.InstancePerDependency();
@r4hulp
r4hulp / AnyDialog.cs
Created December 29, 2017 08:03
CRD BotData
//In your dialog
Activity message = result as Activity;
//Create scope with respect to activity
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
{
//Resolve scope for IBotDataStore<BotData>
IBotDataStore<BotData> stateStore = scope.Resolve<IBotDataStore<BotData>>();
/* Retrieve user address. Address key holds information about Bot, Channel, User and conversation */
@r4hulp
r4hulp / AnyDialog.cs
Created December 29, 2017 07:53
Get Address key from activity
Address key = Address.FromActivity(message);
@r4hulp
r4hulp / AnyDialog.cs
Created December 29, 2017 07:47
Retrieve user data
//In your dialog
Activity message = result as Activity;
//Create scope with respect to activity
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
{
//Resolve scope for IBotDataStore<BotData>
IBotDataStore<BotData> stateStore = scope.Resolve<IBotDataStore<BotData>>();
/* Retrieve user address. Address key holds information about Bot, Channel, User and conversation */
@r4hulp
r4hulp / AnyDialog.cs
Last active December 29, 2017 07:36
Resolve DataStore scope
//In your dialog
Activity message = result as Activity;
//Create scope with respect to activity
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
{
//Resolve scope for IBotDataStore<BotData>
IBotDataStore<BotData> stateStore = scope.Resolve<IBotDataStore<BotData>>();
}
@r4hulp
r4hulp / Global.asax.cs
Created December 29, 2017 07:33
Registering module
//In Global.asax.cs
builder.RegisterModule(new AzureModule(Assembly.GetExecutingAssembly()));
var store = new TableBotDataStore(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
builder.Register(c => store)
.Keyed<IBotDataStore<BotData>>(AzureModule.Key_DataStore)
.AsSelf()
.SingleInstance();