question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Twillio WhatsApp Adapter Microsoft botframework

See original GitHub issue

I cannot seem to find the problem with linking a microsoft bot to whatsapp via Twilio.

I have followed instructions to setup the Twilio WhatsApp adapter.

I have implemented this on the standard echo bot for testing

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

const dotenv = require('dotenv');
const path = require('path');
const restify = require('restify');

// This bot's main dialog.
const { TwilioWhatsAppAdapter } = require('@botbuildercommunity/adapter-twilio-whatsapp');

// Import required bot services.
// See https://aka.ms/bot-services to learn more about the different parts of a bot.
const { BotFrameworkAdapter } = require('botbuilder');

// This bot's main dialog.
const { EchoBot } = require('./bot');

// Import required bot configuration.
const ENV_FILE = path.join(__dirname, '.env');
dotenv.config({ path: ENV_FILE });
// Create HTTP server
const server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, () => {
    console.log(`\n${ server.name } listening to ${ server.url }`);
    console.log('\nGet Bot Framework Emulator: https://aka.ms/botframework-emulator');
    console.log('\nTo talk to your bot, open the emulator select "Open Bot"');
});

// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about how bots work.
const adapter = new BotFrameworkAdapter({
    appId: process.env.MicrosoftAppId,
    appPassword: process.env.MicrosoftAppPassword,
    channelService: process.env.ChannelService,
    openIdMetadata: process.env.BotOpenIdMetadata
});

const adapterSettings = {
    appId: process.env.MicrosoftAppId,
    appPassword: process.env.MicrosoftAppPassword
};

const twilioWhatsAppAdapterSettings = {
    accountSid: process.env.TWILIO_ACCOUNT_SID, // Account SID
    authToken: process.env.TWILIO_AUTH_TOKEN, // Auth Token
    phoneNumber: process.env.TWILIO_NUMBER, // The From parameter consisting of whatsapp: followed by the sending WhatsApp number (using E.164 formatting)
    endpointUrl: process.env.TWILIO_ENDPOINT_URL // Endpoint URL you configured in the sandbox, used for validation
};

const whatsAppAdapter = new TwilioWhatsAppAdapter(
    twilioWhatsAppAdapterSettings,
    adapterSettings);

// Catch-all for errors.
const onTurnErrorHandler = async (context, error) => {
    // This check writes out errors to console log .vs. app insights.
    // NOTE: In production environment, you should consider logging this to Azure
    //       application insights.
    console.error(`\n [onTurnError] unhandled error: ${ error }`);

    // Send a trace activity, which will be displayed in Bot Framework Emulator
    await context.sendTraceActivity(
        'OnTurnError Trace',
        `${ error }`,
        'https://www.botframework.com/schemas/error',
        'TurnError'
    );

    // Send a message to the user
    await context.sendActivity('The bot encountered an error or bug.');
    await context.sendActivity('To continue to run this bot, please fix the bot source code.');
};

// Set the onTurnError for the singleton BotFrameworkAdapter.
adapter.onTurnError = onTurnErrorHandler;

// Create the main dialog.
const myBot = new EchoBot();

// Listen for incoming requests.
server.post('/api/messages', (req, res) => {
    adapter.processActivity(req, res, async (context) => {
        // Route to main dialog.
        await myBot.run(context);
    });
});

server.post('/api/whatsapp/messages', (req, res) => {
    console.log('req', req);
    whatsAppAdapter.processActivity(req, res, async (context) => {
        console.log('context', context);

        // Route to main dialog.
        await myBot.run(context);
    });
});

// Listen for Upgrade requests for Streaming.
server.on('upgrade', (req, socket, head) => {
    // Create an adapter scoped to this WebSocket connection to allow storing session data.
    const streamingAdapter = new BotFrameworkAdapter({
        appId: process.env.MicrosoftAppId,
        appPassword: process.env.MicrosoftAppPassword
    });
    // Set onTurnError for the BotFrameworkAdapter created for each connection.
    streamingAdapter.onTurnError = onTurnErrorHandler;

    streamingAdapter.useWebSocket(req, socket, head, async (context) => {
        // After connecting via WebSocket, run this logic for every request sent over
        // the WebSocket connection.
        await myBot.run(context);
    });
});

Here is our configuration for BotFrameworkAdapterSettings:

TWILIO_NUMBER=+13396007034
TWILIO_ACCOUNT_SID=HIDDEN
TWILIO_AUTH_TOKEN=HIDDEN
TWILIO_ENDPOINT_URL=https://twilio-second-try.azurewebsites.net/api/whatsapp/messages

Here is the configuration in Twilio dashboard: image

Here is the configuration in Twilio Whatsapp dashboard: image

Here is the error we are getting in Twilio Debugger dashboard: image

Could you please let me know if I am missing something in my configuration / implementation ? Thanks.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:17 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
iMicknlcommented, Jun 15, 2020

@nboussarsaracn great to hear! And by now, you should also have improved your debugging experience on Azure App Service a little bit. In the Application folder with different logfiles, you should also be able to find some of the errors you experienced before.

1reaction
nboussarsaracncommented, Jun 15, 2020

Hi @iMicknl, I changed it to the Whatsapp Number provided by Twilio and now it works like charm 😃 Thank you for the support!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Twilio WhatsApp Adapter (beta) - botbuilder-community-js
The Twilio WhatsApp adapter for the Microsoft Bot Framework allows you to add an additional endpoint to your bot for use with Twilio...
Read more >
botbuilder-sangam-twilio-whatsapp-adapter - PyPI
The Twilio WhatsApp adapter for the Microsoft Bot Framework allows you to add an additional endpoint to your bot for use with Twilio...
Read more >
Publish bot in Microsoft Botframework with Twilio for WhatsApp
After developing a Chatbot with Microsoft Bot Framework and publishing it in the Azure portal I want to use it in WhatsApp.
Read more >
Connect a bot to Twilio (SMS) - Azure - Microsoft Learn
Learn how to configure bots to use SMS via Twilio to communicate with people with a TwiML application or the Twilio adapter.
Read more >
Microsoft.Bot.Builder.Adapters.Twilio 4.18.1 - NuGet
Version Downloads Last updated 4.18.1 187 2 months ago 4.17.2 251 3 months ago 4.17.1 280 5 months ago
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found