微软机器人框架网络聊天C#

时间:2017-12-24 07:09:30

标签: c# .net timeout botframework

我正在使用C#中的Microsoft bot框架开发一个聊天机器人。我们有一个查询数据库并返回结果的功能,但返回结果可能需要25-30秒。

到那时机器人说“不能发送,请重试”。有没有办法增加这个超时?或者我们可以为用户提供类似“请等待”的消息,以便用户知道请求正在处理吗?

2 个答案:

答案 0 :(得分:1)

它在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)方法用于30秒的任务测试,您应该能够将其替换为从数据库中检索数据的任务。

答案 1 :(得分:0)

您应该执行以下操作:

  1. 通过基本文本回复确认用户的请求
  2. 保存邮件信息并处理请求
  3. 在您收到系统回复后发出主动消息