首页 > 解决方案 > Azure 机器人框架| 如果一段时间没有用户回复,那么如何给用户发送提醒呢?

问题描述

通过上述问题,我的意思是-

如果机器人 10 秒内没有任何活动

机器人发送消息>>看起来你现在不在。

Bot>> 等你回来后再次 Ping 我。暂时再见。

标签: c#botframeworkazure-web-app-service

解决方案


在 nodejs 中,您可以通过在轮流处理程序(onTurn 或 onMessage)中设置超时来执行此操作。如果您希望消息在用户的最后一条消息之后 X 次,则需要清除超时并在每轮重置它。超时将发送一次消息。如果您希望它重复,例如在用户的最后一条消息之后X 次,您可以使用间隔而不是超时。我发现发送消息的最简单方法是作为主动消息,因此您确实需要使用此方法包含TurnContextBotFrameworkAdapter从 botbuilder 库中获取。C# 的语法可能不同,但这应该为您指明正确的方向。这是我使用的功能:

    async onTurn(context) {

        if (context.activity.type === ActivityTypes.Message) {

            // Save the conversationReference
            const conversationData = await this.dialogState.get(context, {});
            conversationData.conversationReference = TurnContext.getConversationReference(context.activity);
            await this.conversationState.saveChanges(context);
            console.log(conversationData.conversationReference);

            // 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('Bad Request. Please ensure your message contains the conversation reference and message text.');
                    console.log(error);
                }
            }, 300000, conversationData.conversationReference);

            //<<THE REST OF YOUR TURN HANDLER>>
        }
    }

推荐阅读