用Bot框架在模拟器上发送消息到相同的会话

本文关键字:消息 会话 框架 Bot 模拟器 | 更新日期: 2023-09-27 18:08:49

我试图在同一对话中向模拟器上的用户发送从bot的消息,但使用文档中的代码,它只创建一个新的对话,当我使用相同的会话id时,它也创建了一个新的会话,并且没有回复相同的会话。下面的代码创建了一个新的会话

             var userAccount = new ChannelAccount(name: "User1", id: "@2c1c7fa3");
            var botAccount = new ChannelAccount(name: "Conv1", id: "8a684db8");
            var connector = new ConnectorClient(new Uri("http://localhost:9000/"));
            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);

和如果我从用户发送的消息中使用相同的会话id,它也创建了一个新的会话。这样的

  IMessageActivity message = Activity.CreateMessageActivity();
            message.From = botAccount;
            message.Recipient = userAccount;
            message.Conversation = new ConversationAccount(id: "Conv1");
            message.Text = "Hello";
            message.Locale = "en-Us";
            await connector.Conversations.SendToConversationAsync((Activity)message);

用Bot框架在模拟器上发送消息到相同的会话

如果您在控制器中并且想要回复用户,只需使用:

var reply = activity.CreateReply();
reply.Text = "Hello";
await connector.Conversations.SendToConversationAsync(reply);

如果你在一个对话框中,你仍然可以使用activity.CreateReply(),但我会使用context.PostAsync(reply)而不是连接器。

您不需要创建会话来回复用户发送的消息。

例如,您可以在模拟器

中看到这个json数据
{
  "type": "message",
  "timestamp": "2016-12-02T07:39:33.8503472Z",
  "from": {
    "id": "56800324",
    "name": "Bot1"
  },
  "conversation": {
    "id": "8a684db8",
    "name": "Conv1"
  },
  "recipient": {
    "id": "2c1c7fa3",
    "name": "User1"
  },
  "text": "Sorry. I don't know what you are talking about...",
  "replyToId": "7cc6478627584aa497a209b4f284937e"
}

会话id应该是"8a684db8",而不是"Conv1"。您可以在POST方法之外创建SendMessage()方法来发送消息(广告,事件,通知等),如下所示。

private void SendMeesge(string msg)
{
    var userAccount = new ChannelAccount(name: "User1", id: "2c1c7fa3");
    var botAccount = new ChannelAccount(name: "Bot1", id: 56800324);
    var connector = new ConnectorClient(new Uri("http://localhost:9000/"));
    // conversationId for Real Bot
    //var conversationId = connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount).Result;
    // conversationId for Bot Emulator
    var conversationId = _activityHistory.Conversation;
    IMessageActivity message = Activity.CreateMessageActivity();
    message.From = botAccount;
    message.Recipient = userAccount;
    message.Conversation = new ConversationAccount(id: "8a684db8");
    message.Text = msg;
    message.Locale = "en-Us";
    connector.Conversations.SendToConversationAsync((Activity)message);
}

您必须使用"8a684db8"为您的模拟器,但使用"CreateDirectConversationAsync"到REAL BOT发布在azure上。

正如Ezequiel所说,如果您在POST方法中并回复用户(在您的bot收到消息后),您不需要走这种复杂的方式~