Skip to main content

Plugins: Telegram

Receive event notifications directly in your Telegram group or send them to specific contacts individually.

Sergio M avatar
Written by Sergio M
Updated over 3 weeks ago

With the Telegram plugin you’ll be able to easily set up events in Ubidots that trigger messages in a Telegram group or even send them to specific contacts individually. This is possible thanks to an HTTPS URL provided by the plugin that receives messages and sends them using your own Telegram bot.

1. Creating a Telegram bot

For this plugin to work, we’ll first have to create a bot in Telegram. Follow these steps to do so:

  1. Open the Telegram app.

  2. Tap on the search icon in the upper right corner.

  3. Type "botfather" in the search bar and tap on the BotFather result to start a chat.

  4. In the chat with BotFather, type "/newbot" and send the message.

  5. BotFather will ask you to choose a name for your bot, pick one and send it as a message.

  6. Choose a unique username for your bot. This username must end with the word "bot" (e.g., "myexamplebot"). Send the username as a message.

  7. If the chosen username is available, BotFather will create the bot and provide you with a token. This token will be used as input for this plugin.

2. Setting the plugin up

Go to “Dev Center” → “Plugins”. Click on the “+” button in the upper right corner, choose the Telegram option, and follow these steps:

  1. Enter your bot’s token.

  2. Name your plugin.

  3. Fill in a description (optional).

3. Telegram group setup

Now it’s time to invite the bot to the group where we want to receive the messages from our plugin/event. Follow these steps:

(If you prefer to send messages directly to individual contacts rather than using a Telegram group, please skip this step and proceed to the second alternative in Step 4.)

  1. Open the Telegram app.

  2. Click on the hamburger button and then click on “New Group”. Give it a distinctive name (e.g., "Weather Alerts").

  3. Invite your bot to the group.

  4. Send an initial command-like message to the group (even if the command does nothing). Commands in Telegram start with “/”, so this initial message can be something like “/help”.

4. Creating an event that triggers the Telegram plugin

In this case, the type of action we have to use for our event is the “trigger webhook”. To set up this type of event, follow this guide and remember to introduce the following inputs accordingly:

  • Place the HTTPS endpoint URL of your plugin in the “URL” field during the “actions” configuration. You can locate this URL by going into your plugin and then heading into its “decoder” section.

Note: In the following alternatives, it’s better if you manually type all the double quotes in the payload to avoid any potential format issues.

Alternative 1: Telegram group

  • Change the default payload with this JSON:

    {"groupname": "<GROUPNAME>", "message": "<MESSAGE>"}

    Where:

    • <GROUPNAME> is the name of the target Telegram group. This input is case sensitive, so make sure to type/copy it exactly as it appears on Telegram.

    • <MESSAGE> is the content of the message to be sent.

Alternative 2: individual contacts

  • Change the default payload with this JSON:

    {"to": "<NUMBER(S)>", "message": "<MESSAGE>"}

    Where:

    • <NUMBER(S)> is the field where you need enter the phone number(s) you wish to alert. Please keep the following two considerations in mind:

      • Each number must include its respective country code (e.g., "+57 XXX", "+52 XXX").

      • You can send a single alert to multiple recipients by separating the numbers with a comma, like this: "+57 XXX, +52 XXXX". The message will be delivered individually to each number on the list.

    • <MESSAGE> is the content of the message to be sent.

Upon the first event trigger, each user will receive an SMS message similar to the one below. They must accept the notification service by clicking the link provided in the message:

Note: If the user doesn't click the link, they will continue to receive the same message with each trigger until it is accepted. Once confirmed, they will be enabled to receive standard alerts starting with the following event activation.

5. Link the new group with the bot in the Telegram plugin

To ensure the plugin correctly stores the chat ID linked to the bot, you must trigger the webhook when the bot is added to a new group. If this is not done within 24 hours of adding the bot, the plugin will be unable to send messages to this chat.

You can trigger the plugin's webhook using any software that allows you to send HTTP requests, such as Postman. Use the following request:

curl -X POST 'https://dataplugin.ubidots.com/api/web-hook/<plugin>' \
-H 'Content-Type: application/json' \
-d '{"groupname": "<GROUPNAME>", "message": "<MESSAGE>"}'

This also provides an opportunity to send an initial message introducing the bot to the group, and providing context for its purpose:

And this is what it would look like in the group:

6. Troubleshooting

You can check the status of the message delivery by reviewing the plugin’s logs. You may also test your bot by sending a direct HTTPS POST request to this plugin's URL using an HTTP tool such as Postman. This request must also have a payload as indicated earlier.

Did this answer your question?