Search code examples
javascriptbotframeworkmicrosoft-teams

How to send proactive message in a cron with botframework using javascript?


Can't find any relevant documentation for CloudAdapter.createConversationAsync. I can't find what I am doing wrong.

Here is the code I am trying to run:

const {
  CloudAdapter,
  ConfigurationBotFrameworkAuthentication,
  ConfigurationServiceClientCredentialFactory,
} = require('botbuilder');

const adapter = new CloudAdapter(new ConfigurationBotFrameworkAuthentication(
  {},
  new ConfigurationServiceClientCredentialFactory({
    MicrosoftAppId: process.env.MS_BOT_ID,
    MicrosoftAppPassword: process.env.MS_BOT_PASSWORD,
    MicrosoftAppType: 'MultiTenant',
  }),
));

const userId = 'XXXX';
const tenantId = 'YYYY';

adapter.createConversationAsync(
  process.env.MS_BOT_ID,
  'msteams',
  'https://smba.trafficmanager.net/teams/',
  userId, // audience
  {
    isGroup: false,
    bot: {
      id: process.env.MS_BOT_ID,
    },
    members: [{ id: userId }],
    tenantId,
  },
  console.log,
);

This is the error I have:

{"error":"invalid_resource","error_description":"AADSTS500011: The resource principal named XXXX was not found in the tenant named Bot Framework. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You might have sent your authentication request to the wrong tenant.\r\nTrace ID: -------\r\nCorrelation ID: -----\r\nTimestamp: 2023-05-24 09:10:35Z","error_codes":[500011],"timestamp":"2023-05-24 09:10:35Z","trace_id":"-------","correlation_id":"-------","error_uri":"https://login.microsoftonline.com/error?code=500011"}

Solution

  • I finally found how to use it! (thanks to github search)

    • audience needed to be null
    • 5th parameters needed to be { tenantId, members: [{ id }] }

    Here is how to use the function:

    adapter.createConversationAsync(
      MS_BOT_ID,
      'msteams',
      'https://smba.trafficmanager.net/teams/',
      null,
      {
        members: [{ id: userId }],
        tenantId,
      },
      console.log,
    );