Red Hat Training

A Red Hat training course is available for Red Hat Fuse

Chapter 21. Connecting to Telegram

As a business user, you can create an integration that connects to Telegram. A connection to Telegram can do either one of the following:

  • Trigger execution of a simple integration when a Telegram bot that you specify receives a message. The integration passes the message to the next step in the flow. For example, after a simple integration receives a message there might be a filter step that watches for keyword instances such as product names. Upon finding messages that contain those product names, the integration can notify the appropriate contact in a Gmail connection or in a different Telegram connection.
  • Deliver a message to a particular Telegram chat. For example, this behavior is useful when an integration downloads a file from an FTP server and processes it in some way. You can add a Telegram connection that notifies a Telegram chat that the process was successful.

To connect to Telegram in an integration, create a Telegram connection and then add the connection to an integration flow. Details are in the following topics:

21.1. Creating a Telegram connection

In an integration, a Telegram connection can receive messages that have been sent to a particular chat. A chat bot that you create forwards the messages to Fuse Online. A Telegram connection can also send messages to chats that the connection has access to. You can use the same Telegram connection:

  • In any number of integrations that use the connection to send messages to a chat.
  • In addition, you can use it in one integration that uses the connection to receive messages from a Telegram chat.

In other words, you cannot use the same Telegram connection in more than one integration if you want that connection to receive Telegram chat messages in both integrations. This is a Telegram limitation.

Prerequisites

  • In Telegram, you created a bot for the chat that you want to obtain messages from and send messages to. For help with this, see Bots, an introduction for developers.
  • You obtained a bot authorization token from the Telegram BotFather.

Procedure

  1. In Fuse Online, in the left panel, click Connections to display any available connections.
  2. In the upper right, click Create Connection to display Fuse Online connectors.
  3. Click the Telegram connector.
  4. In the Authorization Token field, enter the Telegram authorization token for the bot that you want to get messages from and send messages to.
  5. Click Next.
  6. In the Connection Name field, enter your choice of a name that helps you distinguish this connection from any other connections. For example, enter Telegram for Product Mentions.
  7. In the Description field, optionally enter any information that is helpful to know about this connection.
  8. In the upper right, click Create to see the updated list of available connections, including the connection that you just created. If you entered the example name, you would see that Telegram for Product Mentions appears as a connection that you can choose to add to an integration. You can add this connection to any number of integrations.

21.2. Adding a Telegram connection to trigger integration execution upon receiving messages

A Telegram connection that starts an integration triggers execution of the integration each time that the connection receives a message from a Telegram chat bot that you specify.

Prerequisites

  • You created a Telegram connection.
  • You are creating a simple integration and Fuse Online is prompting you to choose how you want to start the integration.

Procedure

  1. Click the Telegram connection that you want to use to start the integration.
  2. Click the Read Messages action to receive all messages that are sent to the chat bot.

    The connection configuration identifies the chat bot that you want to receive messages from. No other configuration is required.

21.3. Adding a Telegram connection to send a message to a Telegram chat

In an integration, you can send a message to a Telegram chat in the middle of a flow or to finish a simple integration. To do this, add a Telegram connection to the middle of a flow or as a simple integration’s finish connection.

Prerequisites

  • You created a Telegram connection.
  • You are creating or editing a flow and Fuse Online is prompting you to choose a step. Or, Fuse Online is prompting you to choose a finish connection.

Procedure

  1. Click the Telegram connection that you want to add to the flow.
  2. Click the Send a Text Message action.
  3. In the Chat Id field, enter the ID for the Telegram chat that you are sending a message to.

    If you leave this field blank, and if there is an earlier Telegram connection that receives messages, then Fuse Online uses the chat ID that is in an incoming message.

    To obtain the ID for a chat:

    1. In Telegram, add a bot to the chat that you want to send a message to. The BotFather response includes a bot access token.
    2. Copy the access token to the clipboard.
    3. In Telegram, send a message directly to that chat. Do not use the bot to send the message.
    4. Read messages that are sent to that chat by invoking a curl command in the following format. Replace bot-access-token with the access token in the clipboard.

      curl https://api.telegram.org/bot-access-token/getUpdates\?offset\=0

      The response is in JSON format. The JSON chat object contains the chat ID.

  4. Click Done to add the connection to the flow.