微软机器人框架网络聊天C#
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微软机器人框架网络聊天C#相关的知识,希望对你有一定的参考价值。
我正在使用C#中的Microsoft bot框架开发一个聊天机器人。我们有一个功能,它查询数据库并返回结果,但返回结果可能需要25-30秒。
到那时机器人说“不能发送,请重试”。有没有办法增加这个超时?或者我们可以为用户提供类似“请稍候”的消息,以便用户知道请求正在处理中吗?
答案
它在SDK中硬编码,我们无法覆盖“无法发送,重试”等消息。正如Nicolas所说,解决方法是向用户发送proactive message。
例如,您可以首先创建一个ConversationStarter.cs
类,如下所示:
public class ConversationStarter
{
//Note: Of course you don't want these here. Eventually you will need to save these in some table
//Having them here as static variables means we can only remember one user :)
public static string fromId;
public static string fromName;
public static string toId;
public static string toName;
public static string serviceUrl;
public static string channelId;
public static string conversationId;
//This will send an adhoc message to the user
public static async Task Resume(string conversationId, string channelId)
{
var userAccount = new ChannelAccount(toId, toName);
var botAccount = new ChannelAccount(fromId, fromName);
var connector = new ConnectorClient(new Uri(serviceUrl));
IMessageActivity message = Activity.CreateMessageActivity();
if (!string.IsNullOrEmpty(conversationId) && !string.IsNullOrEmpty(channelId))
{
message.ChannelId = channelId;
}
else
{
conversationId = (await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount)).Id;
}
message.From = botAccount;
message.Recipient = userAccount;
message.Conversation = new ConversationAccount(id: conversationId);
message.Text = "Hello, work is done!";
message.Locale = "en-Us";
await connector.Conversations.SendToConversationAsync((Activity)message);
}
}
然后在对话框中,您可以这样编码:
public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
//We need to keep this data so we know who to send the message to. Assume this would be stored somewhere, e.g. an Azure Table
ConversationStarter.toId = message.From.Id;
ConversationStarter.toName = message.From.Name;
ConversationStarter.fromId = message.Recipient.Id;
ConversationStarter.fromName = message.Recipient.Name;
ConversationStarter.serviceUrl = message.ServiceUrl;
ConversationStarter.channelId = message.ChannelId;
ConversationStarter.conversationId = message.Conversation.Id;
await context.PostAsync("Please wait, we're processing...");
Processing();
}
public async Task Processing()
{
//replace the task.delay() method with your task.
await Task.Delay(30000).ContinueWith((t) =>
{
ConversationStarter.Resume(ConversationStarter.conversationId, ConversationStarter.channelId);
});
}
然后Task.Delay(30000)
方法用于30s任务测试,您应该能够将其替换为从数据库中检索数据的任务。
另一答案
您应该执行以下操作:
- 通过基本文本回复确认用户的请求
- 保存消息信息并处理请求
- 收到系统回复后,请发出主动消息
以上是关于微软机器人框架网络聊天C#的主要内容,如果未能解决你的问题,请参考以下文章
NLP开发Python实现聊天机器人(微软Azure机器人服务)
业界 | 现代「罗塞塔石碑」:微软提出深度学习框架的通用语言