Creating and Connecting Azure Bot with Twilio SMS

January 25, 2023
Written by
KK Gan
Twilion
Reviewed by
Paul Kamp
Twilion

Azure Bot Twilio SMS

During one of my recent presentations to Microsoft communities, I shared how to configure Azure Bot with Twilio SMS. The challenge I was facing was not so much with the configuration of the chatbot with Twilio SMS, but more with the creation and deployment of the bot itself – there are different frameworks, tools, versions, and environments to consider. In this article, I would like to share how I did my integration and hope it will help others who might need it.

There are three sections in this article:

  1. Create a bot with the Bot Framework SDK
  2. Deploy the bot to Azure cloud
  3. Connecting the bot with Twilio SMS

Prerequisites

To complete this tutorial, you'll need an account with Microsoft Azure and Twilio, if you don't have those.

Create a bot with the Bot Framework SDK

There are three options to create a bot on the Microsoft platform - Power Virtual Agents, Health Bot, and Bot Framework SDK (please refer to Choose the right chatbot solution for your use case for all the details). We are going to create our bot with the Bot Framework SDK, and I am using a Mac and JavaScript for this exercise.

Creating a bot with the Bot Framework SDK is straightforward. We will follow the steps from Microsoft documentation to Create a bot with the Bot Framework SDK and with all the prerequisites outlined below.

  • Node.js (I am using version 16, I've tested v18+ didn't work for me)
  • Latest version of npm and yeoman (follow the instructions from the Create a bot with the Bot Framework SDK to install if you have not done this yet)
  • Bot Framework Emulator
  • Visual Studio Code

Below are the steps I followed for the purpose:

1. Open a terminal.

2. Create and switch to the folder for our bot project. (note: I created a folder with the name “monkey-kong” for the bot I am going to build).

mkdir monkey-kong
cd monkey-kong

