问题描述
我正在尝试使用Node.js(团队通道)中的botframework发送主动消息,但收到401错误。
我进行了一些搜索,发现使用信任服务URL可能会出错,但是我已经完成了这一部分。
我的adpter配置
const {
BotFrameworkAdapter,} = require('botbuilder');
const { MicrosoftAppCredentials } = require('botframework-connector');
const adapter = new BotFrameworkAdapter({
appId: process.env.MICROSOFT_APP_ID,appPassword: process.env.MICROSOFT_APP_PASSWORD
})
发送主动消息代码
adapter.continueConversation(address,async (t) => {
MicrosoftAppCredentials.trustServiceUrl(process.env.MICROSOFT_BOT_SERVICE_URL);
await t.sendActivity(model.render());
}).then((r) => {
console.log("continue")
console.log(r)
res.status(200).send({
status: "OK"
})
}).catch((e) => {
console.log(e);
res.send("ERRO " + e)
});
请求和响应以及我的AppId和会话ID。
statusCode: 401,> request: WebResource {
> streamResponseBody: false,> url: 'https://smba.trafficmanager.net/amer/v3/conversations/a%3A1MUpsVB7CH-6BTiSUHxOkMhv05saxu9O7qe0zRNPR04PCvXp-6QzsoYKpT-oykqyJpu8SgbawTkbUDauiBGF9bIeG9qg56Ts6lpEGgY6SSrMMj5YL_K-yxOJ5jjoqIrJQ/activities',> method: 'POST',> headers: HttpHeaders { _headersMap: [Object] },> body: '{"type":"message","serviceUrl":"https://smba.trafficmanager.net/amer/","channelId":"msteams","from":{"id":"c96afa27-addb-4bc8-80fb-c0317380bf1a","name":"Luna"},"conversation":{"id":"a:1MUpsVB7CH-6BTiSUHxOkMhv05saxu9O7qe0zRNPR04PCvXp-6QzsoYKpT-oykqyJpu8SgbawTkbUDauiBGF9bIeG9qg56Ts6lpEGgY6SSrMMj5YL_K-yxOJ5jjoqIrJQ"},"text":"Achei aqui! A OV de número 0001302956","inputHint":"acceptingInput"}',> query: undefined,> formData: undefined,> withCredentials: false,> abortSignal: undefined,> timeout: 0,> onUploadProgress: undefined,> onDownloadProgress: undefined,> operationSpec: {
> httpMethod: 'POST',> path: 'v3/conversations/{conversationId}/activities',> urlParameters: [Array],> requestBody: [Object],> responses: [Object],> serializer: [Serializer]
> }
> },> response: {
> body: '{"message":"Authorization has been denied for this request."}',> status: 401
> },> body: { message: 'Authorization has been denied for this request.' }
> }
解决方法
我刚刚看到这是a duplicate of your GitHub issue。我将在这里为他人发布答案:
问题在于,在sendMessage.f.js
中,您是instantiating a new BotFrameworkAdapter
,与实例化的in gateway.f.js
(用于创建对话参考)不同。
它们外观相同,因为您传递的是相同的appId /密码,但是their BotIdentityKey
differs是因为密钥是使用Symbol()
生成的,您可能知道, guarantees a unique key gets generated every time it's called,even with the same input.此BotIdentityKey用于代码深处的某些身份验证缓存。
如果要将它们保存在单独的文件中,建议将适配器导出到gateway.f.js
中,然后将其导入sendMessage.f.js
中。
注意:我通过运行示例57,验证它是否有效,然后在主动消息部分中实例化了新适配器来确认这是问题所在。我也遇到了401错误。