【问题标题】:Microsoft bot framework webchat C#Microsoft bot 框架网络聊天 C#
【发布时间】:2017-12-25 16:49:12
【问题描述】:

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

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

【问题讨论】:

  • 需要看代码。除非您在代码中设置计时器,否则 TCP 应用程序通常不会超时。

标签: c# .net timeout botframework


【解决方案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) 方法用于 30s 任务测试,您应该可以将其替换为从数据库中检索数据的任务。

【讨论】:

  • 感谢您的提示 :)
【解决方案2】:

您应该执行以下操作:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多