我有一个主动消息传递端点,当用户积极参与对话时,该端点可以正常工作。例如,在一个用例中,用户要求检索发票数据,这是异步发生的,并通过主动消息发送回去。这很好。但是,如果尝试在几周后继续对话,则该continueConversation
操作将失败。如果我进入团队并与机器人发起新的对话,那么重新发送主动消息(不进行任何更改)将再次起作用。
在我的一种使用案例中,机器人需要在未来1周内与用户进行跟进。因此,我需要弄清楚如何向团队用户发送主动消息,即使他们最近没有与机器人聊天。我不确定为什么continueConversation
不起作用,因为对话ID不变(而且几个月内也没有变化,也许永远都没有)。
这是我用来发送主动消息的功能。
server.post('/api/notify', async (req, res) => {
//console.log(req.body);
try {
const conversationReference = req.body.conversationReference;
await adapter.continueConversation(conversationReference, async turnContext => {
// If you encounter permission-related errors when sending this message, see
// https://aka.ms/BotTrustServiceUrl
await turnContext.sendActivity(req.body.message);
});
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.write('<html><body><h1>Proactive messages have been sent.</h1></body></html>');
res.end();
} catch (error) {
console.log('Bad Request. Please ensure your message contains the conversation reference and message text.');
console.log(error);
appInsightsClient.trackTrace({
message: `${error.name} - ${path.basename(__filename)}`,
severity: 4,
properties: {'error':error.message,'callStack':error.stack, 'botName': process.env.botName}
});
res.setHeader('Content-Type', 'text/html');
res.writeHead(400);
res.write('<html><body><p>Bad Request. Please ensure your message contains the conversation reference and message text.</p></body></html>');
res.end();
}
});
如果您的机器人重新启动,则等待主动消息的用户将无法收到它,除非他们在重新启动机器人后再次向机器人发送了消息。
这就是问题所在。但是该页面上的说明并未提供完整的细节。您必须通过添加类const { MicrosoftAppCredentials } = require('botframework-connector')
,并将serviceUrl设置为pass(已在上提供conversationReference.serviceUrl
)。
因此,通过这些更改,我MicrosoftAppCredentials.trustServiceUrl(conversationReference.serviceUrl);
在发送主动消息之前添加了该消息,即使在重新启动bot之后,它也可以正常工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句