After a user installs my Teams app and bot I capture the initial conversation event and send a notification to the users main channel using that reference.
However, if I restart my server and run the same code, I get an error that the bot is not authorized.
I am calling MicrosoftAppCredentials.trustedServiceUrl()
right before calling turnContext.sendActivity()
as specified in the docs https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/javascript_nodejs/16.proactive-messages#avoiding-permission-related-errors . And calling MicrosoftAppCredentials.isTrustedServiceUrl
returns true
right before I call turnContext.sendActivity
.
I basically copied the code from this project for my implementation.
{ 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.' } }
This is still my proof of concept implementation so all the values are hardcoded to make it easier.
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');
}
);
}
Turns out the botbuilder
dependency was outdated. I was running 4.7.2
and the latest is 4.8.0
. It's worrying that such a minor version release will render past versions useless and without any notice.