2

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

Diego
  • 336
  • 2
  • 21

1 Answers1

2

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

Nicolas R
  • 13,812
  • 2
  • 28
  • 57
  • Thanks for your help, Nicolas R. It seems to be the problem. Maybe it is a basic problem, but I am new in node.js... When I add your line, I get a 'MicrosoftAppCredentials is not defined' error... How can I add the required reference? – Diego Oct 17 '19 at 14:07
  • 1
    Take a look at your code: `var credentials = new BotConnector.MicrosoftAppCredentials...` . Must be the same origin – Nicolas R Oct 17 '19 at 14:20
  • Thanks, i did not see it. Now I get different error: "TypeError: source.on is not a function". I open a new question, since it is a different problem – Diego Oct 18 '19 at 07:48