3. Create a bot with the Yeoman generator. (note: I assume we already have the prerequisites installed with yeoman and the generator-botbuilder, but follow the instructions in this link if you don't have this: Create a bot with the Bot Framework SDK).

yo botbuilder

Below captures my terminal output running the above command:

monkey-kong % yo botbuilder     

Welcome to the Microsoft Bot Builder generator v4.17.0. 

Detailed documentation can be found at https://aka.ms/botbuilder-generator

? What's the name of your bot? monkey-kong
? What will your bot do? Demonstrate the core capabilities of the Microsoft Bot Framework
? What programming language do you want to use? JavaScript
? Which template would you like to start with? Echo Bot - https://aka.ms/bot-template-echo
? Looking good.  Shall I go ahead and create your new bot? (Y/n) Yes

4. Start the bot (from your project folder – “monkey-kong”, in my case).

npm start

At this point, the bot should be running locally on port 3978 (the below screen shows the output of the npm start command).

> monkey-kong@1.0.0 start
> node ./index.js

(node:89247) [DEP0111] DeprecationWarning: Access to process.binding('http_parser') is deprecated.
(Use `node --trace-deprecation ...` to show where the warning was created)

restify listening to http://[::]:3978

Get Bot Framework Emulator: https://aka.ms/botframework-emulator

To talk to your bot, open the emulator select "Open Bot"

5. Connect and test the bot with Emulator

5.1. Start the Bot Framework Emulator

5.2. Select Open Bot on the Emulator’s Welcome tab

Bot Framework Emulator Welcome

5.3. Enter the bot’s URL (e.g., http://localhost:3978/api/messages), and click Connect

Opem a bot

5.4. Try to send a message in the chat window, and the bot will respond back

Bot Emulator

Deploy your bot to Azure

I found it tricky to deploy my bot directly, so we will use Visual Studio Code with the Azure Extension to provision Azure resources and deploy our Node.js bot to Azure.

With Visual Studio Code and Azure Extension setup, open the Node.js project we have created previously, create the required resources with the Azure Extension to prepare our Azure environment for bot deployment.

To do that, change to the project folder in the terminal and start Visual Studio Code with the code . command.

code .

The Visual Studio Code will open our project folder as shown below.

VS Code NavBar

Sign-in to Azure from Visual Studio Code, if you have not done so.

VS Code Sign in menu

Once signed in, you should see Azure subscription(s) and resources under the subscription similar to the below capture:

VS Code Azure Panel

Create a Resource Group

Before we deploy our bot solution to Azure from the Visual Studio Code with Azure Extension, we need to prepare Azure environment with all the required resources (note: we are using Visual Studio to create resources in this exercise). Select the + sign next to the RESOURCES navigation as shown below, and search for the “create resource group” command to use.

VS Code - Creare Resource Group

Enter the resource group name (“monkey” in my case), then select a location where the resource group to be created. Once done, you should see the new resource group added to the resource navigation as shown below (note: you may also login to the Azure portal to verify).

VS Code Verify Added Resource Group

Create App Service

To create Azure App Service from the Azure Extension panel, right click on the App Services and select Create New Web App... (Advanced). (note: use the Advanced option so you will be able to select which resource group to add the App Service).

VS Code Azure App Service

Enter the App Service name and hit enter.

VS Code Create New Web App

Select the Resource Group we have just created ("monkey" as used in this example).

VS Code Create New Web App 2

Select “Node 16 LTS” for the runtime stack. (the NodeJS version we used to create the bot).

VS Code Create New Web App 3

I have picked Linux for the OS as shown below, and the location for the new resources.

VS Code Create New Web App 4

VS Code Create New Web App 5

We will need to create a new App Service plan for the purpose.

VS Code Create New Web App 6

I am using the same name “monkey-web” and Free (F1) for the new App Service plan.

VS Code Create New Web App 7

VS Code Create New Web App 8

Skip the creation of Application Insights for now.

VS Code Create New Web App 9

Once the web app is created, a message will be shown in the bottom right corner of the Visual Studio Code.

VS Code Create New Web App Prompt 1

Verify if the App Service and App Service Plan were created in the Azure portal.

Azure Portal - Resource Group

Deploy the NodeJS code to the created App Service

Right click on the newly created App Service, then select Deploy to Web App… from the pop-up menu.

VS Code Deploy to Web

Select the project (the folder) to be deployed to the selected Web App.

VS Code Deploy to Web - folder

VS Code Deploy to Web Confirm

Select Yes for redeployment of the project to the same web app.

VS Code Deploy to Web Confirm 2

Once the application is deployed to the Web App, you will see the message Deployment to .. completed..

VS Code Deploy to Web Completed

Azure Portal - Create Azure Bot

Provide the bot details such as the Bot handle, subscription, and Resource group. We will be using User-Assigned Managed Identity for this exercise.

Bot Handle, Subscription, and Resource group, plus the App Type

Make sure the “Create new Microsoft App ID” is selected, and click “Review + create” to continue.

Create a new Microsoft App ID

Review the details and click “Create” to create the Azure Bot.

Review the details to create an Azure bot

Azure bot deployment complete

Once the Azure Bot is created, verify that the Azure Bot and Managed ID were added to the resource group.

Verify Azure Bot and Managed ID were added

Configure the App Service to add the Managed Identity

From the Resource Group, select the App Service.

Select the App service from Resource Group

Select Identity and add the managed identity created earlier to the User assigned tab.

Set the User Assigned identity

Once added, the user id will be shown under the “User assigned” tab as below.

Verify Managed ID was added

Add application settings to the App Service

Add the following application parameters to the App Service we have created earlier.

  • MicrosoftAppType
  • MicrosoftAppId
  • MicrosoftAppPassword
  • MicrosoftAppTenantId

Repeat step 2 and 3 as shown in the screen below to add the above Application variables and values (values are from the Azure Bot configuration as shown in the next screen capture).

Add application configuration variables

Fill these parameters with the values from the Azure Bot as shown below. (note: The order of these parameters may show up differently from Application Settings page, we just need to make sure the right values assigned to the correspondent parameters.)

Populate values in the configuration

Save the changes, and update the Managing endpoint of the Azure Bot with the URL of the App Service (e.g., “monkey-web” in my case, note: include the path where the endpoint is served - /api/messages for this example).

Update the managing endpoint

Save the changes, then perform a Test in Web Chat for the Azure Bot as shown below. You should get a reply from the bot (note: it may take a while for the first time).

Testing an Azure bot in web chat

Connect your bot to Twilio (SMS)

The Azure Bot should be up and running by now on the Azure. The Azure bot service is nice, and supports different channels for users to communicate with the bot. We are going to configure our bot with Twilio on the SMS channel, so our users will be able to chat with the bot via SMS.

Create and configure a Twilio phone number for Azure bot

To do that, create a TwiML application from the Phone Numbers - Manage menu of the Twilio Console, shown in the below capture. Enter the Request URL of the Messaging Configuration section with https://sms.botframework.com/api/sms for global bot, or https://europe.sms.botframework.com/api/sms for regional bot. I have deployed my bot as global bot, so below is the screen capture of my configuration. (note: You may refer to Connect a bot to Twilio (SMS) for more details)

Twilio Console - TwiML App

Config a Twilio Number with the TwiML App for the Messaging.

Configure Message Service with TwiML App

Gather credentials from Twilio console (these are needed in the Azure portal configuration).

Get your credentials from the Twilio Console

Configure Azure Bot for Twilio SMS

From Azure portal, select the Azure Bot (“monkey-kong-bot” in this example), select “Twilio SMS” from the channels setting to configure the bot with Twilio SMS. Enter Twilio credentials in the Azure Bot.

Enter Twilio credentials inside Azure

The Azure Bot is now ready for interaction with the Twilio Phone Number that was configured. Send a SMS to the Twilio Phone Number to test, you should get a respond from the Azure Bot!

Azure Bot’s Channels Status

You can verify all the channels status for the Azure Bot as shown in the screenshot below.

Azure Bot Channel Status

Connecting the Azure Bot platform with Twilio SMS

Congratulations! You've now configured the Azure Bot platform with Twilio SMS, and you can interact with the bot by texting it from your cell phone. Now that you've built the integration, see what else you can do with SMS on the Twilio Blog.

KK Gan is a Developer Evangelist at Twilio. He’ve been involved in developing and implementing business process automation, communication and collaboration solutions for different clients in the past. He loves and enjoyed sharing and learning with the developer communities. He can be reached at linkedin.com/in/kkgan or kogan [at] twilio.com.