Integrating Telegram API and Twilio API using MuleSoft: A Comprehensive Guide

Rohan Deshmukh
6 min readAug 3, 2023

--

Introduction:

  • Integrating APIs enables interconnected applications to leverage the strengths of different services, making them more powerful and versatile.
  • Here we will be exploring how Telegram and Twilio APIs can be used for Two-Factor Authorization(2FA).

Why Telegram API and Twilio API?

  • Integrating MuleSoft with Telegram and Twilio empowers you to leverage messaging capabilities within your business processes, enhancing communication, engagement, and automation.
  • Here are some key scopes where this integration can be used.
  1. Notification and Alerts
  2. Two-Factor Authentication (2FA)
  3. Appointment Reminders
  4. Broadcasting Information
  5. E-commerce Order Updates

Prerequisites:

  1. Telegram Bot Account
  2. Twilio Account
  3. MuleSoft Anypoint Platform account
  4. Anypoint Studio, Postman.

Step-by-Step Integration Guide:

1. Setting Up Telegram Bot and testing api endpoint::

Please refer to this page and follow the steps to create a bot and get the API token.

  1. Once you create your bot, now create a group and give a unique name to it. example- telemule
  2. Add your newly created bot to the new group and give admin access to the bot. Botname- MaxTheMuleBot

3. Now you can test the endpoint using Postman. You can refer to this page for various API methods offered by Telegram. I am using sendMessage method. You can modify the message by changing text queryParam value.

curl --location --request GET 'https://api.telegram.org/bot<token>/sendMessage?chat_id=@telemule&text=Hello'

Here provide the <token> given by BotFather.

You will receive the below response.

{
"ok": true,
"result": {
"message_id": 112,
"from": {
"id": "id",
"is_bot": true,
"first_name": "MaxTheMuleBot",
"username": "rmule_bot"
},
"chat": {
"id": "id",
"title": "TeleMule",
"username": "telemule",
"type": "supergroup"
},
"date": 1691049701,
"text": "Hello"
}
}

2. Configuring Twilio Account and testing api endpoint:

  1. Create a Twilio Account: If you haven’t already, sign up for a Twilio account at their website.
  2. Get Account SID and Auth Token: After creating an account, you’ll find your Account SID and Auth Token in the Twilio Console. Follow this link to get your Twilio Number and SID.
  3. Now you have to add the contact numbers to which you want to send the SMS. Please refer to this page.
  4. You have to generate the token. Use the token in the password after importing the below cURL. Page link.
  5. Now you can test the endpoint from Postman. Page Link. Enter the contact number to who you want to send the messages.
curl 'https://api.twilio.com/2010-04-01/Accounts/ACbee6d0103c2fdac7728756d318d696f0/Messages.json' -X POST \
--data-urlencode 'To=+91****6030' \
--data-urlencode 'From=+1364***7047' \
--data-urlencode 'Body=Hello' \
-u ACbee6d0103c2fdac7728756d318d696f0:[AuthToken]

Provide the token in place of [AuthToken]

You will receive the SMS on your mobile number.

3. Creating MuleSoft Application:

  1. Create a ‘New Project’ in Studio and create the following flow: Configure the Listener port number 8082 and path as /verify
Flow Diagram-

Note: Please refer to the component number highlighted in the box.

2. For component 1 please provide variable value as follows. It will generate a random 4-digit number which will be used by Twilio to send to the user.

3. In component 2 configure the telegram requestor which will send the message to the Telegram Group named @telemule Please enter the code received from Twilio, for contact number verification.

Here ${telegram.BotToken} will be

bot<token>

4. In component 3 we are setting the payload for Twilio Requestor as follows. Please provide the correct contact numbers for the key From and To.

%dw 2.0
output application/x-www-form-urlencoded
---
{
"From": "+136*****047",
"Body": vars.code,
"To": "+9198****842"
}

5. In component 4 we are configuring the Twilio requestor.

Provide headers in the general configuration.

To see the headers check for hidden headers which are autogenerated from Basic Auth.

6. In component 5 we are capturing the payload after successfully sending the message to the contact number.

6. In component 6 we are using flow ref to call flow -twillio-TelegramSub_Flow

7. In component 7 we are using the Wait function. It will stop the flow for 15 seconds till the user enters the code received on SMS as a reply to the bot in the group. You can increase wait time as per requirement.

%dw 2.0
import * from dw::Runtime
output application/json
---
{ "user" : 1 } wait 15000

8. In component 8 we are configuring the telegram requestor which will get the updates from the Telegram Group named @telemule.

9. Now we are checking if the entered code is correct using the choice condition. When expression for correct code would be. fx should be enabled.

payload.result[-1].message.text as Number == vars.code

10. In component 9, the bot will send the message to the group as the Pin code verified successfully!

11. In component 10, the bot will send the message as verification failed!

12. Now deploy the application and trigger the request from Postman.

https://localhost:8082/verify

13. In case you get the error “pkix-path-building-failed-sun-security-provider-certpath” Please refer to this page to resolve it.

14. You will receive the bot message to enter the SMS code received from Twilio for Two Factor Authentication (2FA). Once you enter the code you will see the results for (2FA).

Conclusion:

This can be useful in scenarios such as account creation, user registration, or implementing two-factor authentication (2FA) processes. Bots can interact with users and can use various verification methods to confirm the authenticity of the provided numbers.

Additional resource:

  1. Dataweave wait functions. — Link
  2. Anypoint Studio. Link

Author Bio:

I, Rohan Deshmukh, am a Certified Mulesoft Developer and Mulesoft community Mentor with a passion for API integrations. I am currently working at Infosys Ltd.

Thank you for reading this article! I’d love to hear your thoughts and feedback on this article.

Note: Please feel free to reach out to me email: deshmukhrr1999@gmail.com . You can connect with me on LinkedIn

Sabrina Hockett MuleSoft Training MuleSoft Telegram Messenger Twilio

--

--