【问题标题】:Bot Framework v4 - initiate chat with a user ( proactive messages )Bot Framework v4 - 发起与用户的聊天(主动消息)
【发布时间】:2020-06-10 19:05:43
【问题描述】:

我想在用户尚未与机器人聊天的情况下与他开始对话。

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

这里我如何使用 processActivity 事件捕获 referenceserviceUrl

然后我使用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.activityinside adapter.processActivityadapter.continueConversation 不同,并且拥有所有serviceUrlconversationId

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

【问题讨论】:

    标签: javascript azure botframework azure-bot-service


    【解决方案1】:

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

    可以使用以下方法检索 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--

    【讨论】:

      猜你喜欢
      • 2019-04-22
      • 1970-01-01
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多