@memberjunction/communication-twilio 2.43.0 → 2.45.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +279 -0
  2. package/package.json +4 -4
package/README.md ADDED
@@ -0,0 +1,279 @@
1
+ # @memberjunction/communication-twilio
2
+
3
+ A Twilio provider implementation for the MemberJunction Communication framework, enabling SMS, WhatsApp, and Facebook Messenger messaging capabilities.
4
+
5
+ ## Overview
6
+
7
+ This package provides a Twilio-based implementation of the MemberJunction Communication Provider interface. It supports sending and receiving messages through multiple channels:
8
+
9
+ - **SMS** - Traditional text messaging
10
+ - **WhatsApp** - WhatsApp Business messaging
11
+ - **Facebook Messenger** - Facebook page messaging
12
+
13
+ The provider automatically detects the appropriate channel based on the recipient format and handles all necessary formatting and API interactions with Twilio's services.
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @memberjunction/communication-twilio
19
+ ```
20
+
21
+ ## Configuration
22
+
23
+ The provider requires environment variables to be set for Twilio credentials. Create a `.env` file in your project root:
24
+
25
+ ```env
26
+ # Required - Twilio Account Credentials
27
+ TWILIO_ACCOUNT_SID=your_account_sid
28
+ TWILIO_AUTH_TOKEN=your_auth_token
29
+ TWILIO_PHONE_NUMBER=+1234567890
30
+
31
+ # Optional - For WhatsApp messaging
32
+ TWILIO_WHATSAPP_NUMBER=+1234567890
33
+
34
+ # Optional - For Facebook Messenger
35
+ TWILIO_FACEBOOK_PAGE_ID=your_page_id
36
+ ```
37
+
38
+ ### Environment Variables
39
+
40
+ | Variable | Required | Description |
41
+ |----------|----------|-------------|
42
+ | `TWILIO_ACCOUNT_SID` | Yes | Your Twilio Account SID |
43
+ | `TWILIO_AUTH_TOKEN` | Yes | Your Twilio Auth Token |
44
+ | `TWILIO_PHONE_NUMBER` | Yes | Your Twilio phone number for SMS |
45
+ | `TWILIO_WHATSAPP_NUMBER` | No | Your Twilio WhatsApp-enabled number |
46
+ | `TWILIO_FACEBOOK_PAGE_ID` | No | Your Facebook Page ID for Messenger |
47
+
48
+ ## Usage
49
+
50
+ ### Basic Setup
51
+
52
+ ```typescript
53
+ import { TwilioProvider } from '@memberjunction/communication-twilio';
54
+
55
+ // The provider will be automatically registered with the MemberJunction framework
56
+ // via the @RegisterClass decorator when imported
57
+ const provider = new TwilioProvider();
58
+ ```
59
+
60
+ ### Sending Messages
61
+
62
+ #### SMS Message
63
+ ```typescript
64
+ import { ProcessedMessage } from '@memberjunction/communication-types';
65
+
66
+ const message: ProcessedMessage = {
67
+ To: '+1234567890',
68
+ ProcessedBody: 'Hello from MemberJunction!',
69
+ // Other required fields...
70
+ };
71
+
72
+ const result = await provider.SendSingleMessage(message);
73
+ if (result.Success) {
74
+ console.log('SMS sent successfully');
75
+ } else {
76
+ console.error('Failed to send SMS:', result.Error);
77
+ }
78
+ ```
79
+
80
+ #### WhatsApp Message
81
+ ```typescript
82
+ const whatsappMessage: ProcessedMessage = {
83
+ To: 'whatsapp:+1234567890', // Prefix with 'whatsapp:'
84
+ ProcessedBody: 'Hello from WhatsApp!',
85
+ // Other required fields...
86
+ };
87
+
88
+ const result = await provider.SendSingleMessage(whatsappMessage);
89
+ ```
90
+
91
+ #### Facebook Messenger Message
92
+ ```typescript
93
+ const messengerMessage: ProcessedMessage = {
94
+ To: 'messenger:user_psid', // Prefix with 'messenger:' and use Page-Scoped ID
95
+ ProcessedBody: 'Hello from Messenger!',
96
+ // Other required fields...
97
+ };
98
+
99
+ const result = await provider.SendSingleMessage(messengerMessage);
100
+ ```
101
+
102
+ #### Sending Media (MMS/WhatsApp Media)
103
+ ```typescript
104
+ const mediaMessage: ProcessedMessage = {
105
+ To: '+1234567890',
106
+ ProcessedBody: 'Check out this image!',
107
+ ContextData: {
108
+ mediaUrls: ['https://example.com/image.jpg']
109
+ },
110
+ // Other required fields...
111
+ };
112
+
113
+ const result = await provider.SendSingleMessage(mediaMessage);
114
+ ```
115
+
116
+ ### Retrieving Messages
117
+
118
+ ```typescript
119
+ import { GetMessagesParams } from '@memberjunction/communication-types';
120
+
121
+ const params: GetMessagesParams = {
122
+ NumMessages: 50,
123
+ ContextData: {
124
+ // Optional filters
125
+ from: '+1234567890',
126
+ to: '+0987654321',
127
+ dateSent: new Date('2024-01-01')
128
+ }
129
+ };
130
+
131
+ const result = await provider.GetMessages(params);
132
+ if (result.Success) {
133
+ console.log(`Retrieved ${result.Messages.length} messages`);
134
+ result.Messages.forEach(msg => {
135
+ console.log(`From: ${msg.From}, Body: ${msg.Body}`);
136
+ });
137
+ }
138
+ ```
139
+
140
+ ### Replying to Messages
141
+
142
+ ```typescript
143
+ import { ReplyToMessageParams } from '@memberjunction/communication-types';
144
+
145
+ const replyParams: ReplyToMessageParams = {
146
+ MessageID: 'original_message_sid', // The Twilio Message SID
147
+ Message: {
148
+ ProcessedBody: 'Thanks for your message!',
149
+ // Other message fields...
150
+ }
151
+ };
152
+
153
+ const result = await provider.ReplyToMessage(replyParams);
154
+ if (result.Success) {
155
+ console.log('Reply sent successfully');
156
+ }
157
+ ```
158
+
159
+ ### Forwarding Messages
160
+
161
+ ```typescript
162
+ import { ForwardMessageParams } from '@memberjunction/communication-types';
163
+
164
+ const forwardParams: ForwardMessageParams = {
165
+ MessageID: 'message_to_forward_sid',
166
+ ToRecipients: ['+1234567890', 'whatsapp:+0987654321'],
167
+ Message: 'FYI - forwarding this message' // Optional comment
168
+ };
169
+
170
+ const result = await provider.ForwardMessage(forwardParams);
171
+ if (result.Success) {
172
+ console.log('Message forwarded successfully');
173
+ }
174
+ ```
175
+
176
+ ## Channel Detection
177
+
178
+ The provider automatically detects the communication channel based on the recipient format:
179
+
180
+ - **SMS**: Standard phone number format (e.g., `+1234567890`)
181
+ - **WhatsApp**: Prefixed with `whatsapp:` (e.g., `whatsapp:+1234567890`)
182
+ - **Facebook Messenger**: Prefixed with `messenger:` (e.g., `messenger:user_psid`)
183
+
184
+ ## API Reference
185
+
186
+ ### TwilioProvider
187
+
188
+ The main provider class that implements the `BaseCommunicationProvider` interface.
189
+
190
+ #### Methods
191
+
192
+ ##### `SendSingleMessage(message: ProcessedMessage): Promise<MessageResult>`
193
+ Sends a single message through the appropriate Twilio channel.
194
+
195
+ **Parameters:**
196
+ - `message`: The processed message to send
197
+ - `To`: Recipient (phone number or channel-prefixed ID)
198
+ - `From`: (Optional) Sender ID, defaults to configured numbers
199
+ - `ProcessedBody`: The message content
200
+ - `ContextData.mediaUrls`: (Optional) Array of media URLs for MMS/WhatsApp media
201
+
202
+ **Returns:** `MessageResult` with success status and any error information
203
+
204
+ ##### `GetMessages(params: GetMessagesParams): Promise<GetMessagesResult>`
205
+ Retrieves messages from Twilio based on filter criteria.
206
+
207
+ **Parameters:**
208
+ - `params`: Message retrieval parameters
209
+ - `NumMessages`: Maximum number of messages to retrieve
210
+ - `ContextData`: Optional filters (from, to, dateSent)
211
+
212
+ **Returns:** `GetMessagesResult` with retrieved messages and status
213
+
214
+ ##### `ReplyToMessage(params: ReplyToMessageParams): Promise<ReplyToMessageResult>`
215
+ Sends a reply to a specific message.
216
+
217
+ **Parameters:**
218
+ - `params`: Reply parameters
219
+ - `MessageID`: The Twilio SID of the message to reply to
220
+ - `Message`: The reply message content
221
+
222
+ **Returns:** `ReplyToMessageResult` with success status
223
+
224
+ ##### `ForwardMessage(params: ForwardMessageParams): Promise<ForwardMessageResult>`
225
+ Forwards a message to one or more recipients.
226
+
227
+ **Parameters:**
228
+ - `params`: Forward parameters
229
+ - `MessageID`: The Twilio SID of the message to forward
230
+ - `ToRecipients`: Array of recipient addresses
231
+ - `Message`: (Optional) Additional comment to include
232
+
233
+ **Returns:** `ForwardMessageResult` with success status
234
+
235
+ ## Dependencies
236
+
237
+ This package depends on:
238
+ - `@memberjunction/communication-types` - Communication provider interfaces
239
+ - `@memberjunction/core` - Core MemberJunction utilities
240
+ - `@memberjunction/global` - Global registration utilities
241
+ - `twilio` - Official Twilio SDK
242
+ - `dotenv` - Environment variable management
243
+ - `env-var` - Environment variable validation
244
+
245
+ ## Integration with MemberJunction
246
+
247
+ This provider is automatically registered with the MemberJunction framework using the `@RegisterClass` decorator. Once imported, it becomes available for use through the MemberJunction communication system.
248
+
249
+ The provider name registered is: **"Twilio"**
250
+
251
+ ## Build and Development
252
+
253
+ ### Building the Package
254
+ ```bash
255
+ npm run build
256
+ ```
257
+
258
+ ### Cleaning Build Artifacts
259
+ ```bash
260
+ npm run clean
261
+ ```
262
+
263
+ ### TypeScript Configuration
264
+ The package uses TypeScript and compiles to ES2020 with CommonJS modules. Type definitions are included in the distribution.
265
+
266
+ ## Notes
267
+
268
+ - The provider uses plain text for message bodies as HTML is not supported by SMS/messaging channels
269
+ - Message threading is simulated using Message SIDs as Twilio doesn't have a native thread concept
270
+ - Media attachments are supported through the `mediaUrls` context data property
271
+ - All messages are sent asynchronously using the Twilio REST API
272
+
273
+ ## Error Handling
274
+
275
+ The provider includes comprehensive error handling with detailed logging through the MemberJunction logging system. All errors are caught and returned in the result objects with descriptive error messages.
276
+
277
+ ## License
278
+
279
+ This package is part of the MemberJunction framework. See the main repository for license information.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@memberjunction/communication-twilio",
3
- "version": "2.43.0",
3
+ "version": "2.45.0",
4
4
  "description": "Twilio provider for MemberJunction Communication framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -12,9 +12,9 @@
12
12
  "clean": "rimraf dist"
13
13
  },
14
14
  "dependencies": {
15
- "@memberjunction/communication-types": "2.43.0",
16
- "@memberjunction/core": "2.43.0",
17
- "@memberjunction/global": "2.43.0",
15
+ "@memberjunction/communication-types": "2.45.0",
16
+ "@memberjunction/core": "2.45.0",
17
+ "@memberjunction/global": "2.45.0",
18
18
  "dotenv": "^16.3.1",
19
19
  "env-var": "^7.4.1",
20
20
  "twilio": "^4.23.0"