2
votes

I have a TEAMS node.js bot running locally (with ngrok). I receive messages from TEAMS client and echo works

context.sendActivity(`You said '${context.activity.text}'`);

Now I want to send a 1to1 message to this user, but I receive

Error: Authorization has been denied for this request

when creating a conversation.

My code:

   var sUserId  = "29:1shb_5I6CkkerBVq4qPqcv5dGwDfkXx11Jbjc1UnGCIv"
   var sServiceUrl = "https://smba.trafficmanager.net/emea/";
   var sTenantId = "942369d2-208e-438b-894c-0d0e1510cf61";
   var credentials = new BotConnector.MicrosoftAppCredentials({
        appId: "xxxxxxx",
        appPassword: "yyyyyyyy"
    });
    var connectorClient = new BotConnector.ConnectorClient(credentials, { baseUri: sServiceUrl });

    const parameters = {
        members: [ { id: sUserId } ],
        isGroup: false,
        channelData:
        {
            tenant: {
                id: sTenantId
            }
        }
    };
    var conversationResource = await connectorClient.conversations.createConversation(parameters);

// I get the error here, next is not executed
    await connectorClient.conversations.sendToConversation(conversationResource.id, {
        type: "message",
        from: { id: "xxxxxxx" },
        recipient: { id: sUserId },
        text: 'This a message from Bot Connector Client (NodeJS)'
    });

appId & appPassword are valid (from .env file), if they are wrong I can not receive messages from TEAMS client

I have the same code to create a conversation in a .NET bot and it works for me:

  var parameters = new ConversationParameters
   {
       Members = new[] { new ChannelAccount(sUserId) },
       ChannelData = new TeamsChannelData
       {
            Tenant = new TenantInfo(sTenantId),
       },
   };

   retValue = await connectorClient.Conversations.CreateConversationAsync(parameters);

What is wrong in my node.js code?

Thanks,

Diego

1

1 Answers

2
votes

Have you trusted the service? It don't think so based on your code, and it's a classic cause of 401 in your case.

In node.js, do the following:

MicrosoftAppCredentials.trustServiceUrl(serviceUrl);

If you want more details around that, have a look to the documentation about getting 401 when sending proactive messages here

And also this SO answer about Teams and Proactive messaging, in particular last block. Proactive messaging bot in Teams without mentioning the bot beforehand