Using Salesforce to send text messages using the Telegram Bot API

Sometimes sending messages through Telegram can be better than receiving emails. I’ve had some friends saying that they used Telegram’s Bot API to notify them about services going down, server crashes, and whatnot. So I’m thinking… why not have something on the Salesforce platform too?

Sending messages with attachments (documents/photos) is a pain in the butt using the platform, so I’ll cover just plain text messages.

Telegram’s API requires you to pass your bot’s token and the chat in which you want to send the message. Getting the ids is ok if you know what you are doing, but from the end-user’s perspective, this is awful. To get my user id I had to talk to a bot (@get_id_bot) that replies you with your id. :)

This simple class can be called to send a message to a specific Telegram user.

At line 8 you can see that the bot’s token will be on the URL. I didn’t hard-code it because the bot creator can change the token, so it is easier to change in a custom setting than editing the class, right?

Likewise, we can send the message to different users (using chat_id) so this will be dynamic as well.

With this setup you can run on the console the command Telegram.sendMessage('Hello world!', '<userid>'); and you should receive your message in a couple of seconds.

Telegram.sendMessage('Hello Renato!!', ‘<myuserid>'); produces this:

Sending Files

I did some research about sending files using Telegram Bot API, and I was a little disappointed. Telegram uses multipart/form-data to send files, and it doesn’t appear to be an easy thing to do. I did find an example of file upload, but not for Telegram specifically, so this will be left as homework for now…

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.