繁体   English   中英

如何在Bot Framework上检测机器人空闲

[英]how to detect bot Idleness on Bot Framework

我正在使用带有C#的bot框架V3。 我需要确定我的机器人何时闲置超过5分钟。 我试图通过MessageController处理机器人的空闲状态,但是我的尝试似乎没有效果。

switch (activity.Type)
            {
                case ActivityTypes.Message:
            await Task.Delay(5000).ContinueWith(async (t) =>
                   {
                        var reply = activity.CreateReply();                        
                            var myMessage = "Bot time out. Bye";
                            reply.Text = myMessage;
                            await connector.Conversations.ReplyToActivityAsync(reply);                     

                       });
            await Task.Factory.StartNew(() => Conversation.SendAsync(activity, () => new Dialogs.RootDialog(luisService).DefaultIfException()));
                }
                break;
}

有什么事吗 您可以分享任何样品吗? 提前谢谢!

首先,您只会延迟5秒(5000毫秒),而不是5分钟。

无论如何,您可以尝试以下方法。 添加此类:

public static class TimeoutConversations
    {
        const int TimeoutLength = 10;
        private static Timer _timer;
        private static TimeSpan _timeoutLength;

        static TimeoutConversations()
        {
            _timeoutLength = TimeSpan.FromSeconds(TimeoutLength);
            _timer = new Timer(CheckConversations, null, TimeSpan.Zero, TimeSpan.FromSeconds(5));
        }

        static ConcurrentDictionary<string, UserInfo> Conversations = new ConcurrentDictionary<string, UserInfo>();

        static async void CheckConversations(object state)
        {
            foreach (var userInfo in Conversations.Values)
            {
                if (DateTime.UtcNow - userInfo.LastMessageReceived >= _timeoutLength)
                {
                    UserInfo removeUserInfo = null;
                    Conversations.TryRemove(userInfo.ConversationReference.User.Id, out removeUserInfo);

                    var activity = userInfo.ConversationReference.GetPostToBotMessage();
                    //clear the dialog stack and conversation state for this user
                    using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
                    {
                        var botData = scope.Resolve<IBotData>();
                        await botData.LoadAsync(CancellationToken.None);

                        var stack = scope.Resolve<IDialogStack>();
                        stack.Reset();

                        //botData.UserData.Clear();
                        botData.ConversationData.Clear();
                        botData.PrivateConversationData.Clear();
                        await botData.FlushAsync(CancellationToken.None);
                    }

                    MicrosoftAppCredentials.TrustServiceUrl(activity.ServiceUrl);
                    var connectorClient = new ConnectorClient(new Uri(activity.ServiceUrl), ConfigurationManager.AppSettings["MicrosoftAppId"], ConfigurationManager.AppSettings["MicrosoftAppPassword"]);
                    var reply = activity.CreateReply("I haven't heard from you in awhile.  Let me know when you want to talk.");
                    connectorClient.Conversations.SendToConversation(reply);

                    //await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
                }
            }
        }

        public static void MessageReceived(Activity activity)
        {
            UserInfo userInfo = null;
            if (Conversations.TryGetValue(activity.From.Id, out userInfo))
            {
                userInfo.LastMessageReceived = DateTime.UtcNow;
            }
            else
            {
                Conversations.TryAdd(activity.From.Id, new UserInfo()
                {
                    ConversationReference = activity.ToConversationReference(),
                    LastMessageReceived = DateTime.UtcNow
                });
            }
        }
    }
    public class UserInfo
    {
        public ConversationReference ConversationReference { get; set; }
        public DateTime LastMessageReceived { get; set; }
    }

然后在消息控制器中,调用:

TimeoutConversations.MessageReceived(activity);

在此示例中,它将执行10秒超时,每5秒检查一次。 这是一个使对话超时的基本(有点草率)计时器。 您可能会遇到bug,但是可以对其进行调整,直到它适合您的需求为止。 使用天蓝色队列或其他方法可能更好。

这是用于v4的DCR,可实现此基本功能: https : //github.com/microsoft/botframework-sdk/issues/5237

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM