Messaging.Client
You can use instances of this class to send or receive messages. Please see MessagingClientApiEvents for the full list of events you can subscribe to.
Example
const client = new Messaging.Client({
project: "<project-id>",
token: "<api-token>",
contexts: ['office']
})
client.on('message.received', (message) => {
console.log('message.received', message)
})
await client.send({
context: 'office',
from: '+1xxx',
to: '+1yyy',
body: 'Hello World!'
})
Constructors
• new Client(opts
)
Parameters
Name | Type | Description |
---|---|---|
opts | Object | - |
opts.contexts | string [] | SignalWire contexts, e.g. 'home', 'office'… |
opts.project | string | SignalWire Project ID, e.g. |
opts.token | string | SignalWire API token, e.g. |
Methods
▸ disconnect(): void
Disconnects this client. The client will stop receiving events and you will need to create a new instance if you want to use it again.
Returns
void
Example
client.disconnect()
▸ off<T
>(event
, fn?
): EmitterContract
<MessagingClientApiEvents
>
Type parameters
Name | Type |
---|---|
T | extends keyof MessagingClientApiEvents |
Parameters
Name | Type |
---|---|
event | T |
fn? | (...args : ArgumentMap <MessagingClientApiEvents >[Extract <T , keyof MessagingClientApiEvents >]) => void |
Returns
EmitterContract
<MessagingClientApiEvents
>
▸ on<T
>(event
, fn
): EmitterContract
<MessagingClientApiEvents
>
Type parameters
Name | Type |
---|---|
T | extends keyof MessagingClientApiEvents |
Parameters
Name | Type |
---|---|
event | T |
fn | (...args : ArgumentMap <MessagingClientApiEvents >[Extract <T , keyof MessagingClientApiEvents >]) => void |
Returns
EmitterContract
<MessagingClientApiEvents
>
▸ once<T
>(event
, fn
): EmitterContract
<MessagingClientApiEvents
>
Type parameters
Name | Type |
---|---|
T | extends keyof MessagingClientApiEvents |
Parameters
Name | Type |
---|---|
event | T |
fn | (...args : ArgumentMap <MessagingClientApiEvents >[Extract <T , keyof MessagingClientApiEvents >]) => void |
Returns
EmitterContract
<MessagingClientApiEvents
>
▸ removeAllListeners<T
>(event?
): EmitterContract
<MessagingClientApiEvents
>
Type parameters
Name | Type |
---|---|
T | extends keyof MessagingClientApiEvents |
Parameters
Name | Type |
---|---|
event? | T |
Returns
EmitterContract
<MessagingClientApiEvents
>
▸ send(params
): Promise
<MessagingSendResult
>
Send an outbound SMS or MMS message.
Parameters
Name | Type | Description |
---|---|---|
params | Object | - |
params.body? | string | The content of the message. Optional if |
params.context? | string | Inbound events for the message will be received on this context. If not specified, a |
params.from | string | The phone number to place the message from. Must be a SignalWire phone number or short code that you own. |
params.media? | string [] | Array of URLs to send in the message. Optional if |
params.region? | string | Region of the world to originate the message from. A default value is picked based on account preferences or device location. |
params.tags? | string [] | Array of strings to tag the message with for searching in the UI. |
params.to | string | The phone number to send to. |
Returns
Promise
<MessagingSendResult
>
Asynchronously returns a result object.
Example
Send a message.
try {
const sendResult = await client.send({
from: '+1xxx',
to: '+1yyy',
body: 'Hello World!'
})
console.log('Message ID: ', sendResult.messageId)
} catch (e) {
console.error(e.message)
}