Messaging
Twilio SMS for GoCommerce
A Go package that texts shoppers about their orders through Twilio’s REST API. The words are the store’s own — the engine’s SMS templates, edited in the admin — so nothing has to be registered with a carrier before a message goes out.
- Messaging module
- 6 settings
- 6 tests
- ext/notify-twilio
What it does
The module registers a notifier on the engine’s SMS channel. When an order carrying a phone number is placed, shipped, delivered or cancelled, the engine hands it the event and its data; the module renders the engine’s SMS template for that event and posts it to Twilio’s Messages API.
The package calls it the plain-text counterpart to the MSG91 module. MSG91 sends DLT flow templates, because India requires SMS wording to be registered with the carrier first; Twilio sends free text, so an operator can change the words on the Setup SMS screen without asking anybody for approval. That, the package says, is the whole difference between the two.
A message is addressed either from a Twilio number the store owns or through a messaging service that owns several. Twilio rejects a request carrying both, so the service wins when it is set. No SDK: Twilio’s API is form posts with basic auth, which Go’s standard library does without help.
Configuration
The account SID and auth token are required, and so is one way to address a message: a from number or a messaging service. Set them in Config from your own main(), or leave Config empty and fill them in under Notifications › Setup SMS.
| Setting | Environment variable | Required | What it does |
|---|---|---|---|
AccountSIDAccount SID | TWILIO_ACCOUNT_SID | Yes | The AC… identifier from the Twilio console. |
AuthTokenAuth token | TWILIO_AUTH_TOKEN | Yes | The account’s auth token, or an API key secret. Sent with the SID as HTTP basic auth. |
FromFrom number | TWILIO_FROM | No | A Twilio number the store owns, in international form: +15550000000. This or a messaging service is required. |
MessagingServiceSIDMessaging service SID | TWILIO_MESSAGING_SERVICE_SID | No | The MG… identifier of a messaging service, used instead of the from number and winning over it when both are set. |
BaseURL | — | No | Overrides https://api.twilio.com. Tests set it; in the code’s words, nothing else should. Go only. |
Client | — | No | Replaces the HTTP client, which otherwise times out after 15 seconds. Go only. |
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
- Get the credentialsFrom the Twilio console: the account SID, the auth token, and either a number the store owns or a messaging service.
- Install the moduleImport it and pass twilio.New to gocommerce.New, as below — or run the reference binary with -twilio, which reads the four TWILIO_ variables shown.
- Fill in the settingsIn Config — a SID and token there start it switched on — or under Notifications › Setup SMS. A value typed into the panel counts on the next message, with no restart.
- Edit the wordingThe texts for order placed, shipped, delivered and cancelled are the engine’s templates, edited on the Setup SMS screen.
main.go
import (
"os"
"github.com/itswadesh/gocommerce/core"
twilio "github.com/itswadesh/gocommerce/ext/notify-twilio"
)
app, err := gocommerce.New(cfg,
twilio.New(twilio.Config{
AccountSID: os.Getenv("TWILIO_ACCOUNT_SID"),
AuthToken: os.Getenv("TWILIO_AUTH_TOKEN"),
From: os.Getenv("TWILIO_FROM"),
MessagingServiceSID: os.Getenv("TWILIO_MESSAGING_SERVICE_SID"),
}),
) The package doc has no example of its own; this is how the reference binary’s -twilio flag constructs the module, with imports added. cfg is your gocommerce.Config. Import path github.com/itswadesh/gocommerce/ext/notify-twilio.
How it works
-
The engine’s words
The body is the engine’s SMS template for the event — the panel’s edit if there is one, the default otherwise — rendered with Go’s text/template. There are no wording overrides in Config.
-
Number or service, never both
When a messaging service is set, the request carries MessagingServiceSid and no From; otherwise it carries the from number. Twilio refuses a request with both.
-
Configured means it can send
An account with no from number and no messaging service does not count as configured, so the Notifications screen cannot show a working channel that silently drops every text.
-
Refusals settle, outages retry
A 429 or 5xx is returned as an error and the engine retries the notification with backoff. Any other refusal is logged with Twilio’s code and more_info link, and settled.
-
Every text written down
Sent, with Twilio’s message SID and status; or not attempted because the module is not configured, with the event and the recipient.
-
Empty texts are skipped
An event with no SMS template, or a template that renders to nothing, sends nothing and is not an error.
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 eventsThe engine has SMS wording for order placed, shipped, delivered and cancelled. Payment received and refunds have no SMS template, so they send no text.
- Numbers go as enteredThe shopper’s phone is sent to Twilio exactly as the order holds it. Twilio expects international form, so collect numbers with their country code.
- Refusals count as sentA text Twilio refuses returns no error, so the engine’s notification log records it as sent. The refusal is only in the application log.
- No delivery receiptsNo status callback is registered, so a text Twilio accepts and later fails to deliver is not reported back.
- Not the India moduleThe package’s own comparison: India requires SMS wording to be registered first, which is what the MSG91 module sends. This one sends free text.
FAQ
Questions about the Twilio module
Twilio or MSG91?
The package answers it: MSG91 sends pre-registered DLT templates, which India requires, and Twilio sends free text in the store’s own words. The reference binary’s comment calls MSG91 the right module in India and a week of waiting everywhere else.
Can I change what the texts say?
Yes, on the Setup SMS screen under Notifications. The wording is the engine’s SMS templates, rendered at send time; there is nothing to register with Twilio first.
Can I use it without writing Go?
Yes. The reference binary’s -twilio flag installs it, reading TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, TWILIO_FROM and TWILIO_MESSAGING_SERVICE_SID, and the panel can hold the same settings.
Is this a Twilio partnership?
No. The module calls Twilio’s public API with credentials you supply. Twilio 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/notify-twilio in the GoCommerce repository, MIT licensed. When this page and the code disagree, the code is right and this page is out of date.Twilio 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 one-command stack gives you GoCommerce’s API and admin on your own machine in minutes. Add this module to it and try it on test orders before a real one depends on it.