@xenterprises/fastify-xtwilio 1.0.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.
- package/.dockerignore +63 -0
- package/.env.example +257 -0
- package/API.md +973 -0
- package/CHANGELOG.md +189 -0
- package/LICENSE +15 -0
- package/README.md +261 -0
- package/SECURITY.md +721 -0
- package/index.d.ts +999 -0
- package/package.json +55 -0
- package/server/app.js +88 -0
- package/src/services/conversations.js +328 -0
- package/src/services/email.js +362 -0
- package/src/services/rcs.js +284 -0
- package/src/services/sms.js +268 -0
- package/src/xTwilio.js +37 -0
- package/test/xTwilio.test.js +511 -0
package/.dockerignore
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Git
|
|
2
|
+
.git
|
|
3
|
+
.gitignore
|
|
4
|
+
.gitattributes
|
|
5
|
+
|
|
6
|
+
# Node
|
|
7
|
+
node_modules
|
|
8
|
+
npm-debug.log
|
|
9
|
+
npm-error.log
|
|
10
|
+
yarn-error.log
|
|
11
|
+
package-lock.json
|
|
12
|
+
yarn.lock
|
|
13
|
+
|
|
14
|
+
# IDE
|
|
15
|
+
.vscode
|
|
16
|
+
.idea
|
|
17
|
+
*.swp
|
|
18
|
+
*.swo
|
|
19
|
+
*~
|
|
20
|
+
.DS_Store
|
|
21
|
+
.env
|
|
22
|
+
.env.local
|
|
23
|
+
.env.*.local
|
|
24
|
+
|
|
25
|
+
# Testing
|
|
26
|
+
test
|
|
27
|
+
coverage
|
|
28
|
+
.nyc_output
|
|
29
|
+
.mocha_opts
|
|
30
|
+
|
|
31
|
+
# CI/CD
|
|
32
|
+
.github
|
|
33
|
+
.gitlab-ci.yml
|
|
34
|
+
.circleci
|
|
35
|
+
.travis.yml
|
|
36
|
+
|
|
37
|
+
# Build outputs
|
|
38
|
+
dist
|
|
39
|
+
build
|
|
40
|
+
.next
|
|
41
|
+
out
|
|
42
|
+
|
|
43
|
+
# Documentation
|
|
44
|
+
docs
|
|
45
|
+
README.md
|
|
46
|
+
CHANGELOG.md
|
|
47
|
+
SECURITY.md
|
|
48
|
+
QUICK_START.md
|
|
49
|
+
CONTRIBUTING.md
|
|
50
|
+
|
|
51
|
+
# Development
|
|
52
|
+
Dockerfile
|
|
53
|
+
docker-compose.yml
|
|
54
|
+
.dockerignore
|
|
55
|
+
.eslintrc
|
|
56
|
+
.prettierrc
|
|
57
|
+
tsconfig.json
|
|
58
|
+
vitest.config.js
|
|
59
|
+
|
|
60
|
+
# Other
|
|
61
|
+
.env.example
|
|
62
|
+
LICENSE
|
|
63
|
+
NOTES.md
|
package/.env.example
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
# ============================================================================
|
|
2
|
+
# SERVER CONFIGURATION
|
|
3
|
+
# ============================================================================
|
|
4
|
+
|
|
5
|
+
# Server port to listen on
|
|
6
|
+
PORT=3000
|
|
7
|
+
|
|
8
|
+
# Node environment: development, staging, production
|
|
9
|
+
NODE_ENV=development
|
|
10
|
+
|
|
11
|
+
# Enable detailed logging for debugging
|
|
12
|
+
DEBUG=false
|
|
13
|
+
|
|
14
|
+
# ============================================================================
|
|
15
|
+
# TWILIO SMS CONFIGURATION
|
|
16
|
+
# ============================================================================
|
|
17
|
+
|
|
18
|
+
# Twilio Account SID (Find at: https://console.twilio.com/)
|
|
19
|
+
# This is your unique Twilio account identifier
|
|
20
|
+
TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
|
21
|
+
|
|
22
|
+
# Twilio Auth Token (Find at: https://console.twilio.com/)
|
|
23
|
+
# Keep this secret! Use environment variables in production
|
|
24
|
+
TWILIO_AUTH_TOKEN=your_auth_token_here
|
|
25
|
+
|
|
26
|
+
# Twilio Phone Number for SMS (Find at: https://console.twilio.com/phone-numbers/incoming)
|
|
27
|
+
# Format: +1234567890 or +[country_code][number]
|
|
28
|
+
# This is the "FROM" number for SMS messages
|
|
29
|
+
TWILIO_PHONE_NUMBER=+1234567890
|
|
30
|
+
|
|
31
|
+
# ============================================================================
|
|
32
|
+
# TWILIO RCS (RICH COMMUNICATION SERVICES) CONFIGURATION
|
|
33
|
+
# ============================================================================
|
|
34
|
+
|
|
35
|
+
# Twilio Messaging Service SID (Find at: https://console.twilio.com/messaging/services)
|
|
36
|
+
# Required for RCS and advanced SMS features
|
|
37
|
+
# A Messaging Service manages multiple phone numbers and templates
|
|
38
|
+
TWILIO_MESSAGING_SERVICE_SID=MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
|
39
|
+
|
|
40
|
+
# Messaging Service Fallback Strategy when primary fails
|
|
41
|
+
# Options: none, sms, email
|
|
42
|
+
TWILIO_MESSAGING_FALLBACK_STRATEGY=sms
|
|
43
|
+
|
|
44
|
+
# Default RCS template region for content delivery
|
|
45
|
+
# Options: us, eu, apac
|
|
46
|
+
TWILIO_RCS_REGION=us
|
|
47
|
+
|
|
48
|
+
# ============================================================================
|
|
49
|
+
# TWILIO CONVERSATIONS CONFIGURATION
|
|
50
|
+
# ============================================================================
|
|
51
|
+
|
|
52
|
+
# Enable Conversations service (customer chat, multi-channel messaging)
|
|
53
|
+
TWILIO_CONVERSATIONS_ENABLED=true
|
|
54
|
+
|
|
55
|
+
# Webhook URL for conversation events (optional)
|
|
56
|
+
# Twilio will POST conversation events to this URL
|
|
57
|
+
TWILIO_CONVERSATIONS_WEBHOOK_URL=https://your-domain.com/webhooks/conversations
|
|
58
|
+
|
|
59
|
+
# Webhook authentication secret for validating Twilio requests
|
|
60
|
+
TWILIO_CONVERSATIONS_WEBHOOK_SECRET=your_webhook_secret_here
|
|
61
|
+
|
|
62
|
+
# Default conversation attributes in JSON format
|
|
63
|
+
TWILIO_CONVERSATIONS_DEFAULT_ATTRIBUTES={}
|
|
64
|
+
|
|
65
|
+
# ============================================================================
|
|
66
|
+
# SENDGRID EMAIL CONFIGURATION
|
|
67
|
+
# ============================================================================
|
|
68
|
+
|
|
69
|
+
# SendGrid API Key (Get from: https://app.sendgrid.com/settings/api_keys)
|
|
70
|
+
# This is your authentication token for SendGrid API
|
|
71
|
+
# Treat this as sensitive as a password - never commit to version control
|
|
72
|
+
SENDGRID_API_KEY=SG.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
|
73
|
+
|
|
74
|
+
# Sender email address for all outgoing emails
|
|
75
|
+
# This email must be verified in your SendGrid account
|
|
76
|
+
SENDGRID_FROM_EMAIL=noreply@example.com
|
|
77
|
+
|
|
78
|
+
# Sender name to display in "From" field
|
|
79
|
+
# Example: "Support Team" <noreply@example.com>
|
|
80
|
+
SENDGRID_FROM_NAME=Your Application
|
|
81
|
+
|
|
82
|
+
# Default reply-to email address (optional)
|
|
83
|
+
# If not provided, recipients reply to SENDGRID_FROM_EMAIL
|
|
84
|
+
SENDGRID_REPLY_TO_EMAIL=support@example.com
|
|
85
|
+
|
|
86
|
+
# ============================================================================
|
|
87
|
+
# SENDGRID ADVANCED CONFIGURATION
|
|
88
|
+
# ============================================================================
|
|
89
|
+
|
|
90
|
+
# Enable SendGrid contact management
|
|
91
|
+
# If true, emails sent will also add/update contacts in SendGrid
|
|
92
|
+
SENDGRID_MANAGE_CONTACTS=false
|
|
93
|
+
|
|
94
|
+
# SendGrid list ID for newsletter subscriptions (optional)
|
|
95
|
+
# Contacts can be automatically added to this list
|
|
96
|
+
SENDGRID_NEWSLETTER_LIST_ID=
|
|
97
|
+
|
|
98
|
+
# Enable email validation before sending
|
|
99
|
+
# Checks email format and deliverability (uses SendGrid validation API)
|
|
100
|
+
SENDGRID_VALIDATE_EMAILS=true
|
|
101
|
+
|
|
102
|
+
# Enable click and open tracking
|
|
103
|
+
SENDGRID_TRACK_CLICKS=true
|
|
104
|
+
SENDGRID_TRACK_OPENS=true
|
|
105
|
+
|
|
106
|
+
# Email templates for transactional messages
|
|
107
|
+
# SendGrid Template IDs (create templates in SendGrid dashboard)
|
|
108
|
+
SENDGRID_WELCOME_TEMPLATE_ID=d-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
|
109
|
+
SENDGRID_PASSWORD_RESET_TEMPLATE_ID=d-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
|
110
|
+
SENDGRID_RECEIPT_TEMPLATE_ID=d-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
|
111
|
+
SENDGRID_NOTIFICATION_TEMPLATE_ID=d-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
|
112
|
+
|
|
113
|
+
# ============================================================================
|
|
114
|
+
# FEATURE FLAGS & SERVICE TOGGLES
|
|
115
|
+
# ============================================================================
|
|
116
|
+
|
|
117
|
+
# Enable/disable SMS service
|
|
118
|
+
# If false, SMS methods will throw "Service disabled" error
|
|
119
|
+
TWILIO_ENABLED=true
|
|
120
|
+
|
|
121
|
+
# Enable/disable SendGrid service
|
|
122
|
+
# If false, email methods will throw "Service disabled" error
|
|
123
|
+
SENDGRID_ENABLED=true
|
|
124
|
+
|
|
125
|
+
# ============================================================================
|
|
126
|
+
# RATE LIMITING & THROTTLING
|
|
127
|
+
# ============================================================================
|
|
128
|
+
|
|
129
|
+
# Maximum SMS messages per minute per phone number
|
|
130
|
+
# Prevents accidental/malicious spam
|
|
131
|
+
SMS_RATE_LIMIT_PER_MINUTE=10
|
|
132
|
+
|
|
133
|
+
# Maximum emails per minute per recipient
|
|
134
|
+
# Respects SendGrid's reputation limits
|
|
135
|
+
EMAIL_RATE_LIMIT_PER_MINUTE=5
|
|
136
|
+
|
|
137
|
+
# ============================================================================
|
|
138
|
+
# RETRY & ERROR HANDLING
|
|
139
|
+
# ============================================================================
|
|
140
|
+
|
|
141
|
+
# Maximum retries for failed SMS (Twilio will handle automatically)
|
|
142
|
+
SMS_MAX_RETRIES=3
|
|
143
|
+
|
|
144
|
+
# Maximum retries for failed emails
|
|
145
|
+
EMAIL_MAX_RETRIES=2
|
|
146
|
+
|
|
147
|
+
# Delay in milliseconds before first retry
|
|
148
|
+
RETRY_INITIAL_DELAY=1000
|
|
149
|
+
|
|
150
|
+
# Maximum delay in milliseconds between retries (exponential backoff)
|
|
151
|
+
RETRY_MAX_DELAY=30000
|
|
152
|
+
|
|
153
|
+
# ============================================================================
|
|
154
|
+
# LOGGING & MONITORING
|
|
155
|
+
# ============================================================================
|
|
156
|
+
|
|
157
|
+
# Log level for xTwilio operations
|
|
158
|
+
# Options: error, warn, info, debug, trace
|
|
159
|
+
LOG_LEVEL=info
|
|
160
|
+
|
|
161
|
+
# Enable detailed request/response logging for Twilio API calls
|
|
162
|
+
TWILIO_LOG_REQUESTS=false
|
|
163
|
+
|
|
164
|
+
# Enable detailed request/response logging for SendGrid API calls
|
|
165
|
+
SENDGRID_LOG_REQUESTS=false
|
|
166
|
+
|
|
167
|
+
# ============================================================================
|
|
168
|
+
# SECURITY & VALIDATION
|
|
169
|
+
# ============================================================================
|
|
170
|
+
|
|
171
|
+
# Validate phone numbers before sending SMS
|
|
172
|
+
# Uses Twilio Lookup API (may incur additional charges)
|
|
173
|
+
VALIDATE_PHONE_NUMBERS=true
|
|
174
|
+
|
|
175
|
+
# Country code for default phone number validation
|
|
176
|
+
# Used if no country code provided in phone number
|
|
177
|
+
DEFAULT_COUNTRY_CODE=US
|
|
178
|
+
|
|
179
|
+
# Maximum message length (SMS standard is 160 characters)
|
|
180
|
+
# Longer messages are split into multiple SMS (multi-part)
|
|
181
|
+
MAX_MESSAGE_LENGTH=160
|
|
182
|
+
|
|
183
|
+
# Allowed domains for media URLs in RCS messages
|
|
184
|
+
# Comma-separated list: https://domain1.com,https://domain2.com
|
|
185
|
+
# If empty, all HTTPS domains allowed
|
|
186
|
+
ALLOWED_MEDIA_DOMAINS=
|
|
187
|
+
|
|
188
|
+
# ============================================================================
|
|
189
|
+
# WEBHOOK CONFIGURATION
|
|
190
|
+
# ============================================================================
|
|
191
|
+
|
|
192
|
+
# Webhook path for Twilio to send status updates
|
|
193
|
+
# Twilio will POST message status updates here
|
|
194
|
+
TWILIO_WEBHOOK_PATH=/webhooks/twilio
|
|
195
|
+
|
|
196
|
+
# Webhook authentication - require X-Twilio-Signature validation
|
|
197
|
+
VALIDATE_TWILIO_WEBHOOKS=true
|
|
198
|
+
|
|
199
|
+
# Webhook timeout in milliseconds
|
|
200
|
+
WEBHOOK_TIMEOUT=5000
|
|
201
|
+
|
|
202
|
+
# ============================================================================
|
|
203
|
+
# DATABASE/PERSISTENCE (Optional)
|
|
204
|
+
# ============================================================================
|
|
205
|
+
|
|
206
|
+
# If using a database to store message history, conversation state, etc.
|
|
207
|
+
DATABASE_URL=
|
|
208
|
+
|
|
209
|
+
# Redis connection string for caching, rate limiting, message queue
|
|
210
|
+
# Format: redis://[username:password]@localhost:6379/0
|
|
211
|
+
REDIS_URL=
|
|
212
|
+
|
|
213
|
+
# ============================================================================
|
|
214
|
+
# DEVELOPMENT & TESTING
|
|
215
|
+
# ============================================================================
|
|
216
|
+
|
|
217
|
+
# Use Twilio test credentials (for testing without sending real SMS)
|
|
218
|
+
# Only works with Twilio's test phone numbers
|
|
219
|
+
USE_TWILIO_TEST_CREDENTIALS=false
|
|
220
|
+
|
|
221
|
+
# Twilio test account SID (if using test credentials)
|
|
222
|
+
TWILIO_TEST_ACCOUNT_SID=
|
|
223
|
+
|
|
224
|
+
# Use mock SendGrid for development (no real emails sent)
|
|
225
|
+
USE_SENDGRID_MOCK=false
|
|
226
|
+
|
|
227
|
+
# Directory for saving test emails (when using mock SendGrid)
|
|
228
|
+
SENDGRID_MOCK_STORAGE_PATH=./test-emails
|
|
229
|
+
|
|
230
|
+
# ============================================================================
|
|
231
|
+
# NOTES
|
|
232
|
+
# ============================================================================
|
|
233
|
+
|
|
234
|
+
# 🔐 SECURITY BEST PRACTICES:
|
|
235
|
+
# 1. Never commit .env file to version control (only commit .env.example)
|
|
236
|
+
# 2. Use strong, unique API keys and tokens
|
|
237
|
+
# 3. Rotate credentials regularly
|
|
238
|
+
# 4. Use different keys for development and production
|
|
239
|
+
# 5. Store sensitive keys in secure vaults (AWS Secrets Manager, HashiCorp Vault, etc.)
|
|
240
|
+
#
|
|
241
|
+
# 🚀 DEPLOYMENT:
|
|
242
|
+
# 1. Use environment variables from your hosting provider (Vercel, Railway, etc.)
|
|
243
|
+
# 2. Never log or expose API keys in error messages
|
|
244
|
+
# 3. Enable HTTPS for all webhook endpoints
|
|
245
|
+
# 4. Validate incoming webhooks using Twilio's signature validation
|
|
246
|
+
#
|
|
247
|
+
# 📞 TWILIO RESOURCES:
|
|
248
|
+
# - Console: https://console.twilio.com/
|
|
249
|
+
# - API Docs: https://www.twilio.com/docs/api
|
|
250
|
+
# - Pricing: https://www.twilio.com/pricing
|
|
251
|
+
# - Support: https://www.twilio.com/support/contact
|
|
252
|
+
#
|
|
253
|
+
# 📧 SENDGRID RESOURCES:
|
|
254
|
+
# - Dashboard: https://app.sendgrid.com/
|
|
255
|
+
# - API Docs: https://docs.sendgrid.com/api-reference/
|
|
256
|
+
# - Pricing: https://sendgrid.com/pricing/
|
|
257
|
+
# - Support: https://support.sendgrid.com/
|