For arlanix/notification 1.0.1
Telegram, Slack and Discord
The modules arlanix/module-notification-telegram, arlanix/module-notification-slack and arlanix/module-notification-discord add Telegram, Slack and Discord to a subscription's Delivered by field. Each module is optional.
Each module posts over HTTPS with Guzzle, which magento/framework already requires. The store's server must be able to reach the following hosts:
| Protocol | Host |
|---|---|
| Telegram | api.telegram.org:443, or the Bot API server you set |
| Slack | hooks.slack.com:443 (hooks.slack-gov.com for GovSlack) |
| Discord | discord.com:443 |
If the store cannot reach a host, every delivery fails with a message such as No answer from api.telegram.org: …. The message names the host, but never the full URL, because the URL contains the token or the webhook.
Telegram
You set the store's bot in Stores > Configuration > Arlanix > Notifications > Telegram, in the default scope. To create a bot with @BotFather, see the manual.
The group has the following settings:
| Path | Admin field | Default | Notes |
|---|---|---|---|
arlanix_notification/telegram/bot_token |
Bot token | Stored encrypted. Marked as sensitive, so app:config:dump does not export it. |
|
arlanix_notification/telegram/api_url |
Bot API server | https://api.telegram.org |
Your own Bot API server (opens in a new tab), or a relay where Telegram is blocked. |
To set the token from the command line, run the following command:
bin/magento config:set arlanix_notification/telegram/bot_token '123456789:AAE…'
config:set stores the token encrypted, as the admin does. With --lock-env, it writes the token to app/etc/env.php, also encrypted. If you write the token into env.php by hand, encrypt it too, because the configuration decrypts the value when it reads it.
The Telegram module works as follows:
- A subscription's own bot. A subscription can post as a different bot. That bot's token is a secret of the subscription. It is stored in
arlanix_notification_subscription.secrets, encrypted with the store's key, and it is never sent back to the form. If you switch the subscription back to the store's bot, the token is deleted when you save. - Find chats. This button calls
getUpdatesand does not confirm the updates. So any other tool that reads the bot's updates still gets them. If the bot has a webhook set, Telegram answers Conflict. In that case, type the chat id by hand. - Messages. The module sends each message with
sendMessagein HTML, with link previews turned off. A message is cut to 4,000 characters. A digest that is too long shows the messages that fit and ends with … and N more: see the Message Log. When Telegram refuses a message, the delivery stores Telegram's own words and what to do. If Telegram returnsmigrate_to_chat_id, the message gives the group's new chat id.
Slack
Slack needs no setup in the configuration. Each subscription has its own incoming webhook. To create one, see the manual.
The Slack module works as follows:
- The webhook is a secret of the subscription. It is stored encrypted in
arlanix_notification_subscription.secrets. It is never sent back to the form, and it is never shown in a grid or the log. The subscription's address is the SHA-256 hash of the webhook. A topic accepts one subscription per address, so the same webhook cannot be added to a topic twice. - Only webhooks that start with
https://hooks.slack.com/services/(or the GovSlack equivalent) are accepted. A workflow trigger URL expects a different payload, so it is not accepted. - Messages. The module posts
{"text": …}in Slack's mrkdwn format, with link unfurling turned off. A message is cut to 12,000 characters. A long digest is cut in the same way as in Telegram. Slack answers a webhook in plain words, such asno_service. The delivery stores this answer as it is, together with what to do.
Discord
Discord also needs no setup in the configuration. Each subscription has its channel's webhook. To create one, see the manual.
The Discord module works as follows:
- The webhook is a secret of the subscription and is stored in the same way as a Slack webhook.
- The grid shows the subscription's Channel. If no channel is entered, the grid shows the webhook's id. The id is the number in the webhook URL. The id is not secret, but the token after it is. A Slack subscription without a channel also shows its webhook's id.
- The module accepts webhooks in the form
https://discord.com/api/webhooks/<id>/<token>. It also acceptsdiscordapp.com, theptb.andcanary.hosts, and URLs with or without an API version. - Messages. The module posts each message as one embed. The subject is the embed's title, cut to 256 characters. The body is its description, cut to 4,000 characters. A long digest is cut in the same way as in Telegram. The severity sets the embed's colour.
allowed_mentionsis empty, so@everyonein a message does not notify anyone. - Discord answers
204on success. When Discord refuses a message, the delivery stores themessagefield of Discord's JSON answer, together with what to do.
When a service asks to wait
Telegram, Slack and Discord can answer 429 when too many messages are sent at once. The delivery then waits for the time the service asks for:
- Telegram:
parameters.retry_afterin the answer. - Slack: the
Retry-Afterheader. - Discord: the
Retry-Afterheader, orretry_afterin the JSON answer.
The wait is at most one hour. If the service does not say how long to wait, the delivery waits one minute.
During the first day after the message was published, a wait does not use up one of the delivery's attempts. After the first day, each wait uses up one attempt. So if a service keeps asking to wait, the delivery fails in the end.
Check them
To check any of the three, use Send test message on a subscription. It shows what Telegram, Slack or Discord answered.
To check from the command line, publish to a topic that has such a subscription, and run the consumer:
bin/magento arlanix:notification:publish "Store health" "Telegram check" --severity=info
bin/magento queue:consumers:start arlanix_notification_deliver --max-messages=10