加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 01:26:46 所属栏目:百科 来源:网络整理
导读:我将两条消息发回用户作为回复,如下所示, static Timer t = new Timer(new TimerCallback(TimerEvent));static Timer t1 = new Timer(new TimerCallback(TimerEventInActivity));static int timeOut = Convert.ToInt32(ConfigurationManager.AppSettings["di
我将两条消息发回用户作为回复,如下所示,

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

OUTPUT

Here is what you get

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读