通过对话广播消息

时间:2018-11-10 17:43:35

标签: c# botframework

我想在机器人所在的对话中广播消息,发出这样的命令:

/broadcast gameid=15 message

对于机器人在哪里的每个对话,我都在对话状态下保存activity.Conversation.Id,并且当机器人收到命令/broadcast时,它将提取对话的所有状态,并根据所需条件进行过滤和sendActivityAsync来使用在状态中检索到的所有使用Conversation.Id的聊天。

 var activity = new Activity()
 {
     Conversation = new ConversationAccount(id: state.ConversationId),
     Text = "toto",
     Type = ActivityTypes.Message,
     ServiceUrl = turnContext.Activity.ServiceUrl,
     ChannelId = turnContext.Activity.ChannelId
 };
 await turnContext.SendActivityAsync(activity);

但是消息仅在发出命令的聊天室中发出,而忽略了我在活动中设置的Conversation.Id

我使用机器人模拟器进行测试。

那么,是否可以将聊天活动发送给其他人,如果可以,我的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

It sounds like you are trying to send proactive messages to all of your users. Proactive messages in the BotFramework v4 SDK enable you to continue conversations with individual users or send them notifications.

To send proactive messages outside the scope of a conversation, you first need to get the conversation reference and save the resulting value in some persistent storage such as a database. You can get the reference from the turnContext in on onTurnAsync by calling:

var conversation = turnContext.Activity.GetConversationReference();

Then in your script to broadcast messages you can retrieve all the conversation references from storage, configure a new adapter, and call the ContinueConversationAsync method from the adapter and send an activity in the callback for each reference to message users.

await adapter.ContinueConversationAsync(botId, reference, async (turnContext) => {
    await turnContext.SendActivityAsync("Broadcast Message");
} , cancellationToken);

For more information about proactive messages, take a look at the documentation and this proactive messages example in Microsoft's BotBuilder-Samples repository on GitHub. Hope this is helpful.