2
votes

I'm trying to send a proactive message using botframework in Nodejs (Teams channel), but a received 401 error.

I make some searches and I found that the error can be possible with the trust service URL, but I have already done that part.

My adpter config

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
})

Send proactive message code

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)
        });

Request and response, with my AppId and conversation 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."}',
>      headers: HttpHeaders { _headersMap: [Object] },
>      status: 401
>    },
>    body: { message: 'Authorization has been denied for this request.' }
>  }

configs

send proactive message

Request and response

1
As we got this closed in your GH issue, please "accept" and upvote it so others can quickly find the answer and I can clear this from my support tracker.mdrichardson

1 Answers

0
votes

I just saw this was a duplicate of your GitHub issue. I'll post my answer here for others:

The issue is that in sendMessage.f.js, you're instantiating a new BotFrameworkAdapter, which differs from the one instantiated in gateway.f.js, which you use to create the conversationReference.

They look the same because you're passing in the same appId/password, but their BotIdentityKey differs because the key is generated using Symbol(), which as you may know, guarantees a unique key gets generated every time it's called, even with the same input. This BotIdentityKey is used in some auth chaching, deep in the code.

If you want to keep them in separate files, I'd recommend exporting your adapter in gateway.f.js and importing it in sendMessage.f.js.

Note: I confirmed this was the issue by running Sample 57, verifying it worked, then instantiating a new adapter in the proactive message section. I, too, got the 401 error.