In the previous article we saw a demo video of the 2-way integration between Salesforce and Telegram and a simple use case. In this article I will quickly show the configurations and Apex code to send messages from Salesforce to Telegram. In the next article we will discuss 2 different ways of achieving the other way round i.e processing messages from Telegram to Salesforce. So lets start!
Bot Creation in Telegram
- Open the Telegram App and Search for BotFather
- Enter command /start and /newbot
- After a series of straightforward question and answers you should be able a create a Bot and generate an access token. One such conversation is shown below:
Send Message to Telegram
Once the access token is generated, sending message from Salesforce is a simple Apex callout as shown below:
HttpRequest req = new HttpRequest(); req.setMethod('POST'); req.setHeader('Content-Type','application/x-www-form-urlencoded'); payload = 'text=' + message +'&chat_id='+receiver_chatid; req.setEndpoint('https://api.telegram.org/bot'+*access_token*+'/sendMessage');
- message is the text you want to send to the Telegram Contact
- receiver_chatid is numeric id (usually 7 digits) of the contact. In the use case we saw in the previous article we get this id when a Telegram user sends message to the Bot
Make sure you create a Remote Site Setting for the URL https://api.telegram.org
At inteygrate.com, I am trying out ways to integrate Salesforce to various other technologies.