This covers the material that was presented as part of the Conversations sessions at the 2020 Twilio Signal SuperClass. You can find more information at signal.twilio.com
Check out the documentation here. View the file [SendAMessage.js] or refer to the snippets below for sending the different types of messages.
client.messages.create({
to: "<Destinataion Number>",
from: "<Source Number>",
body: "<Message Body>"
}).then(message => {
console.log(`Woohoo! Your Message SID is ${message.sid}`);
}).catch(error => {
console.error('Looks like the Twilio API returned an error:');
console.error(error);
});
To send from an Alphanumeric ID replace the from number in the call to Twilio with the Alpha ID. To check of Alpha Sender IDs are support in your country refer to here
Check out this blog for more info on Alphanumeric Sender IDs
client.messages.create({
to: "<Destinataion Number>",
from: "<Alphanumeric Sender ID>",
body: "<Message Body>"
}).then(message => {
console.log(`Woohoo! Your Message SID is ${message.sid}`);
}).catch(error => {
console.error('Looks like the Twilio API returned an error:');
console.error(error);
});
To send from a message service specify the message service SID in the from field. For more details on Message Services check out this blog post.
client.messages.create({
to: "<Destinataion Number>",
from: "<Message Service SID>",
body: "<Message Body>"
}).then(message => {
console.log(`Woohoo! Your Message SID is ${message.sid}`);
}).catch(error => {
console.error('Looks like the Twilio API returned an error:');
console.error(error);
});
Follow this tutorial for instructions.
The file I used in the session is [SMSStatusCallbacks.js]. Snippet below is Express Route for
// Create a route to handle incoming SMS messages
app.post('/sms', (request, response) => {
console.log(
`Incoming message from ${request.body.From}: ${request.body.Body}`
);
response.type('text/xml');
// Edit the Below to Add a Status Callback.
response.send(`
<Response>
<Message>Thanks for your message.</Message>
</Response>
`);
});
To receive status callbacks for a message you send refer to the documentation here
To respond to a message and specify a status callback add an action parameter as shown below and point to an endpoint that can receive the callbacks.
// Create a route to handle incoming SMS messages with status callback
app.post('/sms', (request, response) => {
console.log(
`Incoming message from ${request.body.From}: ${request.body.Body}`
);
response.type('text/xml');
// Edit the Below to Add a Status Callback.
response.send(`
<Response>
<Message action="/status">Thanks for your message.</Message>
</Response>
`);
});
// Create a route to handle the status update
app.post('/status', request => {
console.log('Status update received');
console.log('Message status: ', request.body.MessageStatus);
console.log('Message SID: ', request.body.MessageSid);
});
Checkout Twilio Conversations here. I ran through the Conversations Quickstart.
To add a whatsapp partipant via the CLI use the below command.
twilio api:conversations:v1:conversations:participants:create \
--conversation-sid=CHXXXXX \
--messaging-binding.address=whatsapp:<WhatsApp ID of recipient> \
--messaging-binding.proxy-address=whatsapp:<Twilio registered WhatsApp SenderID>
For more information on using WhatsApp in conversations refer to this guide
Note:To add a Whatsapp participant to a conversation you need an approved WhatsApp Sender ID on your account. More Information Here. The Conversations API does not yet support using the Whatsapp Sandbox.
Redacting messages in conversations requires the use of Conversations Webhooks. Please refer to the documentation for more info
Below is the express route used to redact a credit card message using the onMessageAdd pre-event webhook
// Create a route to handle incoming Preevent WebHooks
app.post('/preEvent', asyncHandler(async (request, response) => {
let event = request.body;
let eventType = event.EventType;
console.log(
`Incoming ${eventType}`
);
console.log(event);
if (eventType == 'onMessageAdd') {
// Demonstrate Redaction
// Regex for a Credit card like number.
var checkCredit = RegExp('([0-9- ]{15,16})');
if (checkCredit.test(event.Body)) {
console.log('Detected a Credit Card');
//Respond to the message.
response.send({
'body': 'A credit card was redacted.',
'author': 'Chief Compliance Bot'
});
}
};
}));
Snippet for pre-pending messages with the users name.
// Create a route to handle incoming Preevent WebHooks
app.post('/preEvent', asyncHandler(async (request, response) => {
let event = request.body;
let eventType = event.EventType;
console.log(
`Incoming ${eventType}`
);
console.log(event);
if (eventType == 'onMessageAdd') {
// Demonstrating Prepending the User Name
response.send({
'body': `${event.Author} said: ${event.Body}`
});
};
}));