我正在尝试使用我的机器人在 while循环的中间发送消息。在破坏之前这可能吗?从下面的代码中,我循环并自动将消息发布到bot,具体取决于我从服务获得的响应状态....即值的变化。直到我达到5级,我才打破。有没有办法在循环实际完成之前继续对话?
int prev = 2;
do
{
StatusOption(reply);
context.Wait(StatusSelected);
{
int ride_status = prev;
context.UserData.TryGetValue<int>("ride_status", out ride_status);
string trip_status = CheckTripStatus(res[3].ToString());
prev = ride_status;
int current = Convert.ToInt32(trip_status);
if (prev != current)
{
if (current == 2)
{
reply.Text = "Your driver is coming to pick you.";
await context.PostAsync(reply);
context.UserData.SetValue<int>("ride_status", current);
}
else if (current == 3)
{
reply.Text = "Your driver has arrived.";
await context.PostAsync(reply);
context.UserData.SetValue<int>("ride_status", current);
}
else if (current == 4)
{
reply.Text = "Your trip has started.";
await context.PostAsync(reply);
context.UserData.SetValue<int>("ride_status", current);
}
else if (current == 5)
{
reply.Text = "Your trip has ended.";
await context.PostAsync(reply);
context.UserData.SetValue<int>("ride_status", current);
break;
}
else
{
StatusOption(reply);
context.Wait(StatusSelected);
}
}
Thread.Sleep(5000);
}
}
while (prev <= 5) ;
答案 0 :(得分:1)
从概念上讲,在长时间运行的循环中使用上下文线程并不是一个好的设计实践。查看 Azure功能Bot主动模板。它向Microsoft.WindowsAzure.Storage.Queue添加包含 ConversationReference 的消息。当任何内容添加到队列时,会触发单独的函数,并调用bot发送消息。这是一种更具可扩展性和可管理性的设计。
当有人请求乘车时,您可以将 ConversationReference 添加到队列中。不是自动触发响应,而是通过乘坐状态的变化触发功能。
另一个不使用函数的选项是在某处存储 ConversationReference ,并在机器人的项目中公开另一个WebApi端点。当状态发生变化时,请调用此端点并使用 ConversationReference 主动向骑车者发送状态消息。可以在此处找到一些主动消息传递文档:https://docs.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-proactive-messages?view=azure-bot-service-3.0(代码示例:https://github.com/Microsoft/BotBuilder-Samples/tree/master/Node/core-proactiveMessages)注意:这些只是示例,并且使用静态变量。尽管如此,它们是一个很好的起点,概述了如何进行主动消息传递。