Microsoft Bot框架Webchat C#

问题描述:

我正在使用C#中的Microsoft bot框架开发聊天机器人.我们具有查询数据库并返回结果的功能,但是返回结果最多可能需要25到30秒的时间.

I am developing a chatbot using the Microsoft bot framework in C#. We have a functionality where it queries the database and returns the result, but it might take up to 25-30 secs for the result to return.

到那时,漫游器会说无法发送,请重试".有没有办法增加这个超时时间?还是我们可以为用户提供类似请稍等"的消息,以便用户知道请求正在处理中?

By that time bot says "cannot send,please retry". Is there a way to increase this timeout? Or can we have something like "please wait" message for the user so that user will know that the request is processing?

在SDK中进行了硬编码,我们无法覆盖无法发送,请重试"之类的消息.正如Nicolas所说,一种解决方法是发送

It's hard coded in SDK, we're not able to override the message like "Couldn't send, retry". As Nicolas said, a workaround is to send a proactive message to user.

例如,您可以首先创建一个ConversationStarter.cs类,如下所示:

For example you can firstly create a ConversationStarter.cs class like this:

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);
    }
}

然后在对话框中,您可以像这样进行编码:

Then in your dialog, you can code like this:

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秒钟的任务测试,您应该可以将其替换为从数据库中检索数据的任务.

Then Task.Delay(30000) method is used for a 30s task testing, you should be able to replace it with your task for retrieving data from your database.