Bot Framework v4-与用户聊天(主动消息)

时间:2020-06-10 19:05:43

标签: javascript azure botframework azure-bot-service

我想与用户开始对话,而无需他已经与机器人聊天。

当用户将机器人应用安装为mentionned here时,我将继续获取conversationId

这是我如何通过reference事件来捕捉serviceUrlprocessActivity

然后我使用continueConversation + reference

const {
    BotFrameworkAdapter,
} = require('botbuilder');
const adapter = new BotFrameworkAdapter({
    appId: "xxxx",
    appPassword: "xxxxy"
})
module.exports = async function(context, req) {
    console.log(adapter);
    try {

        adapter.processActivity(req, context.res, async (turnContext) => {
            const reference = turnContext.activity.conversation.id
            const serviceUrl = turnContext.activity.serviceUrl

            await adapter.continueConversation(reference, async (turnContext) => {
                try {
                    await turnContext.sendActivity("this is proacive message");

                } catch (e) {
                    console.log(e)
                }
            });

        });
    } catch (e) {
        console.log(e)
    }
    return;
};

我遇到此错误

Error: BotFrameworkAdapter.sendActivity(): missing serviceUrl.

我已经检查了turnContext.activity值。我得到: {"type":"event","name":"continueConversation"}"所有其他值都是未定义的(也是serviceUrl)

我注意到turnContext.activity内部的adapter.processActivityadapter.continueConversation中的serviceUrlconversationId都相同

如何编辑此代码示例,以便能够向用户发送主动消息?

1 个答案:

答案 0 :(得分:2)

如果将const引用用于BotFrameworkAdapter.continueConversation(),则const引用不仅需要是ID,还需要是ConversationReference。

可以使用以下方法检索ConversationReference:

const reference = TurnContext.getConversationReference(turnContext.activity);

https://docs.microsoft.com/en-us/javascript/api/botbuilder/botframeworkadapter?view=botbuilder-ts-latest#continueconversation-partial-conversationreference----context--turncontext-----promise-void--

https://docs.microsoft.com/en-us/javascript/api/botbuilder-core/turncontext?view=botbuilder-ts-latest#getconversationreference-partial-activity--