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.

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.

Telegram module settings — 5 settings, 2 required
SettingEnvironment variableRequiredWhat it does
BotToken
Bot 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.
ChatID
Chat 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

  1. 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.
  2. Install the modulePass telegramalerts.New to gocommerce.New, as below, or start the reference binary with -telegram, which installs it idle.
  3. 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.
  4. 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

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.

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.

Chat on WhatsApp