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.

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.

Twilio module settings — 6 settings, 2 required
SettingEnvironment variableRequiredWhat it does
AccountSID
Account SID
TWILIO_ACCOUNT_SID Yes The AC… identifier from the Twilio console.
AuthToken
Auth token
TWILIO_AUTH_TOKEN Yes The account’s auth token, or an API key secret. Sent with the SID as HTTP basic auth.
From
From number
TWILIO_FROM No A Twilio number the store owns, in international form: +15550000000. This or a messaging service is required.
MessagingServiceSID
Messaging 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

  1. Get the credentialsFrom the Twilio console: the account SID, the auth token, and either a number the store owns or a messaging service.
  2. 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.
  3. 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.
  4. 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

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 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.

Chat on WhatsApp