In this article, we will discuss how to integrate Telegram with Salesforce using webhook and public site, and also facilitate the process of communication with the client through automation tools and LWC component.
First of all, you need to create your own Telegram bot. To do that, enter “BotFather” in the search bar of the Telegram application.
Next, you should perform the following operations: /start → /newbot → “Input bot name” → “Input bot username (camelCase or snake_case)”.
Done! You will see the following message:
In this message, you can find the address of your bot and the token to interact with it.
Since the purpose of the application is to communicate with customers, you should set up the organization in Salesforce. The communication process will occur as follows: the client finds your Telegram bot and starts interacting with it via the /start command. Now you should get the chat ID to send messages to the client.
To do this, use a webhook listener that can intercept events that come from the user. For example, create (if this is a new user) or update (if the user has already been created) a record of the "Contact" object.
To do this, register this webhook using a GET request to the following URL - link:
{API_KEY} is the token you received from BotFather.
{REST_RESOURCE_URL} - the environment address where you want to use the webhook (in our example, this is a link to a public site).
Example {REST_RESOURCE_URL}:
With a successful result, you will get a web page with the following content:
Now let's move on to our Salesforce organization. First, we need to add Telegram API to Remote Site settings. You should go to Setup → Remote Site Settings → select Remote Site Settings → New Remote Site and add the address:
Next, you should save our Telegram_Bot_API_key (token) in Custom Settings. To do this, go to Setup → Custom Settings → New, enter the name and API name. Next, select: Setting type: List and Visibility: Public. After that, add a field with the Text type and enter the token there.
Since our example is based on interaction with the client, we need to customize the “Contact” object, adding the field Telegram_User_ID__c of type Number (18, 0). You need that to save the chat ID with each client.
Next, you will create an Apex class in order to create a new user and send him a message about successful registration.
Telegram integration with Salesforce: work with TelegramWebhook.cls
There are two methods in this class:
createUser() - to create a new record for the Contact object;
sendMessage() - method for sending a message to the user about registration or that the user is already registered.
An additional Apex class for sending a request to Telegram:
Telegram integration with Salesforce: work with TelegramUtility.cls
Now, when the users starts working with your Telegram bot, they will receive the following notification:
Done! We have connected the Telegram bot to the Salesforce organization.
The next step is to create a custom Lightning component and add it to the Contact Record Page in order to simplify communication with the client.
Telegram integration with Salesforce: work with SendMessageToTelegram.js
Telegram integration with Salesforce: work with SendMessageToTelegram.html
Telegram integration with Salesforce: work with SendMessageToTelegramController.cls
As a result, you will get the following component:
We can also use Automation Tools to build an automated process for sending a notification about a contact's email address change. To do this, use the Flow Builder, which will call the logic of sending a message to the user from the Apex class.
Telegram integration with Salesforce: work with TelegramInvocable.cls
After creating the Apex class, let's move on to creating the Flow. To do this, go to Setup → Flows → New Flow. Next, select the Record-Triggered Flow type and configure as shown in the figure below:
After that, let’s add one more element, and in the Interaction section choose Action. Configure as shown below.
For the “Message” variable, select New Resource → Formula and then enter the parameters as shown in the figure below:
After that, save Flow and activate it. If you try to change the email in a contact, you will receive the following notification in Telegram:
How exactly the whole process that we covered in this article works can be seen in the diagram below.
To sum up
So, in this article, we looked at telegram integration with Salesforce and how exactly you can connect Salesforce with Telegram using a Webhook, made your own LWC component to send a message from the organization to the messenger, and also used Flow Builder to create an automatic notification of the user in Telegram when the email address changes.
Need help with Salesforce integrations?
Be welcome to reach out and share your business ideas and requests. We are here to help you with all the Salesforce-related challenges.
i got this error no replay or log has been shown
error {"ok":true,"result":{"url":"https://nbsconsulting-9a-dev-ed.develop.my.salesforce.com/services/apexrest/telegramWebhook","has_custom_certificate":false,"pending_update_count":6,"last_error_date":1689849076,"last_error_message":"Wrong response from the webhook: 401 Unauthorized",}