Messaging
Telegram order alerts in Go
A Go package that writes to the store’s owner, not its shoppers, on Telegram. The owner makes a bot with @BotFather, sends it a message, picks that chat under Notifications › Setup Telegram, and every new order, payment, cancellation and return arrives there as it happens. No provider account, and no price per message.
- Messaging module
- 5 settings
- 23 tests
- 10 API operations
- ext/telegram-alerts
What it does
In the package’s words it is not a notifier: the engine’s notifiers write to shoppers, on a channel the shopper gave, and this writes to the owner about the shop. It subscribes to four events, order.created, order.paid, order.cancelled and order.returned, and words each one as a short plain-text message: the order number, the total, the items, the customer’s first name, the payment, and a link to the order in the panel when the panel’s address is set.
Each alert can be switched off or reworded on the Setup Telegram screen, with a live preview. The wording is a template over the same flat order data the engine’s own templates read, so {{.order_number}} means one thing everywhere, and only the alerts the owner changed are stored, so a default improved in a release reaches every store that never touched it. Wording that would not render is refused as it is saved.
The sending follows the webhooks module’s shape. The subscriber only writes a row and returns, so a Telegram that takes ten seconds to answer never holds the engine’s outbox, and a refusal is recorded against the alert rather than returned to the outbox, which would re-run every other consumer of the event. A worker sends, with backoff and a dead state.
Configuration
Two settings, both normally typed on the Notifications › Setup Telegram screen: the bot token and the chat. Config can carry them for a store wired in its own main(), and the screen’s values win. The reference binary reads neither from the environment, because a bot and a chat belong to one owner.
| Setting | Environment variable | Required | What it does |
|---|---|---|---|
BotTokenBot token | — | Yes | From @BotFather in Telegram: send it /newbot and it answers with a token like 123456789:AAE…. Checked for that shape before use. It travels in the Bot API’s URL path, so no error that could quote it is logged, stored or answered as it stands. |
ChatIDChat ID | — | Yes | Where the alerts go: the owner’s own chat with the bot, or a group it is in. Find my chat lists the chats that have written to the bot lately, or the ID can be typed. With a token and a chat in Config the plugin starts switched on. |
Timeout | — | No | Bounds one call to Telegram: 10 seconds when zero. Go only; not in the panel. |
BaseURL | — | No | Replaces https://api.telegram.org. The package says tests set it and nothing else should. Go only. |
Client | — | No | Replaces the HTTP client. Go only; not in the panel. |
The module reads its Config struct, not the environment. The variable names are the ones the package’s own example or the reference binary uses; in your own main() you choose where each value comes from. Where a setting has a panel label, the admin’s settings drawer can hold it too, and a value typed there wins over Config.
Setting it up
- Make a botIn Telegram, send /newbot to @BotFather and keep the token it answers with. Then send your new bot a message, or add it to the group the alerts should go to.
- Install the modulePass telegramalerts.New to gocommerce.New, as below, or start the reference binary with -telegram, which installs it idle.
- Set it upUnder Notifications › Setup Telegram, save the token, press Find my chat and pick yours, then Send test message. The test works before the alerts are switched on.
- Switch the alerts onTurn on Send alerts. Reword or switch off any of the four alerts there, and watch Recent alerts for what arrived and what Telegram answered.
main.go
import (
"github.com/itswadesh/gocommerce/core"
telegramalerts "github.com/itswadesh/gocommerce/ext/telegram-alerts"
)
app, err := gocommerce.New(cfg, telegramalerts.New(telegramalerts.Config{})) The package doc’s own example, with its imports; cfg is your gocommerce.Config. With an empty Config the module installs idle and each store sets its own bot and chat on the Setup Telegram screen, which is how the reference binary’s -telegram flag and examples/store/main.go install it. Import path github.com/itswadesh/gocommerce/ext/telegram-alerts.
How it works
-
Queued, then sent
Each event becomes one row holding the message as it was worded when the event happened. A worker claims 20 at a time with FOR UPDATE SKIP LOCKED every five seconds, and sooner when an alert has just been queued.
-
One message per event
A unique key on the event id means the engine redelivering an event, which its outbox may do, never becomes a second message on the owner’s phone.
-
Retries with backoff
When Telegram cannot be reached, is busy or asks to slow down, the alert waits and goes again, the wait doubling up to 15 minutes and honouring Telegram’s retry_after, for 12 attempts. Then it is marked dead and kept.
-
A refusal stops at once
An unknown chat, a blocked bot or a revoked token is a 4xx Telegram will give again, so the alert is marked dead with the reason in words the owner can act on, and can be sent again from Recent alerts once the setting is fixed.
-
Plain text, no previews
Messages go as plain text, so a customer’s name with an underscore in it cannot break the formatting, with link previews off. A message is cut at 3,500 characters, under Telegram’s limit.
-
Money as a person reads it
The total is written with the currency’s sign and decimals, and a rupee amount is grouped in lakhs and crores: ₹1,29,900.00, not 12990000 paise.
What it does not do
Read these before an order depends on it. No store is known to run GoCommerce in production yet, so these come from the code, not from anyone’s experience.
- Four eventsNew order, payment received, order cancelled and return received. Shipping, delivery and refunds send no alert.
- One chat per storeEvery alert goes to the one chat the store chose. There is no routing of some events to one person and others to another.
- Nothing queued while offAn event that happens while the alerts are switched off is not queued for later. Alerts already queued when they are switched off wait for the switch rather than being thrown away.
- Find my chat needs no webhookThe chat list comes from the Bot API’s getUpdates, which Telegram refuses for a bot that has a webhook set. Such a bot’s chat ID is typed by hand.
- The order link needs the panel’s addressThe link into the panel is built from the install’s panel URL, GOCOMMERCE_PANEL_URL. Without it the alert carries no link rather than a guessed one.
- The log holds no textRecent alerts shows each alert’s event, state, attempts and Telegram’s last answer, not the message, because it carries a customer’s name.
FAQ
Questions about the Telegram module
Does it cost anything?
The package doc puts it this way: a bot writing to its owner costs nothing, with no account with a provider, no wording registered with a carrier and no price per message. The bot is the owner’s own, made with @BotFather.
Can it write to a group?
Yes. Add the bot to the group, send a message there, and pick the group with Find my chat. If the group becomes a supergroup, Telegram moves it to a new chat ID, and the alert’s reason says to choose it again.
Who can change the alerts?
The module adds two rights. alerts.read, which managers have by default, sees the alerts, their wording and whether they arrived. alerts.write, which only the owner has until it is granted, rewords them, finds the chat, sends a test and retries one that failed.
Does it message shoppers?
No. It writes only to the chat the store chose. What shoppers are sent is the engine’s notifiers’ job, under Notifications.
Is Telegram a partner?
No. The module calls Telegram’s public Bot API with a token you supply. Telegram does not endorse GoCommerce, and no store is known to run this module in production yet.
Source
Everything on this page is read from ext/telegram-alerts in the GoCommerce repository, MIT licensed. When this page and the code disagree, the code is right and this page is out of date.Telegram is a trademark of its owner; this module talks to its public API and implies no endorsement. See trademarks.
Try it against a store of your own
The backend + admin quick start runs GoCommerce’s API and admin on your own machine, without a storefront. Add this module to it and try it on test orders before a real one depends on it.