Data source: Telegram


1Presentation

Telegram allows you to create communication “spaces” to which notifications can be sent.

If you have correctly configured the Telegram connection parameters, Temma automatically creates an object of type \Temma\Datasources\Telegram, which you can use to send messages. By convention, we will assume that you named this connection telegram in the etc/temma.php file (see the configuration documentation).

In controllers, the connection to Telegram is then available by writing:

$tg = $this->telegram;

In other objects managed by the dependency injection component, the connection to Telegram is accessible by writing:

$tg = $loader->dataSources->telegram;
$tg = $loader->dataSources['telegram'];

2Configuration

To send messages on Telegram, you must first create a bot. To do this, search for the user BotFather, then send the message /newbot. Answer its questions and you will receive an API token.

In the etc/temma.php file (see the configuration documentation), you can then declare the DSN (Data Source Name) used to connect to Telegram.

The Telegram connection DSN is written as follows: telegram://API_TOKEN

To send messages, you will need a chat identifier corresponding to the conversation or channel where you want the messages to be sent:

  • For a private conversation, send a message to your bot, then run the following command:
    curl -s https://api.telegram.org/bot<TOKEN>/getUpdates
    (replacing <TOKEN> with the token you received)
    In the returned JSON stream, look for a section like "chat": {"id": 123456789} to retrieve the chat identifier.
  • For a group conversation, add the bot to the group, then run the following command:
    curl -s https://api.telegram.org/bot<TOKEN>/getUpdates
    (replacing <TOKEN> with the token you received)
    In the returned JSON stream, look for a section like "chat": {"id": -123456789} (negative number) to retrieve the chat identifier.
  • To send messages in a channel, add the bot to the channel as an administrator. The chat identifier is the name of the channel (in the form @my_channel).

3Notification content

Notifications can contain plain text or simplified HTML code.


4Unified calls

4.1Array-like access

// send a simple message
$tg['123456789'] = "Message to send";

// message with HTML
$tg['@my_channel'] = 'Text with <i>simple</i> <b>formatting</b>.';

4.2Advanced method

// send a simple message
$tg->set('@my_channel', "Message to send");

// rich notification
$tg->set('123456789', 'Text with <i>simple</i> <b>formatting</b>.');