简体   繁体   中英

Send a message to the same conversation on emulator with Bot Framework

I am trying to send a message from the bot to the user on the emulator on the same conversation , but using the code in the documentation , It only creates a new conversation , when I used the same conversation id also it created a new one and didnt reply to the same conversation. This code creates a new conversation

             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);

and If i use the same conversation id from the message sent from the user it also created a new conversation . like this

  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);

If you are in the controller and you want to reply to the user, just use:

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

If you are within a Dialog, you can still use activity.CreateReply() but then I would use context.PostAsync(reply) instead of the connector.

You don't need to create a conversation to reply to a message sent by the user.

For example, you can see this json data in your emulator

{
  "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"
}

Conversation id should be " 8a684db8 ", not "Conv1". Your can create a SendMessage() method outside the POST method to send message (Ad, event, nitification, etc.) as below.

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);
}

You have to use "8a684db8" for your Emulator but use " CreateDirectConversationAsync " to a REAL BOT published on azure.

And as Ezequiel said, if you are in the POST method and reply to user(after your bot receives a message), you don't need to go this complicated way~

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM