LoginSignup
1
3

More than 5 years have passed since last update.

Bot FrameworkでBotの方から話しかける(C#)

Last updated at Posted at 2017-01-06

やりたいこと

BotFrameworkでBot開発をするときチャットを開始したらBot側から話しかけれくれるようにしたい。

結果

本当はNode.jsで開発したかったが実装方法はわからずじまい・・・。
C#でBotFrameworkテンプレートでソリューションを作りチャットを開始すると、Activity型の引数のTypeがConversationUpdateとなってHandleSystemMessageに来るのでそこでメッセージを生成して送信する。

コード


private async Task<Activity> HandleSystemMessage(Activity message)
{
    if (message.Type == ActivityTypes.ConversationUpdate)
    {
      using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
      {
        var client = scope.Resolve<IConnectorClient>();
        var reply = message.CreateReply();
        reply.Text = $"Welcome!";
        await client.Conversations.ReplyToActivityAsync(reply);                 
      }
    }

    return null;
}

 最後に

例によって試行錯誤したらこのような結果になったという備忘録なので、この方法が正攻法かはわかりません。

2017/08/08追記
Microsoftから正式なやり方がリリースされました!
https://docs.microsoft.com/en-us/bot-framework/dotnet/bot-builder-dotnet-proactive-messages

1
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3