2016-09-24 159 views
3

同一個對話我試圖發送一條消息從機器人到模擬器上的用戶在同一個對話,但使用文檔中的代碼,它只創建一個新的對話,當我使用相同的對話ID時,它也創建了一個新對話,並且沒有回覆相同的對話。這段代碼創建一個新的對話發送消息到模擬器Bot Bot

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

回答

1

如果你是在控制器和要回復給用戶,只需使用:

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

如果你是一個對話框內,你仍然可以使用activity.CreateReply()但後來我會使用context.PostAsync(回覆)而不是連接器。

您不需要創建會話來回複用戶發送的消息。

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方法和回覆用戶(在你的機器人收到信息後),你不需要去這個複雜的方式〜