Send & receive SMS
Membrane includes an sms
program (pre-installed in your workspace) for sending and receiving text messages programmatically.
Configure
To configure your sms
program, find it in the Navigator, click the configure
action, type in your phone number, and hit “Invoke”.
Send
To send yourself a text manually, click on the sms
program in the Navigator, click the send
action, type in a message
, and hit “Invoke”. Check your phone! (And Membrane Logs 👀).
To send yourself a text programmatically, add the sms
program as a connection to any program and call:
nodes.sms.send({ message: "whatever" });
Receive
Try responding to the text you sent yourself above, and check the sms
Logs for your text.
When sms
receives a text, it fires a received
event that other Membrane programs can subscribe to. We created an sms-subscriber program as a minimal example of how to do this.
import { state, nodes, root } from "membrane";
state.messages ??= [];
/**
* This action subscribes to the `sms.received` event.
* Invoke it to set up the subscription.
*/
export async function listenSms() {
await nodes.sms.received.$subscribe(root.readSms);
}
/**
* This action is the handler for `sms.received` events.
* When you text Membrane, you'll see the `received` event in Logs.
*/
export async function readSms(_, { event }) {
const received = event.message;
state.messages.push(received);
// TODO: do something cool!
}