如何在Bot框架中从Bot向多客户端发送消息

时间:2017-02-22 05:31:32

标签: c# .net botframework

问候每一个人!我是Microsoft Bot Framework的新成员,我想知道我们如何从Bot向多个客户端发送消息(不使用聊天组)?请给我一些建议。谢谢 还有一个问题是,当客户端关闭浏览器以便在客户端再次返回时检索对话时,我们如何保存Bot和客户端之间的对话?

2 个答案:

答案 0 :(得分:0)

阅读文档后,我认为有一种方法CreateConversation()方法可以向多个客户发送电子邮件。

但我认为你可以在for循环中使用CreateDirectConversation()方法,迭代你的用户帐户。这将启动与所有用户帐户的1:1对话。

答案 1 :(得分:0)

取自Microsoft Bot Framwork SDK Reference: Starting Conversations

开始对话

要发起对话,您需要调用CreateConversation()CreateDirectConversation()方法从频道获取ConversationAccount条记录。获得ConversationAccount之后,您可以在对SendToConversation()的消息调用中使用它。

创建1:1对话

CreateDirectConversation()方法用于在机器人和用户之间创建私人1:1对话。

要初始化ConnectorClient,请使用先前消息中保留的ServiceUrl

示例:

var userAccount = new ChannelAccount(name: "Larry", id: "@UV357341");
var connector = new ConnectorClient(new Uri(incomingMessage.ServiceUrl));
var conversationId = await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount);
IMessageActivity message =  Activity.CreateMessageActivity();
message.From = botAccount;
message.Recipient = userAccount;
message.Conversation = new ConversationAccount(id: conversationId.Id);
message.Text = "Hello";
message.Locale = "en-Us";
await connector.Conversations.SendToConversationAsync((Activity)message); 

创建群组对话

CreateConversation()方法用于创建新的群组对话。

  

注意:目前,电子邮件是支持机器人发起的群组对话的唯一渠道

示例:

var connector = new ConnectorClient(new Uri(incomingMessage.ServiceUrl));
List<ChannelAccount> participants = new List<ChannelAccount>();
participants.Add(new ChannelAccount("joe@contoso.com", "Joe the Engineer"));
participants.Add(new ChannelAccount("sara@contso.com", "Sara in Finance"));
ConversationParameters cpMessage = new ConversationParameters(message.Recipient, true, participants, "Quarter End Discussion");
var conversationId = await connector.Conversations.CreateConversationAsync(cpMessage);
IMessageActivity message = Activity.CreateMessageActivity();
message.From = botAccount;
message.Recipient = new ChannelAccount("lydia@contoso.com", "Lydia the CFO"));
message.Conversation = new ConversationAccount(id: conversationId.Id);
message.ChannelId = incomingMessage.ChannelId;
message.Text = "Hey, what's up everyone?";
message.Locale = "en-Us";
await connector.Conversations.SendToConversationAsync((Activity)message);