Setup Twilio Integration on your server to connect Google Dialogflow
By: Pragnakalp Techlabs
Tags: Twilio, Chatbots, Bots, Chatbot Development, Dialogflow

Dialogflow has discontinued the one-click integration of Twilio for SMS and instead made the Twilio integration available as open source.

Here, we take you through the process of setting up Twilio integration on your own server to connect Twilio phone number with the Dialogflow agent.

Pre-requisites

You have created an account with billing enabled for the project on Dialogflow and an agent is ready.

You must have an account with Twilio, purchased a mobile number and credits send SMS messages.

We are using textnow.com to test our integration create if you don’t have or you can use any other service or your own mobile phone to send the SMS.

Installed nodejs on your system and basic knowledge of nodejs.

Ngrok is installed in your system.

Gcloud cli is installed on your system and your working project is selected.

Steps

You can download the integration code from the official github repo of Dialogflow https://github.com/GoogleCloudPlatform/dialogflow-integrations . But in that there is code for multiple platforms.

Trending Bot Articles:

Also see: Appointment Booking Chatbot Using Dialogflow With Google Calendar Integration

To simplify it, we have created our own zip file which contains code for twilio only and also we have setup the code to run the server easily.

You can download the zip file by clicking here.

Extract the zip file in the folder and change the working directory to the extracted folder.

2. Now, browse to the directory where you extracted the folder and run

npm install

This will install all the required libraries for the project.

3. Now, download the json file for the service account.

To do that, go to https://dialogflow.cloud.google.com/#/agent/.

Select your agent.

4. Click on the settings button.

5. Now copy the Project id and paste it in the index.js file as value of projectId. Then open service account page in new tab by clicking on the service account.

6. Now on the service account page

Check the project

Check the service account is the same as the previous page

Click on the 3 dots

On clicking on the “create key” you will get the JSON file.

Save the JSON file in the working directory where you have extracted the zip file.

NOTE: the json file and index.js must be in the same directory at the same level.

Now export the path of the json file. By typing in the terminal export GOOGLE_APPLICATION_CREDENTIALS=”/full/path/to/your/json/file”

NOTE: Don’t close the terminal we will start our nodejs server here.

7. Now, the next step is to login to https://www.twilio.com/console.

Copy SID and paste in index.js as value of accountSID.

Copy Authtoken and paste in index.js as value of authToken.

8. Now click on the three dots

9. Click on “programmable sms”.

10. Click on “SMS”.

Click on “+” sign.

11. Give service name and then select mixed from the dropdown.

12. Check the radio button “send an incoming message webhook”.

Keep the window open we will back here to paste the ngrok url in Request URL.

Open index.js file and paste all the required fields.

Now run the node server with node index.js in the terminal where you have exported the path earlier.

Open ngrok in new terminal and type sudo ngrok http 8989

You will get the ngrok URL for your server. Paste it in the Twilio as SEND AN INCOMING MESSAGE WEBHOOK.

Now, the connection of Twilio of Dialogflow is done using the integration we setup. You can test it by sending the SMS.`

Send a message from your mobile to the number purchased from Twilio. You will get a reply from Dialogflow agent.

STEPS TO DEPLOY THE APP ON HEROKU

Create your account in Heroku and login. Create an app on Heroku. Install git from https://git-scm.com/downloads Now initialize the working directory as git directory to do that use the following command

5. Now install Heroku CLI with

sudo snap install --classic heroku

6. Now run the below command and login with heroku via CLI.

heroku login

5. Now run following three commands one after one.

git add .

git commit -am "Initial commit"

git push heroku master

6. Set environment variable in Heroku

heroku config:set GOOGLE_APPLICATION_CREDENTIALS='YOUR_JSON_FILE_PATH'

Feel free to comment your doubts/questions. We would be glad to help you.

If you are looking for Chatbot Development or Natural Language Processing services then do contact us or send your requirement at letstalk@pragnakalp.com. We would be happy to offer our expert services.