使用Bot Framework在模拟器上向同一对话发送消息

时间:2016-09-24 13:47:10

标签: c# botframework

我正在尝试在同一个对话的模拟器上向机器人发送消息,但是使用文档中的代码,它只会创建一个新的对话,当我使用相同的对话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);

2 个答案:

答案 0 :(得分:1)

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

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

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

您无需创建对话即可回复用户发送的消息。

答案 1 :(得分:0)

例如,您可以在模拟器中看到此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 ”到azure上发布的 REAL BOT

正如Ezequiel所说,如果您使用POST方法并回复用户(在您的机器人收到消息后),您不需要采用这种复杂的方式〜