修改响应的目的地

时间:2018-08-27 18:13:28

标签: botframework

我使用Microsoft Bot Framework。发件人:https://dev.botframework.com/
AND
Microsoft Bot Emulator(V4 Preview)版本4.0.15-alpha。发件人:https://github.com/microsoft/botframework-emulator

我使用“启动应用程序”模板创建了一个新的C#项目。我运行这个项目。我启动了模拟器的两个实体。 现在,我从第一个仿真器实体收到消息,但是我想将响应发送到第二个仿真器实体。我该怎么办?

这是我尝试修改目标位置(注释的代码)但不起作用的功能。

/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
    //activity.Recipient.Id = "default-user";
    //activity.ServiceUrl = "http://localhost:52234";
    //activity.Conversation.Id = "e7bbb310-a93c-11e8-8dcc-7d6fd69e3901|livechat";
    //activity.ReplyToId = "6cc291f0-a93d-11e8-9634-9f01a6c082d4";

    if (activity.Type == ActivityTypes.Message)
    {
        await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
    }
    else
    {
        HandleSystemMessage(activity);
    }
    var response = Request.CreateResponse(HttpStatusCode.OK);
    return response;
}

1 个答案:

答案 0 :(得分:0)

  

我启动了模拟器的两个实体。现在,我从第一个仿真器实体收到消息,但是我想将响应发送到第二个仿真器实体。我该怎么办?

在编写代码将消息发送到另一个仿真器之前,需要获取ServiceUrlconversationId等的值。然后,您可以参考以下代码将消息发送到指定的对话

在对话框中:

await context.PostAsync($"{this.count++}: You said {activity.Text}");

var userAccount = new ChannelAccount(name: "User", id: "default-user");
var botAccount = new ChannelAccount(name: "Bot", id: "default-bot");

var connector = new ConnectorClient(new Uri("{your_ServiceUrl_here}"));

IMessageActivity message = Activity.CreateMessageActivity();
message.From = botAccount;
message.Recipient = userAccount;

//specify conversationId
message.Conversation = new ConversationAccount(id: "{your_conversationId_here}");
message.Text = $"You said {activity.Text} from emulator1";
message.Locale = "en-Us";
await connector.Conversations.SendToConversationAsync((Activity)message);
context.Wait(MessageReceivedAsync); 

测试结果:

enter image description here