Bot 在使用保存的对话引用和调用 trustServiceUrl 重新启动后无法进行身份验证
Bot fails to authenticate after restart with saved conversation reference and calling trustServiceUrl
在用户安装我的 Teams 应用程序和机器人后,我捕获初始对话事件并使用该引用向用户主频道发送通知。
但是,如果我重新启动我的服务器并 运行 相同的代码,我会收到一个错误,指出该 bot 未被授权。
我在调用 turnContext.sendActivity()
之前调用 MicrosoftAppCredentials.trustedServiceUrl()
,如文档 https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/javascript_nodejs/16.proactive-messages#avoiding-permission-related-errors 中所述。在我调用 turnContext.sendActivity
.
之前调用 MicrosoftAppCredentials.isTrustedServiceUrl
returns true
我基本上是从这个项目中复制代码来实现的。
{ Error: Authorization has been denied for this request.
at new RestError (...../node_modules/@azure/ms-rest-js/lib/restError.ts:18:5)
at ..../node_modules/@azure/ms-rest-js/lib/policies/deserializationPolicy.ts:117:27
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
code: undefined,
statusCode: 401,
request:
WebResource {
streamResponseBody: false,
url: 'https://smba.trafficmanager.net/amer/v3/conversations/{convo ID}%40thread.skype/activities/{activiyID}',
method: 'POST',
headers: HttpHeaders { _headersMap: [Object] },
body: '{"type":"message","serviceUrl":"https://smba.trafficmanager.net/amer/","channelId":"msteams","from":{"id":"{ID}","name":"CrossLead Dev Local"},"conversation":{"isGroup":true,"conversationType":"channel","id":"{ID}","tenantId":"{ID}"},"recipient":{"id":"{ID}","aadObjectId":"{ID}"},"text":"The bot encountered an error or bug.","inputHint":"acceptingInput","replyToId":"{ID}"}',
query: undefined,
formData: undefined,
withCredentials: false,
abortSignal: undefined,
timeout: 0,
onUploadProgress: undefined,
onDownloadProgress: undefined,
operationSpec:
{ httpMethod: 'POST',
path: 'v3/conversations/{conversationId}/activities/{activityId}',
urlParameters: [Array],
requestBody: [Object],
responses: [Object],
serializer: [Object] } },
response:
{ body: '{"message":"Authorization has been denied for this request."}',
headers: HttpHeaders { _headersMap: [Object] },
status: 401 },
body: { message: 'Authorization has been denied for this request.' } }
这仍然是我的概念实施证明,因此所有值都经过硬编码以使其更容易。
const msBotAdapter = new BotFrameworkAdapter({
appId: {ID as string}
appPassword: {ID as string},
});
//Some people also said to do this, but never used it anywhere. It doesn't seem to help
const msCreditials = new MicrosoftAppCredentials(
{ID as string},
{ID as string}
);
//Save Conversation Reference from when the app was installed into teams
const conversationReference: {
[key: string]: Partial<ConversationReference>;
} = {
activityId: '{activity ID}',
//@ts-ignore
user: {
id:
'{ID}',
aadObjectId: '{ID}',
},
bot: {
id: '{Bot Id}',
name: '{Bot Name}',
},
//@ts-ignore
conversation: {
isGroup: true,
conversationType: 'channel',
tenantId: '{tenant ID}',
id: '19:{ID}@thread.skype',
},
channelId: 'msteams',
serviceUrl: 'https://smba.trafficmanager.net/amer/',
};
const postMessage = async () => {
MicrosoftAppCredentials.trustServiceUrl(conversationReference.serviceUrl);
await msBotAdapter.continueConversation(
conversationReference as any,
async turnContext => {
// If you encounter permission-related errors when sending this message, see
// https://aka.ms/BotTrustServiceUrl
await turnContext.sendActivity('test message');
}
);
}
原来 botbuilder
依赖项已过时。我是 运行 4.7.2
,最新的是 4.8.0
。令人担忧的是,这样的小版本发布会使过去的版本无用且没有任何通知。
在用户安装我的 Teams 应用程序和机器人后,我捕获初始对话事件并使用该引用向用户主频道发送通知。
但是,如果我重新启动我的服务器并 运行 相同的代码,我会收到一个错误,指出该 bot 未被授权。
我在调用 turnContext.sendActivity()
之前调用 MicrosoftAppCredentials.trustedServiceUrl()
,如文档 https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/javascript_nodejs/16.proactive-messages#avoiding-permission-related-errors 中所述。在我调用 turnContext.sendActivity
.
MicrosoftAppCredentials.isTrustedServiceUrl
returns true
我基本上是从这个项目中复制代码来实现的。
{ Error: Authorization has been denied for this request.
at new RestError (...../node_modules/@azure/ms-rest-js/lib/restError.ts:18:5)
at ..../node_modules/@azure/ms-rest-js/lib/policies/deserializationPolicy.ts:117:27
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
code: undefined,
statusCode: 401,
request:
WebResource {
streamResponseBody: false,
url: 'https://smba.trafficmanager.net/amer/v3/conversations/{convo ID}%40thread.skype/activities/{activiyID}',
method: 'POST',
headers: HttpHeaders { _headersMap: [Object] },
body: '{"type":"message","serviceUrl":"https://smba.trafficmanager.net/amer/","channelId":"msteams","from":{"id":"{ID}","name":"CrossLead Dev Local"},"conversation":{"isGroup":true,"conversationType":"channel","id":"{ID}","tenantId":"{ID}"},"recipient":{"id":"{ID}","aadObjectId":"{ID}"},"text":"The bot encountered an error or bug.","inputHint":"acceptingInput","replyToId":"{ID}"}',
query: undefined,
formData: undefined,
withCredentials: false,
abortSignal: undefined,
timeout: 0,
onUploadProgress: undefined,
onDownloadProgress: undefined,
operationSpec:
{ httpMethod: 'POST',
path: 'v3/conversations/{conversationId}/activities/{activityId}',
urlParameters: [Array],
requestBody: [Object],
responses: [Object],
serializer: [Object] } },
response:
{ body: '{"message":"Authorization has been denied for this request."}',
headers: HttpHeaders { _headersMap: [Object] },
status: 401 },
body: { message: 'Authorization has been denied for this request.' } }
这仍然是我的概念实施证明,因此所有值都经过硬编码以使其更容易。
const msBotAdapter = new BotFrameworkAdapter({
appId: {ID as string}
appPassword: {ID as string},
});
//Some people also said to do this, but never used it anywhere. It doesn't seem to help
const msCreditials = new MicrosoftAppCredentials(
{ID as string},
{ID as string}
);
//Save Conversation Reference from when the app was installed into teams
const conversationReference: {
[key: string]: Partial<ConversationReference>;
} = {
activityId: '{activity ID}',
//@ts-ignore
user: {
id:
'{ID}',
aadObjectId: '{ID}',
},
bot: {
id: '{Bot Id}',
name: '{Bot Name}',
},
//@ts-ignore
conversation: {
isGroup: true,
conversationType: 'channel',
tenantId: '{tenant ID}',
id: '19:{ID}@thread.skype',
},
channelId: 'msteams',
serviceUrl: 'https://smba.trafficmanager.net/amer/',
};
const postMessage = async () => {
MicrosoftAppCredentials.trustServiceUrl(conversationReference.serviceUrl);
await msBotAdapter.continueConversation(
conversationReference as any,
async turnContext => {
// If you encounter permission-related errors when sending this message, see
// https://aka.ms/BotTrustServiceUrl
await turnContext.sendActivity('test message');
}
);
}
原来 botbuilder
依赖项已过时。我是 运行 4.7.2
,最新的是 4.8.0
。令人担忧的是,这样的小版本发布会使过去的版本无用且没有任何通知。