Integrate Salesforce with Telegram using Webhook

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.

BotFather

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:

The message

In this message, you can find the address of your bot and the token to interact with it.

dont miss out iconDon't forget to check out: Salesforce and Microsoft Teams Integration | All You Need to Know

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:

https://api.telegram.org/bot{API_KEY}/setWebhook?url={REST_RESOURCE_URL}

{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}:

https://instance.salesforce.com/services/apexrest/{urlMapping}

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:

https://api.telegram.org

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.

TelegramWebhook.cls

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:

TelegramUtility.cls

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.

SendMessageToTelegram.js

SendMessageToTelegram.js

SendMessageToTelegram.html

SendMessageToTelegram.html

SendMessageToTelegramController.cls

SendMessageToTelegramController.cls

As a result, you will get the following component:

Place it on the Contact Record page. Now, if you try to send a message, the client will see it in the chat with the Telegram bot:

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.

TelegramInvocable.cls

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:

Configure Start

After that, let’s add one more element, and in the Interaction section choose Action. Configure as shown below.

New Action

For the “Message” variable, select New Resource → Formula and then enter the parameters as shown in the figure below:

New Resource

dont miss out iconCheck out another amazing blog by Sparkybit here: How to overcome Red Flags that indicate harm to your digital strategy?

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:

notification

How exactly the whole process that we covered in this article works can be seen in the diagram below.

the whole process

To Sum Up

So, in this article, we looked at 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.

Sparkybit, Salesforce consulting and development company. We make Salesforce perform at its best by fitting it to Clients’ custom business needs. 9 years of sharpened expertise, 25+ successful long-term projects globally.

[email protected]

Copyright belongs to Sparkybit GmbH, Im Dörener Feld 3, 33100, Paderborn, Germany.
2023 © Sparkybit. All rights reserved.

Responses

Popular Salesforce Blogs