WhatsappResource
The whatsapp resource provides methods for sending WhatsApp messages, managing typing indicators, and reactions.
await contiguity . whatsapp . send ( params : WhatsappSendParams ): Promise < { message_id : string } >
Send a WhatsApp message to a recipient.
params
WhatsappSendParams
required
Recipient phone number in E.164 format (e.g., +1234567890).
The message content to send.
Sender phone number. If not provided, a number will be automatically selected.
Array of attachment URLs to include with the message.
Fallback configuration for when WhatsApp fails or is unsupported. Array of conditions: whatsapp_unsupported or whatsapp_fails.
Fallback sender number for SMS delivery.
Enable fast-track delivery for time-sensitive messages.
Unique identifier for the sent message.
const result = await contiguity . whatsapp . send ({
to: '+1234567890' ,
from: '+0987654321' ,
message: 'Hello via WhatsApp!' ,
attachments: [ 'https://example.com/document.pdf' ],
fallback: {
when: [ 'whatsapp_unsupported' , 'whatsapp_fails' ],
from: '+0987654321'
}
});
console . log ( result . message_id ); // "msg_..."
reply()
await contiguity . whatsapp . reply (
event : WebhookEventBase ,
params : Omit < WhatsappSendParams , 'to' | 'from' >
): Promise < { message_id : string } >
Reply to an incoming WhatsApp webhook. Automatically fills to and from fields from the webhook event.
The webhook event received from an incoming WhatsApp message.
params
Omit<WhatsappSendParams, 'to' | 'from'>
required
Message parameters (same as send() but without to and from).
Unique identifier for the sent reply.
app . post ( '/webhook' , async ( req , res ) => {
const event = contiguity . webhook . parse ( req . body );
if ( event . type === 'whatsapp.incoming' ) {
await contiguity . whatsapp . reply ( event , {
message: 'Thanks for your WhatsApp message!'
});
}
res . sendStatus ( 200 );
});
typing()
await contiguity . whatsapp . typing ( params : WhatsappTypingParams ): Promise < any >
Send a typing indicator to show that youβre composing a message.
params
WhatsappTypingParams
required
Recipient phone number in E.164 format.
Whether to start or stop the typing indicator.
// Show typing indicator
await contiguity . whatsapp . typing ({
to: '+1234567890' ,
from: '+0987654321' ,
action: 'start'
});
// Simulate composing message...
await new Promise ( resolve => setTimeout ( resolve , 2000 ));
// Stop typing indicator and send message
await contiguity . whatsapp . typing ({
to: '+1234567890' ,
from: '+0987654321' ,
action: 'stop'
});
await contiguity . whatsapp . send ({
to: '+1234567890' ,
from: '+0987654321' ,
message: 'Here is my response!'
});
react()
await contiguity . whatsapp . react (
action : 'add' | 'remove' ,
params : WhatsappReactParams
): Promise < any >
Add or remove a reaction to a WhatsApp message.
Whether to add or remove the reaction.
params
WhatsappReactParams
required
The emoji reaction to add. To remove a reaction, pass null or use action: 'remove'.
The message text to react to.
// Add a reaction
await contiguity . whatsapp . react ( 'add' , {
to: '+1234567890' ,
reaction: 'π' ,
message: 'Thanks for the help!'
});
// Remove a reaction
await contiguity . whatsapp . react ( 'remove' , {
to: '+1234567890' ,
reaction: 'π' ,
message: 'Thanks for the help!'
});
Complete Example
import { Contiguity } from 'contiguity' ;
const contiguity = new Contiguity ( 'contiguity_sk_...' );
// Send a message with typing indicator
await contiguity . whatsapp . typing ({
to: '+1234567890' ,
from: '+0987654321' ,
action: 'start'
});
await new Promise ( resolve => setTimeout ( resolve , 1500 ));
const { message_id } = await contiguity . whatsapp . send ({
to: '+1234567890' ,
from: '+0987654321' ,
message: 'Hello! How can I help you today?' ,
attachments: [ 'https://example.com/menu.pdf' ]
});
await contiguity . whatsapp . typing ({
to: '+1234567890' ,
from: '+0987654321' ,
action: 'stop'
});
console . log ( 'Message sent:' , message_id );