Bot Framework v4 - 发起与用户的聊天(主动消息)
Bot Framework v4 - initiate chat with a user ( proactive messages )
我想开始与用户对话,但他已经与机器人聊天。
当用户将机器人应用程序安装为 mentionned here
时,我正在继续获取 conversationId
这是我如何使用 processActivity
事件
捕获 reference
和 serviceUrl
然后我用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)
我注意到 adapter.processActivity
中的 turnContext.activity
与 adapter.continueConversation
中的不一样并且具有所有 serviceUrl
和 conversationId
如何编辑此代码示例以便能够向用户发送主动消息?
const 引用需要是 ConversationReference,而不仅仅是 Id,如果您将它用于 BotFrameworkAdapter.continueConversation()。
可以使用以下方法检索 ConversationReference:
const reference = TurnContext.getConversationReference(turnContext.activity);
我想开始与用户对话,但他已经与机器人聊天。
当用户将机器人应用程序安装为 mentionned here
时,我正在继续获取conversationId
这是我如何使用 processActivity
事件
reference
和 serviceUrl
然后我用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)
我注意到 adapter.processActivity
中的 turnContext.activity
与 adapter.continueConversation
中的不一样并且具有所有 serviceUrl
和 conversationId
如何编辑此代码示例以便能够向用户发送主动消息?
const 引用需要是 ConversationReference,而不仅仅是 Id,如果您将它用于 BotFrameworkAdapter.continueConversation()。
可以使用以下方法检索 ConversationReference:
const reference = TurnContext.getConversationReference(turnContext.activity);