Listen for Incoming Messages

The Messages provider allows you to listen for incoming messages using the Messages API.

Method Signature

onMessage(callback: string, from: Contact, to: Contact)

Types

This function uses the Contact type for the sender and recipient of the message.

MessageContact:

  • type: (String) The channel of the text, e.g. sms, mms, whatsapp, messenger, or viber_service.
  • number: (String) A phone number in in the E.164 format.
  • id: (Optional String) An ID number, for use with Facebook Messenger.

Listening for Messages

Using null as a wild card character, you can listen for any incoming messages to a specified Vonage number. In this example, incoming messages will call the onMessage route.

const session = vcr.createSession();
const messaging = new Messages(session);

const vonageNumber = { type: "sms", number: "447700900000" };
const from = { type: "sms", number: null }; 

await messaging.onMessage("onMessage", from, vonageNumber);