简体   繁体   中英

How to use Global Commands in Microsoft Bot Framework?

I have read somewhere that we can use IScorable.cs to implement globally available commands eg type "settings" anywhere in chat flow to open settings dialog. But there seems to be no documentation on how to use it.

Please help.

You are right, implementing IScorable.cs is the way to go if you want to have global commands in BotFramework.

Having an IScorable it's basically a two steps procedure:

  1. Write your IScorable
  2. Register your IScorable in the BotFramework's Autofac container

BotFramework will go through all the IScorables first to see if any of them "wins" (aka if they will be doing something or not) and if no one handle the message; then it will continue dispatching the message to the dialogs.

Regarding how to write your IScorable , the best thing is to look into some examples:

  • Settings scorable (from ContosoFlowers sample ). It launches a new dialog when the a "settings" message is sent.
  • DeleteProfile scorable (coming in the BotFramework). It deletes the PrivateConversationData and UserData bags and also reset the stack when a "/deleteprofile" message is sent.
  • Alarm scorable (from the AlarmsBot sample). It calls a service to perform an operation if the message matches to any of the predefined verbs.

Regarding how to register the IScorable , I would recommend you doing something like the following in the Global.asax.cs , Application_Start method:

var builder = new ContainerBuilder();

builder.RegisterType<SettingsScorable>()
    .As<IScorable<IActivity, double>>()
    .InstancePerLifetimeScope();

builder.Update(Conversation.Container);

That shows how the SettingsScorable of the ContosoFlowers is being registered in the Autofac container.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM