@xenterprises/fastify-xtwilio
v1.0.0
Published
Fastify plugin for Twilio communications (SMS, Conversations, RCS) and SendGrid email.
Downloads
77
Readme
xTwilio
Fastify v5 plugin for Twilio communications (SMS, Conversations, RCS) and SendGrid email services.
Requirements
- Fastify v5.0.0+
- Node.js v20+
Installation
npm install @xenterprises/fastify-xtwilio fastify@5Usage
import Fastify from 'fastify';
import xTwilio from '@xenterprises/fastify-xtwilio';
const fastify = Fastify();
await fastify.register(xTwilio, {
twilio: {
accountSid: process.env.TWILIO_ACCOUNT_SID,
authToken: process.env.TWILIO_AUTH_TOKEN,
phoneNumber: process.env.TWILIO_PHONE_NUMBER,
messagingServiceSid: process.env.TWILIO_MESSAGING_SERVICE_SID, // For RCS
},
sendgrid: {
apiKey: process.env.SENDGRID_API_KEY,
fromEmail: process.env.SENDGRID_FROM_EMAIL,
},
});Services
SMS Service (fastify.sms)
// Send SMS
await fastify.sms.send('+1234567890', 'Hello World');
// Send MMS
await fastify.sms.sendMMS('+1234567890', 'Check this out!', 'https://example.com/image.jpg');
// Schedule SMS
await fastify.sms.schedule('+1234567890', 'Reminder', new Date('2024-12-31T10:00:00Z'));
// Send bulk SMS
await fastify.sms.sendBulk([
{ to: '+1234567890', body: 'Message 1' },
{ to: '+0987654321', body: 'Message 2' },
]);
// Get message status
await fastify.sms.getStatus('SMXXXXXXXX');
// Validate phone number
await fastify.sms.validatePhoneNumber('+1234567890');Conversations Service (fastify.conversations)
// Create conversation
const conversation = await fastify.conversations.create('Support Chat');
// Add participants
await fastify.conversations.addParticipant(conversation.sid, '[email protected]');
await fastify.conversations.addParticipant(conversation.sid, null, '+1234567890');
// Send message
await fastify.conversations.sendMessage(conversation.sid, 'Hello!', '[email protected]');
// Get messages
const messages = await fastify.conversations.getMessages(conversation.sid);
// List participants
const participants = await fastify.conversations.listParticipants(conversation.sid);RCS Service (fastify.rcs)
// Send basic RCS message
await fastify.rcs.send('+1234567890', 'Hello from RCS!');
// Send rich card
await fastify.rcs.sendRichCard('+1234567890', {
title: 'Summer Sale',
description: '50% off all items',
mediaUrl: 'https://example.com/banner.jpg',
actions: [
{ type: 'url', text: 'Shop Now', url: 'https://example.com/shop' },
],
});
// Send carousel
await fastify.rcs.sendCarousel('+1234567890', [
{ title: 'Product 1', description: 'Description 1', mediaUrl: 'url1.jpg' },
{ title: 'Product 2', description: 'Description 2', mediaUrl: 'url2.jpg' },
]);
// Send with quick replies
await fastify.rcs.sendQuickReplies('+1234567890', 'How can we help?', [
{ text: 'Support', payload: 'support' },
{ text: 'Sales', payload: 'sales' },
]);
// Use content template
await fastify.rcs.sendTemplate('+1234567890', 'HXXXXXXXC', { name: 'John' });Email Service (fastify.email)
// Send email
await fastify.email.send(
'[email protected]',
'Welcome!',
'<h1>Welcome to our service</h1>',
'Welcome to our service'
);
// Send with template
await fastify.email.sendTemplate(
'[email protected]',
'Welcome Email',
'd-1234567890abcdef',
{ firstName: 'John', code: '123456' }
);
// Send with attachments
await fastify.email.sendWithAttachments(
'[email protected]',
'Invoice',
'<p>Your invoice is attached</p>',
[
{
content: 'base64EncodedContent',
filename: 'invoice.pdf',
type: 'application/pdf',
},
]
);
// Send bulk emails
await fastify.email.sendBulk(
['[email protected]', '[email protected]'],
'Newsletter',
'<h1>This months updates</h1>'
);
// Validate email
const validation = await fastify.email.validate('[email protected]');
// Contact management
await fastify.email.addContact('[email protected]', {
firstName: 'John',
lastName: 'Doe',
});
await fastify.email.searchContact('[email protected]');
// List management
const list = await fastify.email.createList('Newsletter Subscribers');
const lists = await fastify.email.getLists();Configuration Options
Twilio Options
{
twilio: {
accountSid: 'ACXXXXXXXX', // Required
authToken: 'your_auth_token', // Required
phoneNumber: '+1234567890', // Required for SMS (or use messagingServiceSid)
messagingServiceSid: 'MGXXXXXXXX', // Required for RCS, optional for SMS
active: true, // Optional, default true
}
}SendGrid Options
{
sendgrid: {
apiKey: 'SG.XXXXXXXX', // Required
fromEmail: '[email protected]', // Required
active: true, // Optional, default true
}
}API Reference
SMS Methods
send(to, body, options)- Send SMSsendMMS(to, body, mediaUrl, options)- Send MMSschedule(to, body, sendAt)- Schedule messagecancelScheduled(messageSid)- Cancel scheduled messageget(messageSid)- Get message detailsgetStatus(messageSid)- Get message statuslist(filters)- List messagesdelete(messageSid)- Delete messagegetMedia(messageSid)- Get media from MMSvalidatePhoneNumber(phoneNumber, options)- Validate phonesendBulk(messages)- Send bulk SMS
Conversations Methods
create(friendlyName, attributes)- Create conversationget(conversationSid)- Get conversationupdate(conversationSid, updates)- Update conversationlist(filters)- List conversationsdelete(conversationSid)- Delete conversationaddParticipant(conversationSid, identity, messagingBindingAddress)- Add participantlistParticipants(conversationSid)- List participantsremoveParticipant(conversationSid, participantSid)- Remove participantsendMessage(conversationSid, body, author, attributes)- Send messagesendMediaMessage(conversationSid, mediaUrl, body, author)- Send mediagetMessages(conversationSid, options)- Get messagesgetMessage(conversationSid, messageSid)- Get specific messagedeleteMessage(conversationSid, messageSid)- Delete messagegetWebhooks(conversationSid)- Get webhooksupdateWebhooks(conversationSid, webhookConfig)- Update webhooks
RCS Methods
send(to, body, options)- Send basic RCSsendMedia(to, body, mediaUrl)- Send with mediasendTemplate(to, contentSid, contentVariables)- Send templatesendRichCard(to, card)- Send rich cardsendCarousel(to, cards)- Send carouselsendQuickReplies(to, body, replies)- Send quick repliesgetStatus(messageSid)- Get statuslistTemplates(options)- List templatesgetTemplate(contentSid)- Get templatedeleteTemplate(contentSid)- Delete template
Email Methods
send(to, subject, html, text, options)- Send emailsendTemplate(to, subject, templateId, dynamicData, options)- Send templatesendWithAttachments(to, subject, html, attachments)- Send with attachmentssendBulk(to, subject, html)- Send bulksendPersonalizedBulk(messages)- Send personalized bulkvalidate(email)- Validate emailaddContact(email, data, listIds)- Add contactsearchContact(email)- Search contactdeleteContact(contactId)- Delete contactcreateList(name)- Create listgetLists()- Get listsdeleteList(listId)- Delete list
License
ISC
