Skip to main content

MessageContract

An object representing an SMS or MMS message.

Properties

body
stringrequired

Body of the message.

context
stringrequired

The topic the message-object is associated with. Topic was previously called context.

direction
stringrequired

The direction of the message: inbound or outbound.

from
stringrequired

The phone number the message comes from.

id
stringrequired

The unique identifier of the message.

media
string[]required

Array of media URLs.

reason
string

Reason why the message was not sent. This is present only in case of failure.

segments
numberrequired

Number of segments of the message.

state
MessagingMessageStaterequired

The current state of the message. See MessagingMessageState.

tags
string[]required

Array of strings with message tags.

to
stringrequired

The destination number of the message.

Type Aliases

MessagingMessageState

Ƭ MessagingMessageState: "queued" | "initiated" | "sent" | "delivered" | "undelivered" | "failed"

The state a message can be in.

  • queued: The message has been queued in Relay.
  • initiated: Relay has initiated the process of sending the message.
  • sent: Relay has sent the message.
  • delivered: The message has been successfully delivered. Due to the nature of SMS and MMS, receiving a delivered event is not guaranteed, even if the message is delivered successfully.
  • undelivered: The message has not been delivered. Due to the nature of SMS and MMS, receiving a undelivered event is not guaranteed, even if the message fails to be delivered.
  • failed: The request has failed.

Example

Listening for incoming and outgoing messages:

import { SignalWire } from "@signalwire/realtime-api";

const client = await SignalWire({ project: "your-project-id", token: "your-api-token" });

await client.messaging.listen({
topics: ["my-topic"],
onMessageReceived: (message) => {
console.log("Message ID:", message.id);
console.log("From:", message.from);
console.log("To:", message.to);
console.log("Body:", message.body);
console.log("Direction:", message.direction);
console.log("State:", message.state);
console.log("Segments:", message.segments);

if (message.media.length > 0) {
console.log("Media URLs:", message.media);
}
},
onMessageUpdated: (message) => {
console.log("Message updated:", message.id);
console.log("New state:", message.state);

if (message.state === "failed" && message.reason) {
console.log("Failed reason:", message.reason);
}
}
});