Saturday, 25 April 2020

Integrate Salesforce with Telegram to Engage with Customers

As a business, it is very important to connect with customers in the way they like. Customer can prefer Email, Phone, or their favorite messaging application.

While there are countless messaging applications in the market, one of the popular messaging application is Telegram. And as a developer, what particularly I like about the app it there easy to use API with minimal configurations needed.

So today, we will be implementing a solution to allow the Sales and Marketing team to push messages to Customers and also how the Salesforce administrator can create Automation to send updates to Customer Inbox.

1. First, we need to set up a Telegram bot, which will be the front face of our business to the customer.
Registering a Bot is way easy then you might think via another bot named “BotFather”. Navigate to the bot using this link and create a bot using /newbot command. You will instantly get the Bot URL and API key needed.

2. Now lets setup the Salesforce, so we can connect with customers through the bot. When a customer navigates to our bot and selects /start command, we get a new message event with the Chat ID. We need to capture this ID in Salesforce so we can use it later to send messages just like we store Email/phone.
For this, we need to set up a webhook listener, to capture user events and create/update Contact with the Telegram User ID/ Chat ID (Same in one to one conversion).

This Webhook service will be now updating the contact if found with the User telegram ID or will create a new contact when a new message is received. 
For this to work, we need to register this webhook for our bot. This can be done by a GET request to the below URL.

Now send a message to your bot, (in our case and a new contact should be created in Salesforce.

Now that, we have Telegram ID of the customers in the Telegram_User_ID__c field on Contact. We can send them messages. But now we need to provide a way to the Sales and Marketing team to send messages and Salesforce administrators to create an automation process to send messages.

Sales and Marketing team solution

For them, we will build a custom Lightning component and place it on the Contact Record page, though this Sales and Marketing team can send messages directly without any efforts.

Salesforce administrator (Automated solution)

Every organization have various automation process to keep the customer updated about the events happening related to the customer, for example, an e-commerce business might have automation to send a notification to customer when their Order is shipped or Banking business might have a notification for Debit/credit transactions. 
For the automation, we will be using the Lightning Process builder with an Invocable method to process and send messages over Telegram.
So let’s first get the Invocable class ready. You can get the code from the GIT repo mentioned at the end of the article.

Now we can proceed to create a Process builder according to the business requirement.
In our case, we will send a message notification to the customer when their Email Address is updated.

Now let’s update the email and test!

Using the Process builder, we can now create various custom notifications on events without any hassle.

Other points:
  1. You need to add Telegram API to Remote Site settings in Salesforce. From Setup, enter Remote Site Settings in the Quick Find box, then select Remote Site Settings and add
  2. You need to create a custom label with API Name Telegram_Bot_API_key with your telegram bot API key.
  3. Create a field Telegram_User_ID__c (Number — 18,0) on Contact.

You can find the code in the git repo below. Please note the code is for demonstration purposes. Error handling is not implemented properly.

For more description of the Bot API, see this page:

1 comment: