在botframework v4中,如果机器人闲置5分钟,如何给用户发送消息?

问题描述 投票:1回答:1

我想发送一个提示(我在等待),如果我没有收到任何消息从用户在直接在线网络聊天频道说5分钟后。

这是一个演示机器人,所以我使用本地内存存储。

任何帮助将被感激。

.net asp.net-core botframework
1个回答
0
投票

有几种方法可以处理这个问题。如果你是通过脚本调用这个(使用类似于botframework-webchat选项使用Directline通道),你可以检查出 这个答案在SO 其中告诉你如何在你的HTML文件中设置它。

如果你想在你的机器人中直接实现这个功能,你可以使用像Sainath Reddy提到的时间函数。然而,我注意到上下文对象会变得无效,所以你必须使用主动式消息传递来代替。我不确定这是否是最有效的方法,但以下是我如何能够实现这一点。

首先,你必须从botbuilder导入TurnContext和BotFrameworkAdapter。

const { TurnContext, BotFrameworkAdapter } = require('botbuilder');

然后,在onMessage函数中添加以下代码(如果你使用的是早期设置,则在onTurn中添加)。setTimeout 将只运行一次。您可以使用 setInterval 如果你想让它重复。

            // Save the conversationReference
            var conversationReference = TurnContext.getConversationReference(context.activity);

            // Reset the inactivity timer
            clearTimeout(this.inactivityTimer);
            this.inactivityTimer = setTimeout(async function(conversationReference) {
                console.log('User is inactive');
                try {
                    const adapter = new BotFrameworkAdapter({
                        appId: process.env.microsoftAppID,
                        appPassword: process.env.microsoftAppPassword
                    });
                    await adapter.continueConversation(conversationReference, async turnContext => {
                        await turnContext.sendActivity('Are you still there?');
                    });
                } catch (error) {
                    console.log(error);
                }
            }, 300000, conversationReference);

我喜欢这种方法,因为它 如果你使用的是 botframework-webchat 或类似的软件,它就可以工作。事实上,它在每一个渠道都可以工作。如果你不想在某些频道中发生这种情况,你必须在函数中添加一些额外的逻辑。或者,如果你只想在botframework-webchat这样的渠道中实现,你可以使用我链接的第一个方法。

© www.soinside.com 2019 - 2024. All rights reserved.