onMessage

View as MarkdownOpen in Claude

Register the inbound SMS/MMS message handler. The provided function is called for each messaging.receive event on the subscribed contexts. The function receives a Message object with message properties and state tracking.

Only one message handler can be active at a time. Calling onMessage() again replaces the previous handler.

Parameters

handler
MessageHandlerRequired

An async function that receives a Message object. Signature: (message: Message) => void | Promise<void>.

Returns

void

Example

1import { RelayClient } from '@signalwire/sdk';
2
3const client = new RelayClient({
4 project: process.env.SIGNALWIRE_PROJECT_ID!,
5 token: process.env.SIGNALWIRE_TOKEN!,
6 contexts: ['default']
7});
8
9client.onMessage(async (message) => {
10 console.log(`SMS from ${message.fromNumber}: ${message.body}`);
11});
12
13await client.run();