Skip to main content

HiveMind Twilio Bridge

This bridges Twilio SMS to a HiveMind node. A HiveMind bridge is a satellite whose input and output are a chat platform instead of a microphone: text messages sent to your Twilio number become HiveMind utterances, and the hub's spoken replies are sent back as SMS to the same phone number.

This bridge needs a real Twilio account and a real phone number. It has not been exercised against live Twilio traffic or a real HiveMind hub — only unit-tested with both sides mocked.

Getting a Twilio account and number

  1. Sign up at twilio.com. A trial account works for testing, with two limits worth knowing up front: trial messages carry a "Sent from a Twilio trial account" prefix, and you can only send SMS to phone numbers you have verified in the trial console. Neither limit applies once you upgrade to a paid account.
  2. In the Twilio console, copy your Account SID and Auth Token from the dashboard. Treat the Auth Token like a password.
  3. Buy (or claim the free trial) a phone number with SMS capability: console sidebar → Phone Numbers → Buy a number. Note the number in E.164 format, e.g. +15551234567.

Pointing Twilio at this bridge

This bridge exposes a webhook at /sms. Twilio needs to reach it over the public internet, so you need either a public server or a tunnel (e.g. ngrok http 8080 while testing locally).

  1. Run this bridge (see below) so it is listening, e.g. at https://your-public-host/sms.
  2. In the Twilio console: Phone Numbers → your number → Messaging configuration → "A MESSAGE COMES IN" → set to Webhook, method POST, URL https://your-public-host/sms. Save.

Twilio will now POST every inbound SMS to this bridge.

Registering the bridge on the hub

Every HiveMind client needs credentials and, separately, permission to send the message types it uses. On the machine running hivemind-core:

hivemind-core add-client

This prints an access key and password; pass them to the bridge as --access-key / --password (or store them once with hivemind-client set-identity and omit the flags).

A freshly added client is denied every message type by default. The bridge needs at least:

hivemind-core allow-msg recognizer_loop:utterance <client_id>
hivemind-core allow-msg speak <client_id>

<client_id> is printed by add-client (and by hivemind-core list-clients afterwards). Skipping this step is the single most common reason a bridge "connects fine" but nothing ever seems to happen: the hub silently drops every message the client sends until it is whitelisted.

Running the bridge

pip install .
hivemind-twilio-bridge \
  --twilio-account-sid <ACxxxx> \
  --twilio-auth-token <token> \
  --twilio-number +15551234567 \
  --public-url https://your-public-host \
  --access-key <key> --password <password> \
  --host ws://127.0.0.1 --port 5678

This starts a web server (default 0.0.0.0:8080, override with --web-host / --web-port) that Twilio's webhook needs to reach.

Useful flags:

  • --site-id: this bridge's HiveMind site id. If you run more than one bridge on the same host, give each a distinct site id — otherwise they collide over the same identity file and pinned peer keys.
  • --self-signed: accept a self-signed TLS certificate on wss:// hubs.
  • --lang: the language tag attached to forwarded utterances (default en-us).

Run hivemind-twilio-bridge --help for the full list.

Docker

docker build -t hivemind-twilio-bridge .
docker run --rm -p 8080:8080 \
  -e TWILIO_ACCOUNT_SID=... \
  -e TWILIO_AUTH_TOKEN=... \
  -e TWILIO_NUMBER=+15551234567 \
  -e PUBLIC_URL=https://your-public-host \
  -e HIVEMIND_ACCESS_KEY=... \
  -e HIVEMIND_PASSWORD=... \
  -e HIVEMIND_HOST=ws://hivemind-core \
  hivemind-twilio-bridge

or via docker-compose.yml — copy it, fill in the environment section, and docker compose up.

What this bridge does, precisely

  • Runs a FastAPI web server exposing POST /sms, the webhook Twilio calls on every inbound SMS with From (sender's E.164 number) and Body (message text) as form fields.
  • Connects to the HiveMind hub with hivemind_bus_client.HiveMessageBusClient.
  • Drops empty messages and anything received before the HiveMind handshake has completed — forwarding earlier would get the connection killed by the hub instead of just failing the one message.
  • Forwards each remaining message as a recognizer_loop:utterance bus message, carrying the sender's phone number in the message context so the hub's speak reply can be routed back to the right number.
  • Sends speak replies (and a fixed fallback line on hive.complete_intent_failure) back to the originating number via the Twilio REST API (twilio.rest.Client.messages.create).
  • Voice calls are not implemented. Twilio also supports a <Gather> speech webhook for inbound calls; this bridge only handles SMS. Adding a /voice webhook that transcribes speech via <Gather input="speech"> and replies with TwiML <Say> would follow the same shape as /sms but is out of scope here.

Testing

pip install -e .[test]
pytest tests/

The test suite mocks both the Twilio REST client and the HiveMind HiveMessageBusClient, so it runs without a live Twilio account or a live hub. It has not been exercised against real Twilio traffic or a real HiveMind hub — that needs an actual Twilio account and phone number, which this repository does not have.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hivemind_twilio_bridge-0.1.0.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hivemind_twilio_bridge-0.1.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file hivemind_twilio_bridge-0.1.0.tar.gz.

File metadata

  • Download URL: hivemind_twilio_bridge-0.1.0.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for hivemind_twilio_bridge-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9f6de8fcd96e7ba7dc570114dc4e93d74423f5e6634b74bf47c6248e80b22873
MD5 80c3a9f1e0fd3e5b6c4bcdb182a1843a
BLAKE2b-256 1ba928885e3c6cdd49298daf46aff38832d6d0da49d7b7692680f4d45c858ea9

See more details on using hashes here.

File details

Details for the file hivemind_twilio_bridge-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for hivemind_twilio_bridge-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a64475b9e9feaa257e25cc0172e211b7282acbfa5f801cca0170bb4d18911de3
MD5 3a4d179aae4aca3bd703deaf4a69d669
BLAKE2b-256 d7a716bfa2e8e076cb70b55d7ff6511e8c4d8b2f0bc35a06b13245ed654bb226

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page