gpt4 book ai didi

c# - Botframework 中的消息不会延迟

转载 作者:行者123 更新时间:2023-11-30 14:22:20 34 4
gpt4 key购买 nike

我正在向用户发回两条消息作为回复,如下所示,

static Timer t = new Timer(new TimerCallback(TimerEvent));
static Timer t1 = new Timer(new TimerCallback(TimerEventInActivity));
static int timeOut = Convert.ToInt32(ConfigurationManager.AppSettings["disableEndConversationTimer"]); //3600000

public static void CallTimer(int due) {
t.Change(due, Timeout.Infinite);
}

public static void CallTimerInActivity(int due) {
t1.Change(due, Timeout.Infinite);
}

public async static Task PostAsyncWithDelay(this IDialogContext ob, string text) {
try {
var message = ob.MakeMessage();
message.Type = Microsoft.Bot.Connector.ActivityTypes.Message;
message.Text = text;
await PostAsyncWithDelay(ob, message);

CallTimer(300000);

if ("true".Equals(ConfigurationManager.AppSettings["disableEndConversation"])) {
CallTimerInActivity(timeOut);
}

} catch (Exception ex) {
Trace.TraceInformation(ex.Message);
}

}

await context.PostAsyncWithDelay("Great!");
await context.PostAsyncWithDelay("I can help you with that.");

但是,在接收时它们之间没有延迟。两条消息一次性收到。

如何延迟第二条消息?

最佳答案

根对话框

要延迟您的消息,您可以使用 Task.Delay 方法。将您的 PostAsyncWithDelay 更改为:

public async static Task PostAsyncWithDelay(IDialogContext context, string text)
{
await Task.Delay(4000).ContinueWith(t =>
{
var message = context.MakeMessage();
message.Text = text;
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
{
var client = scope.Resolve<IConnectorClient>();
client.Conversations.ReplyToActivityAsync((Activity)message);
}

});
}

当你想延迟消息时,你可以调用 PostAsyncWithDelay 方法,否则使用 context.PostAsync 方法来发送你的消息。

  private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
//Sending a message nomally
await context.PostAsync("Hi");

//Notify the user that the bot is typing
var typing = context.MakeMessage();
typing.Type = ActivityTypes.Typing;
await context.PostAsync(typing);

//The message you want to delay.
//NOTE: Do not use context.PostAsyncWithDelay instead simply call the method.
await PostAsyncWithDelay(context, "2nd Hi");
}

输出

Here is what you get

关于c# - Botframework 中的消息不会延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50174202/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com