opencode-skills-collection 1.0.186 → 1.0.187

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 (71) hide show
  1. package/bundled-skills/.antigravity-install-manifest.json +5 -1
  2. package/bundled-skills/3d-web-experience/SKILL.md +152 -37
  3. package/bundled-skills/agent-evaluation/SKILL.md +1088 -26
  4. package/bundled-skills/agent-memory-systems/SKILL.md +1037 -25
  5. package/bundled-skills/agent-tool-builder/SKILL.md +668 -16
  6. package/bundled-skills/ai-agents-architect/SKILL.md +271 -31
  7. package/bundled-skills/ai-product/SKILL.md +716 -26
  8. package/bundled-skills/ai-wrapper-product/SKILL.md +450 -44
  9. package/bundled-skills/algolia-search/SKILL.md +867 -15
  10. package/bundled-skills/autonomous-agents/SKILL.md +1033 -26
  11. package/bundled-skills/aws-serverless/SKILL.md +1046 -35
  12. package/bundled-skills/azure-functions/SKILL.md +1318 -19
  13. package/bundled-skills/browser-automation/SKILL.md +1065 -28
  14. package/bundled-skills/browser-extension-builder/SKILL.md +159 -32
  15. package/bundled-skills/bullmq-specialist/SKILL.md +347 -16
  16. package/bundled-skills/clerk-auth/SKILL.md +796 -15
  17. package/bundled-skills/computer-use-agents/SKILL.md +1870 -28
  18. package/bundled-skills/context-window-management/SKILL.md +271 -18
  19. package/bundled-skills/conversation-memory/SKILL.md +453 -24
  20. package/bundled-skills/crewai/SKILL.md +252 -46
  21. package/bundled-skills/discord-bot-architect/SKILL.md +1207 -34
  22. package/bundled-skills/docs/integrations/jetski-cortex.md +3 -3
  23. package/bundled-skills/docs/integrations/jetski-gemini-loader/README.md +1 -1
  24. package/bundled-skills/docs/maintainers/repo-growth-seo.md +3 -3
  25. package/bundled-skills/docs/maintainers/skills-update-guide.md +1 -1
  26. package/bundled-skills/docs/users/bundles.md +1 -1
  27. package/bundled-skills/docs/users/claude-code-skills.md +1 -1
  28. package/bundled-skills/docs/users/gemini-cli-skills.md +1 -1
  29. package/bundled-skills/docs/users/getting-started.md +1 -1
  30. package/bundled-skills/docs/users/kiro-integration.md +1 -1
  31. package/bundled-skills/docs/users/usage.md +4 -4
  32. package/bundled-skills/docs/users/visual-guide.md +4 -4
  33. package/bundled-skills/email-systems/SKILL.md +646 -26
  34. package/bundled-skills/faf-expert/SKILL.md +221 -0
  35. package/bundled-skills/faf-wizard/SKILL.md +252 -0
  36. package/bundled-skills/file-uploads/SKILL.md +212 -11
  37. package/bundled-skills/firebase/SKILL.md +646 -16
  38. package/bundled-skills/gcp-cloud-run/SKILL.md +1117 -32
  39. package/bundled-skills/graphql/SKILL.md +1026 -27
  40. package/bundled-skills/hubspot-integration/SKILL.md +804 -19
  41. package/bundled-skills/idea-darwin/SKILL.md +120 -0
  42. package/bundled-skills/inngest/SKILL.md +431 -16
  43. package/bundled-skills/interactive-portfolio/SKILL.md +342 -44
  44. package/bundled-skills/langfuse/SKILL.md +296 -41
  45. package/bundled-skills/langgraph/SKILL.md +259 -50
  46. package/bundled-skills/micro-saas-launcher/SKILL.md +343 -44
  47. package/bundled-skills/neon-postgres/SKILL.md +572 -15
  48. package/bundled-skills/nextjs-supabase-auth/SKILL.md +269 -21
  49. package/bundled-skills/notion-template-business/SKILL.md +371 -44
  50. package/bundled-skills/personal-tool-builder/SKILL.md +537 -44
  51. package/bundled-skills/plaid-fintech/SKILL.md +825 -19
  52. package/bundled-skills/prompt-caching/SKILL.md +438 -25
  53. package/bundled-skills/rag-engineer/SKILL.md +271 -29
  54. package/bundled-skills/salesforce-development/SKILL.md +912 -19
  55. package/bundled-skills/satori/SKILL.md +54 -0
  56. package/bundled-skills/scroll-experience/SKILL.md +381 -44
  57. package/bundled-skills/segment-cdp/SKILL.md +817 -19
  58. package/bundled-skills/shopify-apps/SKILL.md +1475 -19
  59. package/bundled-skills/slack-bot-builder/SKILL.md +1162 -28
  60. package/bundled-skills/telegram-bot-builder/SKILL.md +152 -37
  61. package/bundled-skills/telegram-mini-app/SKILL.md +445 -44
  62. package/bundled-skills/trigger-dev/SKILL.md +916 -27
  63. package/bundled-skills/twilio-communications/SKILL.md +1310 -28
  64. package/bundled-skills/upstash-qstash/SKILL.md +898 -27
  65. package/bundled-skills/vercel-deployment/SKILL.md +637 -39
  66. package/bundled-skills/viral-generator-builder/SKILL.md +132 -37
  67. package/bundled-skills/voice-agents/SKILL.md +937 -27
  68. package/bundled-skills/voice-ai-development/SKILL.md +375 -46
  69. package/bundled-skills/workflow-automation/SKILL.md +982 -29
  70. package/bundled-skills/zapier-make-patterns/SKILL.md +772 -27
  71. package/package.json +1 -1
@@ -1,13 +1,21 @@
1
1
  ---
2
2
  name: twilio-communications
3
- description: "Basic pattern for sending SMS messages with Twilio. Handles the fundamentals: phone number formatting, message delivery, and delivery status callbacks."
3
+ description: "Build communication features with Twilio: SMS messaging, voice
4
+ calls, WhatsApp Business API, and user verification (2FA). Covers the full
5
+ spectrum from simple notifications to complex IVR systems and multi-channel
6
+ authentication."
4
7
  risk: unknown
5
- source: "vibeship-spawner-skills (Apache 2.0)"
6
- date_added: "2026-02-27"
8
+ source: vibeship-spawner-skills (Apache 2.0)
9
+ date_added: 2026-02-27
7
10
  ---
8
11
 
9
12
  # Twilio Communications
10
13
 
14
+ Build communication features with Twilio: SMS messaging, voice calls,
15
+ WhatsApp Business API, and user verification (2FA). Covers the full
16
+ spectrum from simple notifications to complex IVR systems and multi-channel
17
+ authentication. Critical focus on compliance, rate limits, and error handling.
18
+
11
19
  ## Patterns
12
20
 
13
21
  ### SMS Sending Pattern
@@ -22,10 +30,8 @@ Key considerations:
22
30
  - Messages over 160 characters are split (and cost more)
23
31
  - Carrier filtering can block messages (especially to US numbers)
24
32
 
33
+ **When to use**: Sending notifications to users,Transactional messages (order confirmations, shipping),Alerts and reminders
25
34
 
26
- **When to use**: ['Sending notifications to users', 'Transactional messages (order confirmations, shipping)', 'Alerts and reminders']
27
-
28
- ```python
29
35
  from twilio.rest import Client
30
36
  from twilio.base.exceptions import TwilioRestException
31
37
  import os
@@ -95,8 +101,39 @@ class TwilioSMS:
95
101
  except TwilioRestException as e:
96
102
  return self._handle_error(e)
97
103
 
98
- def _handle_error(self, error: Twilio
99
- ```
104
+ def _handle_error(self, error: TwilioRestException) -> dict:
105
+ """Handle Twilio-specific errors."""
106
+ error_handlers = {
107
+ 21610: "Recipient has opted out. They must reply START.",
108
+ 21614: "Invalid 'To' phone number format.",
109
+ 21211: "'From' phone number is not valid.",
110
+ 30003: "Phone is unreachable (off, airplane mode, no signal).",
111
+ 30005: "Unknown destination (invalid number or landline).",
112
+ 30006: "Landline or unreachable carrier.",
113
+ 30429: "Rate limit exceeded. Implement exponential backoff.",
114
+ }
115
+
116
+ return {
117
+ "success": False,
118
+ "error_code": error.code,
119
+ "error": error_handlers.get(error.code, error.msg),
120
+ "details": str(error)
121
+ }
122
+
123
+ # Usage
124
+ sms = TwilioSMS()
125
+ result = sms.send_sms(
126
+ to="+14155551234",
127
+ body="Your order #1234 has shipped!",
128
+ status_callback="https://your-app.com/webhooks/twilio/status"
129
+ )
130
+
131
+ ### Anti_patterns
132
+
133
+ - Not validating E.164 format before sending
134
+ - Hardcoding Twilio credentials in code
135
+ - Ignoring delivery status callbacks
136
+ - Not handling the opted-out (21610) error
100
137
 
101
138
  ### Twilio Verify Pattern (2FA/OTP)
102
139
 
@@ -112,10 +149,8 @@ Key benefits over DIY OTP:
112
149
  Google found SMS 2FA blocks "100% of automated bots, 96% of bulk
113
150
  phishing attacks, and 76% of targeted attacks."
114
151
 
152
+ **When to use**: User phone number verification at signup,Two-factor authentication (2FA),Password reset verification,High-value transaction confirmation
115
153
 
116
- **When to use**: ['User phone number verification at signup', 'Two-factor authentication (2FA)', 'Password reset verification', 'High-value transaction confirmation']
117
-
118
- ```python
119
154
  from twilio.rest import Client
120
155
  from twilio.base.exceptions import TwilioRestException
121
156
  import os
@@ -188,8 +223,88 @@ class TwilioVerify:
188
223
  to: Phone number or email that received code
189
224
  code: The code entered by user
190
225
 
191
- R
192
- ```
226
+ Returns:
227
+ Verification result
228
+ """
229
+ try:
230
+ check = self.client.verify \
231
+ .v2 \
232
+ .services(self.service_sid) \
233
+ .verification_checks \
234
+ .create(
235
+ to=to,
236
+ code=code
237
+ )
238
+
239
+ return {
240
+ "success": True,
241
+ "valid": check.status == "approved",
242
+ "status": check.status # "approved" or "pending"
243
+ }
244
+
245
+ except TwilioRestException as e:
246
+ # Code was wrong or expired
247
+ return {
248
+ "success": False,
249
+ "valid": False,
250
+ "error": str(e)
251
+ }
252
+
253
+ def _handle_verify_error(self, error: TwilioRestException) -> dict:
254
+ """Handle Verify-specific errors."""
255
+ error_handlers = {
256
+ 60200: "Invalid phone number format",
257
+ 60203: "Max send attempts reached for this number",
258
+ 60205: "Service not found - check VERIFY_SID",
259
+ 60223: "Failed to create verification - carrier rejected",
260
+ }
261
+
262
+ return {
263
+ "success": False,
264
+ "error_code": error.code,
265
+ "error": error_handlers.get(error.code, error.msg)
266
+ }
267
+
268
+ # Usage Example - Signup Flow
269
+ verify = TwilioVerify()
270
+
271
+ # Step 1: User enters phone number
272
+ result = verify.send_verification("+14155551234", VerifyChannel.SMS)
273
+ if result["success"]:
274
+ print("Code sent! Check your phone.")
275
+
276
+ # Step 2: User enters the code they received
277
+ code = "123456" # From user input
278
+ check = verify.check_verification("+14155551234", code)
279
+
280
+ if check["valid"]:
281
+ print("Phone verified! Create account.")
282
+ else:
283
+ print("Invalid code. Try again.")
284
+
285
+ # Best Practice: Offer voice fallback
286
+ async def verify_with_fallback(phone: str, max_attempts: int = 3):
287
+ """Verify with voice fallback if SMS fails."""
288
+ for attempt in range(max_attempts):
289
+ channel = VerifyChannel.SMS if attempt == 0 else VerifyChannel.CALL
290
+ result = verify.send_verification(phone, channel)
291
+
292
+ if result["success"]:
293
+ return result
294
+
295
+ # If SMS failed, wait and try voice
296
+ if channel == VerifyChannel.SMS:
297
+ await asyncio.sleep(30)
298
+ continue
299
+
300
+ return {"success": False, "error": "All verification attempts failed"}
301
+
302
+ ### Anti_patterns
303
+
304
+ - Storing OTP codes in your database (Twilio handles this)
305
+ - Not implementing rate limiting on your verify endpoint
306
+ - Using same-code retries (let Verify generate new codes)
307
+ - No fallback channel when SMS fails
193
308
 
194
309
  ### TwiML IVR Pattern
195
310
 
@@ -208,10 +323,8 @@ Core TwiML verbs:
208
323
  Key insight: Twilio makes HTTP request to your webhook, you return
209
324
  TwiML, Twilio executes it. Stateless, so use URL params or sessions.
210
325
 
326
+ **When to use**: Phone menu systems (press 1 for sales...),Automated customer support,Appointment reminders with confirmation,Voicemail systems
211
327
 
212
- **When to use**: ['Phone menu systems (press 1 for sales...)', 'Automated customer support', 'Appointment reminders with confirmation', 'Voicemail systems']
213
-
214
- ```python
215
328
  from flask import Flask, request, Response
216
329
  from twilio.twiml.voice_response import VoiceResponse, Gather
217
330
  from twilio.request_validator import RequestValidator
@@ -281,20 +394,1189 @@ def menu_selection():
281
394
 
282
395
  elif digit == "3":
283
396
  # Voicemail
284
- response.say("Please leave a message after
397
+ response.say("Please leave a message after the beep.")
398
+ response.record(
399
+ action="/voice/voicemail-saved",
400
+ max_length=120,
401
+ transcribe=True,
402
+ transcribe_callback="/voice/transcription"
403
+ )
404
+
405
+ else:
406
+ response.say("Invalid selection.")
407
+ response.redirect("/voice/incoming")
408
+
409
+ return Response(str(response), mimetype="text/xml")
410
+
411
+ @app.route("/voice/voicemail-saved", methods=["POST"])
412
+ @validate_twilio_request
413
+ def voicemail_saved():
414
+ """Handle saved voicemail."""
415
+ response = VoiceResponse()
416
+
417
+ recording_url = request.form.get("RecordingUrl")
418
+ recording_sid = request.form.get("RecordingSid")
419
+
420
+ # Save to database, notify team, etc.
421
+ print(f"Voicemail saved: {recording_url}")
422
+
423
+ response.say("Thank you. Goodbye.")
424
+ response.hangup()
425
+
426
+ return Response(str(response), mimetype="text/xml")
427
+
428
+ @app.route("/voice/transcription", methods=["POST"])
429
+ @validate_twilio_request
430
+ def transcription_callback():
431
+ """Handle voicemail transcription."""
432
+ transcription = request.form.get("TranscriptionText")
433
+ recording_sid = request.form.get("RecordingSid")
434
+
435
+ # Save transcription, send to Slack, etc.
436
+ print(f"Transcription: {transcription}")
437
+
438
+ return "", 200
439
+
440
+ # Outbound call example
441
+ from twilio.rest import Client
442
+
443
+ def make_outbound_call(to: str, message: str):
444
+ """Make outbound call with custom TwiML."""
445
+ client = Client(
446
+ os.environ["TWILIO_ACCOUNT_SID"],
447
+ os.environ["TWILIO_AUTH_TOKEN"]
448
+ )
449
+
450
+ # TwiML Bin URL or your endpoint
451
+ call = client.calls.create(
452
+ to=to,
453
+ from_=os.environ["TWILIO_PHONE_NUMBER"],
454
+ url="https://your-app.com/voice/outbound-message",
455
+ status_callback="https://your-app.com/voice/status"
456
+ )
457
+
458
+ return call.sid
459
+
460
+ if __name__ == "__main__":
461
+ app.run(debug=True)
462
+
463
+ ### Anti_patterns
464
+
465
+ - Not validating X-Twilio-Signature (security risk)
466
+ - Returning non-XML responses to Twilio
467
+ - Not handling timeout/no-input cases
468
+ - Hardcoding phone numbers in TwiML
469
+
470
+ ### WhatsApp Business API Pattern
471
+
472
+ Send and receive WhatsApp messages via Twilio API.
473
+ Uses the same Twilio Messages API as SMS with minor changes.
474
+
475
+ Key WhatsApp rules:
476
+ - 24-hour session window: Can only reply within 24 hours of user message
477
+ - Template messages: Pre-approved templates for outside session window
478
+ - Opt-in required: Users must explicitly consent to receive messages
479
+ - Rate limit: 80 MPS default (up to 400 with approval)
480
+ - Character limits: Non-template 1024 chars, templates ~550 chars
481
+
482
+ **When to use**: Customer support with rich media,Order notifications with buttons,Marketing messages (with templates),Interactive flows (booking, surveys)
483
+
484
+ from twilio.rest import Client
485
+ from twilio.base.exceptions import TwilioRestException
486
+ import os
487
+ from datetime import datetime, timedelta
488
+ from typing import Optional
489
+
490
+ class TwilioWhatsApp:
491
+ """
492
+ WhatsApp Business API via Twilio.
493
+ Handles session windows and template messages.
494
+ """
495
+
496
+ def __init__(self):
497
+ self.client = Client(
498
+ os.environ["TWILIO_ACCOUNT_SID"],
499
+ os.environ["TWILIO_AUTH_TOKEN"]
500
+ )
501
+ # WhatsApp number format: whatsapp:+14155551234
502
+ self.from_number = os.environ["TWILIO_WHATSAPP_NUMBER"]
503
+
504
+ def send_message(
505
+ self,
506
+ to: str,
507
+ body: str,
508
+ media_url: Optional[str] = None
509
+ ) -> dict:
510
+ """
511
+ Send WhatsApp message within 24-hour session.
512
+
513
+ Args:
514
+ to: Recipient number (E.164, without whatsapp: prefix)
515
+ body: Message text (max 1024 chars for non-template)
516
+ media_url: Optional image/document URL
517
+
518
+ Returns:
519
+ Message result
520
+ """
521
+ # Format for WhatsApp
522
+ to_whatsapp = f"whatsapp:{to}"
523
+ from_whatsapp = f"whatsapp:{self.from_number}"
524
+
525
+ try:
526
+ message_params = {
527
+ "to": to_whatsapp,
528
+ "from_": from_whatsapp,
529
+ "body": body
530
+ }
531
+
532
+ if media_url:
533
+ message_params["media_url"] = [media_url]
534
+
535
+ message = self.client.messages.create(**message_params)
536
+
537
+ return {
538
+ "success": True,
539
+ "message_sid": message.sid,
540
+ "status": message.status
541
+ }
542
+
543
+ except TwilioRestException as e:
544
+ return self._handle_whatsapp_error(e)
545
+
546
+ def send_template_message(
547
+ self,
548
+ to: str,
549
+ content_sid: str,
550
+ content_variables: dict
551
+ ) -> dict:
552
+ """
553
+ Send pre-approved template message.
554
+ Use this for messages outside 24-hour window.
555
+
556
+ Content templates must be approved by WhatsApp first.
557
+ Create them in Twilio Console > Content Template Builder.
558
+ """
559
+ to_whatsapp = f"whatsapp:{to}"
560
+ from_whatsapp = f"whatsapp:{self.from_number}"
561
+
562
+ try:
563
+ message = self.client.messages.create(
564
+ to=to_whatsapp,
565
+ from_=from_whatsapp,
566
+ content_sid=content_sid,
567
+ content_variables=content_variables
568
+ )
569
+
570
+ return {
571
+ "success": True,
572
+ "message_sid": message.sid,
573
+ "template": True
574
+ }
575
+
576
+ except TwilioRestException as e:
577
+ return self._handle_whatsapp_error(e)
578
+
579
+ def _handle_whatsapp_error(self, error: TwilioRestException) -> dict:
580
+ """Handle WhatsApp-specific errors."""
581
+ error_handlers = {
582
+ 63016: "Outside 24-hour window. Use template message.",
583
+ 63018: "Template not approved or doesn't exist.",
584
+ 63025: "Too many template messages sent to this user.",
585
+ 63038: "Rate limit exceeded for WhatsApp.",
586
+ }
587
+
588
+ return {
589
+ "success": False,
590
+ "error_code": error.code,
591
+ "error": error_handlers.get(error.code, error.msg)
592
+ }
593
+
594
+ # Flask webhook for incoming WhatsApp messages
595
+ from flask import Flask, request
596
+
597
+ app = Flask(__name__)
598
+
599
+ @app.route("/webhooks/whatsapp", methods=["POST"])
600
+ def whatsapp_webhook():
601
+ """Handle incoming WhatsApp messages."""
602
+ from_number = request.form.get("From", "").replace("whatsapp:", "")
603
+ body = request.form.get("Body", "")
604
+ media_url = request.form.get("MediaUrl0") # First attachment
605
+
606
+ # Track session start (24-hour window begins now)
607
+ session_start = datetime.now()
608
+ session_expires = session_start + timedelta(hours=24)
609
+
610
+ # Store in database for session tracking
611
+ # user_sessions[from_number] = session_expires
612
+
613
+ # Process message and respond
614
+ response = process_whatsapp_message(from_number, body, media_url)
615
+
616
+ # Reply within session
617
+ whatsapp = TwilioWhatsApp()
618
+ whatsapp.send_message(from_number, response)
619
+
620
+ return "", 200
621
+
622
+ def process_whatsapp_message(phone: str, text: str, media: str) -> str:
623
+ """Process incoming message and generate response."""
624
+ text_lower = text.lower()
625
+
626
+ if "order status" in text_lower:
627
+ return "Your order #1234 is out for delivery!"
628
+ elif "support" in text_lower:
629
+ return "A support agent will contact you shortly."
630
+ else:
631
+ return "Thanks for your message! Reply with 'order status' or 'support'."
632
+
633
+ # Send typing indicator (2025 feature)
634
+ def send_typing_indicator(to: str):
635
+ """Let user know you're typing."""
636
+ # Requires Senders API setup
637
+ pass
638
+
639
+ ### Anti_patterns
640
+
641
+ - Sending non-template messages outside 24-hour window
642
+ - Not tracking session windows per user
643
+ - Exceeding 1024 char limit for session messages
644
+ - Not handling template rejection errors
645
+
646
+ ### Webhook Handler Pattern
647
+
648
+ Handle Twilio webhooks for delivery status, incoming messages,
649
+ and call events. Critical: always validate X-Twilio-Signature.
650
+
651
+ Twilio sends webhooks for:
652
+ - Message status updates (queued → sent → delivered/failed)
653
+ - Incoming SMS/WhatsApp messages
654
+ - Call events (initiated, ringing, answered, completed)
655
+ - Recording/transcription ready
656
+
657
+ **When to use**: Tracking message delivery status,Receiving incoming messages,Call analytics and logging,Voicemail transcription processing
658
+
659
+ from flask import Flask, request, abort
660
+ from twilio.request_validator import RequestValidator
661
+ from functools import wraps
662
+ import os
663
+ import logging
664
+
665
+ app = Flask(__name__)
666
+ logger = logging.getLogger(__name__)
667
+
668
+ def validate_twilio_signature(f):
669
+ """
670
+ Validate that request came from Twilio.
671
+ CRITICAL: Always use this for webhook endpoints.
672
+ """
673
+ @wraps(f)
674
+ def wrapper(*args, **kwargs):
675
+ validator = RequestValidator(os.environ["TWILIO_AUTH_TOKEN"])
676
+
677
+ # Build full URL (including query params)
678
+ url = request.url
679
+
680
+ # Get POST body as dict
681
+ params = request.form.to_dict()
682
+
683
+ # Get signature from header
684
+ signature = request.headers.get("X-Twilio-Signature", "")
685
+
686
+ if not validator.validate(url, params, signature):
687
+ logger.warning(f"Invalid Twilio signature from {request.remote_addr}")
688
+ abort(403)
689
+
690
+ return f(*args, **kwargs)
691
+ return wrapper
692
+
693
+ @app.route("/webhooks/twilio/sms/status", methods=["POST"])
694
+ @validate_twilio_signature
695
+ def sms_status_callback():
696
+ """
697
+ Handle SMS delivery status updates.
698
+
699
+ Status progression: queued → sending → sent → delivered
700
+ Or: queued → sending → undelivered/failed
701
+ """
702
+ message_sid = request.form.get("MessageSid")
703
+ status = request.form.get("MessageStatus")
704
+ error_code = request.form.get("ErrorCode")
705
+ error_message = request.form.get("ErrorMessage")
706
+
707
+ logger.info(f"SMS {message_sid}: {status}")
708
+
709
+ if status == "delivered":
710
+ # Message successfully delivered
711
+ update_message_status(message_sid, "delivered")
712
+
713
+ elif status == "undelivered":
714
+ # Carrier rejected or other failure
715
+ logger.error(f"SMS failed: {error_code} - {error_message}")
716
+ handle_failed_message(message_sid, error_code, error_message)
717
+
718
+ elif status == "failed":
719
+ # Twilio couldn't send
720
+ logger.error(f"SMS send failed: {error_code}")
721
+ handle_failed_message(message_sid, error_code, error_message)
722
+
723
+ return "", 200
724
+
725
+ @app.route("/webhooks/twilio/sms/incoming", methods=["POST"])
726
+ @validate_twilio_signature
727
+ def incoming_sms():
728
+ """
729
+ Handle incoming SMS messages.
730
+ """
731
+ from_number = request.form.get("From")
732
+ to_number = request.form.get("To")
733
+ body = request.form.get("Body")
734
+ num_media = int(request.form.get("NumMedia", 0))
735
+
736
+ # Handle media attachments
737
+ media_urls = []
738
+ for i in range(num_media):
739
+ media_urls.append(request.form.get(f"MediaUrl{i}"))
740
+
741
+ # Check for opt-out keywords
742
+ if body.strip().upper() in ["STOP", "UNSUBSCRIBE", "CANCEL"]:
743
+ handle_opt_out(from_number)
744
+ return "", 200
745
+
746
+ # Check for opt-in keywords
747
+ if body.strip().upper() in ["START", "SUBSCRIBE"]:
748
+ handle_opt_in(from_number)
749
+ return "", 200
750
+
751
+ # Process message
752
+ process_incoming_sms(from_number, body, media_urls)
753
+
754
+ return "", 200
755
+
756
+ @app.route("/webhooks/twilio/voice/status", methods=["POST"])
757
+ @validate_twilio_signature
758
+ def voice_status_callback():
759
+ """Handle call status updates."""
760
+ call_sid = request.form.get("CallSid")
761
+ status = request.form.get("CallStatus")
762
+ duration = request.form.get("CallDuration")
763
+ direction = request.form.get("Direction")
764
+
765
+ # Call statuses: initiated, ringing, in-progress, completed, busy, no-answer, canceled, failed
766
+
767
+ logger.info(f"Call {call_sid}: {status} ({duration}s)")
768
+
769
+ if status == "completed":
770
+ # Call ended normally
771
+ log_call_completion(call_sid, duration)
772
+
773
+ elif status in ["busy", "no-answer", "canceled", "failed"]:
774
+ # Call didn't connect
775
+ handle_failed_call(call_sid, status)
776
+
777
+ return "", 200
778
+
779
+ # Helper functions
780
+ def update_message_status(message_sid: str, status: str):
781
+ """Update message status in database."""
782
+ pass
783
+
784
+ def handle_failed_message(message_sid: str, error_code: str, error_msg: str):
785
+ """Handle failed message delivery."""
786
+ # Notify team, retry logic, etc.
787
+ pass
788
+
789
+ def handle_opt_out(phone: str):
790
+ """Handle user opting out of messages."""
791
+ # Mark user as opted out in database
792
+ # IMPORTANT: Must respect this!
793
+ pass
794
+
795
+ def handle_opt_in(phone: str):
796
+ """Handle user opting back in."""
797
+ pass
798
+
799
+ def process_incoming_sms(from_phone: str, body: str, media: list):
800
+ """Process incoming SMS message."""
801
+ pass
802
+
803
+ def log_call_completion(call_sid: str, duration: str):
804
+ """Log completed call."""
805
+ pass
806
+
807
+ def handle_failed_call(call_sid: str, status: str):
808
+ """Handle call that didn't connect."""
809
+ pass
810
+
811
+ ### Anti_patterns
812
+
813
+ - Not validating X-Twilio-Signature
814
+ - Exposing webhook URLs without authentication
815
+ - Not handling opt-out keywords (STOP)
816
+ - Blocking webhook response (should be fast)
817
+
818
+ ### Rate Limit and Retry Pattern
819
+
820
+ Handle Twilio rate limits and implement proper retry logic.
821
+
822
+ Default limits:
823
+ - SMS: 80 messages per second (MPS)
824
+ - Voice: Varies by number type and region
825
+ - API calls: 100 requests per second
826
+
827
+ Error codes:
828
+ - 20429: Voice API rate limit
829
+ - 30429: Messaging API rate limit
830
+
831
+ **When to use**: High-volume messaging applications,Bulk SMS campaigns,Automated calling systems
832
+
833
+ import time
834
+ import random
835
+ from functools import wraps
836
+ from twilio.base.exceptions import TwilioRestException
837
+ import logging
838
+
839
+ logger = logging.getLogger(__name__)
840
+
841
+ def exponential_backoff_retry(
842
+ max_retries: int = 5,
843
+ base_delay: float = 1.0,
844
+ max_delay: float = 60.0,
845
+ rate_limit_codes: list = [20429, 30429]
846
+ ):
847
+ """
848
+ Decorator for exponential backoff retry on rate limits.
849
+
850
+ Uses jitter to prevent thundering herd.
851
+ """
852
+ def decorator(func):
853
+ @wraps(func)
854
+ def wrapper(*args, **kwargs):
855
+ last_exception = None
856
+
857
+ for attempt in range(max_retries + 1):
858
+ try:
859
+ return func(*args, **kwargs)
860
+
861
+ except TwilioRestException as e:
862
+ last_exception = e
863
+
864
+ # Only retry on rate limit errors
865
+ if e.code not in rate_limit_codes:
866
+ raise
867
+
868
+ if attempt == max_retries:
869
+ logger.error(f"Max retries exceeded: {e}")
870
+ raise
871
+
872
+ # Calculate delay with jitter
873
+ delay = min(
874
+ base_delay * (2 ** attempt) + random.uniform(0, 1),
875
+ max_delay
876
+ )
877
+
878
+ logger.warning(
879
+ f"Rate limited (attempt {attempt + 1}/{max_retries}). "
880
+ f"Retrying in {delay:.1f}s"
881
+ )
882
+ time.sleep(delay)
883
+
884
+ raise last_exception
885
+
886
+ return wrapper
887
+ return decorator
888
+
889
+ # Usage
890
+ from twilio.rest import Client
891
+
892
+ client = Client(account_sid, auth_token)
893
+
894
+ @exponential_backoff_retry(max_retries=5)
895
+ def send_sms(to: str, body: str):
896
+ return client.messages.create(
897
+ to=to,
898
+ from_=from_number,
899
+ body=body
900
+ )
901
+
902
+ # Bulk sending with rate limiting
903
+ import asyncio
904
+ from asyncio import Semaphore
905
+
906
+ class RateLimitedSender:
907
+ """
908
+ Send messages with built-in rate limiting.
909
+ Stays under Twilio's 80 MPS limit.
910
+ """
911
+
912
+ def __init__(self, client, from_number: str, mps: int = 50):
913
+ self.client = client
914
+ self.from_number = from_number
915
+ self.mps = mps
916
+ self.semaphore = Semaphore(mps)
917
+
918
+ async def send_bulk(self, messages: list[dict]) -> list[dict]:
919
+ """
920
+ Send messages with rate limiting.
921
+
922
+ Args:
923
+ messages: List of {"to": "+1...", "body": "..."}
924
+
925
+ Returns:
926
+ Results for each message
927
+ """
928
+ tasks = [
929
+ self._send_with_limit(msg["to"], msg["body"])
930
+ for msg in messages
931
+ ]
932
+
933
+ return await asyncio.gather(*tasks, return_exceptions=True)
934
+
935
+ async def _send_with_limit(self, to: str, body: str):
936
+ """Send single message with semaphore-based rate limit."""
937
+ async with self.semaphore:
938
+ try:
939
+ # Use sync client in thread pool
940
+ loop = asyncio.get_event_loop()
941
+ result = await loop.run_in_executor(
942
+ None,
943
+ lambda: self.client.messages.create(
944
+ to=to,
945
+ from_=self.from_number,
946
+ body=body
947
+ )
948
+ )
949
+ return {"success": True, "sid": result.sid, "to": to}
950
+
951
+ except TwilioRestException as e:
952
+ return {"success": False, "error": str(e), "to": to}
953
+
954
+ finally:
955
+ # Delay to maintain rate limit
956
+ await asyncio.sleep(1 / self.mps)
957
+
958
+ # Usage
959
+ async def send_campaign():
960
+ sender = RateLimitedSender(client, from_number, mps=50)
961
+
962
+ messages = [
963
+ {"to": "+14155551234", "body": "Hello!"},
964
+ {"to": "+14155555678", "body": "Hello!"},
965
+ # ... thousands of messages
966
+ ]
967
+
968
+ results = await sender.send_bulk(messages)
969
+
970
+ successful = sum(1 for r in results if r.get("success"))
971
+ print(f"Sent {successful}/{len(messages)} messages")
972
+
973
+ ### Anti_patterns
974
+
975
+ - Retrying immediately without backoff
976
+ - No jitter causing thundering herd
977
+ - Retrying non-rate-limit errors
978
+ - Exceeding Twilio's MPS limit
979
+
980
+ ## Sharp Edges
981
+
982
+ ### Sending to Users Who Opted Out (Error 21610)
983
+
984
+ Severity: HIGH
985
+
986
+ Situation: Sending SMS to a phone number
987
+
988
+ Symptoms:
989
+ Message fails with error code 21610. Twilio rejects the message.
990
+ User never receives the SMS. Same number worked before.
991
+
992
+ Why this breaks:
993
+ The recipient replied "STOP" (or UNSUBSCRIBE, CANCEL, etc.) to a previous
994
+ message from your number. Twilio automatically honors opt-outs and blocks
995
+ further messages to that number from your account.
996
+
997
+ This is legally required for US messaging (TCPA, CTIA guidelines).
998
+ You cannot override this - the user must reply "START" to opt back in.
999
+
1000
+ Recommended fix:
1001
+
1002
+ ## Track opt-out status in your database
1003
+
1004
+ ```python
1005
+ # In your webhook handler
1006
+ @app.route("/webhooks/sms/incoming", methods=["POST"])
1007
+ def incoming_sms():
1008
+ from_number = request.form.get("From")
1009
+ body = request.form.get("Body", "").strip().upper()
1010
+
1011
+ # Standard opt-out keywords
1012
+ if body in ["STOP", "UNSUBSCRIBE", "CANCEL", "END", "QUIT"]:
1013
+ mark_user_opted_out(from_number)
1014
+ return "", 200
1015
+
1016
+ # Standard opt-in keywords
1017
+ if body in ["START", "SUBSCRIBE", "YES", "UNSTOP"]:
1018
+ mark_user_opted_in(from_number)
1019
+ return "", 200
1020
+
1021
+ # Process other messages...
1022
+
1023
+ # Before sending
1024
+ def send_sms_safe(to: str, body: str):
1025
+ if is_user_opted_out(to):
1026
+ return {"success": False, "error": "User has opted out"}
1027
+
1028
+ try:
1029
+ return send_sms(to, body)
1030
+ except TwilioRestException as e:
1031
+ if e.code == 21610:
1032
+ # Update database - they opted out via carrier
1033
+ mark_user_opted_out(to)
1034
+ raise
1035
+ ```
1036
+
1037
+ ## Include opt-out instructions
1038
+ Add "Reply STOP to unsubscribe" to marketing messages.
1039
+
1040
+ ### Phone Unreachable But Valid (Error 30003)
1041
+
1042
+ Severity: MEDIUM
1043
+
1044
+ Situation: Sending SMS to a mobile number
1045
+
1046
+ Symptoms:
1047
+ Message fails with error 30003. Number was valid and worked before.
1048
+ Intermittent - sometimes works, sometimes fails.
1049
+
1050
+ Why this breaks:
1051
+ Error 30003 means "Unreachable destination handset." The phone exists but
1052
+ can't receive messages right now. Common causes:
1053
+ - Phone powered off
1054
+ - Airplane mode
1055
+ - Out of signal range
1056
+ - Carrier network issues
1057
+ - Phone storage full
1058
+
1059
+ Unlike 30006 (permanent unreachable), 30003 is usually temporary.
1060
+
1061
+ Recommended fix:
1062
+
1063
+ ## Implement retry logic for transient failures
1064
+
1065
+ ```python
1066
+ TRANSIENT_ERRORS = [30003, 30008, 30009] # Retriable errors
1067
+
1068
+ async def send_with_retry(to: str, body: str, max_retries: int = 3):
1069
+ for attempt in range(max_retries):
1070
+ result = send_sms(to, body)
1071
+
1072
+ if result["success"]:
1073
+ return result
1074
+
1075
+ if result.get("error_code") not in TRANSIENT_ERRORS:
1076
+ # Don't retry permanent failures
1077
+ return result
1078
+
1079
+ # Exponential backoff: 5min, 15min, 45min
1080
+ delay = 300 * (3 ** attempt)
1081
+ await asyncio.sleep(delay)
1082
+
1083
+ return {"success": False, "error": "Max retries exceeded"}
1084
+ ```
1085
+
1086
+ ## Provide fallback channel
1087
+
1088
+ ```python
1089
+ async def notify_user(user, message):
1090
+ # Try SMS first
1091
+ result = await send_sms(user.phone, message)
1092
+
1093
+ if result.get("error_code") == 30003:
1094
+ # Phone unreachable - try email
1095
+ await send_email(user.email, message)
1096
+ return {"channel": "email", "status": "sent"}
1097
+
1098
+ return {"channel": "sms", "status": result["status"]}
1099
+ ```
1100
+
1101
+ ### Messages Blocked by Carrier Filtering
1102
+
1103
+ Severity: HIGH
1104
+
1105
+ Situation: Sending SMS to US phone numbers
1106
+
1107
+ Symptoms:
1108
+ Messages show as "sent" but never "delivered." No error from Twilio.
1109
+ Users say they never received the message. Pattern in specific carriers
1110
+ or message content.
1111
+
1112
+ Why this breaks:
1113
+ US carriers (Verizon, AT&T, T-Mobile) aggressively filter SMS for spam.
1114
+ Your message might be blocked if:
1115
+ - Contains URLs (especially short URLs or unknown domains)
1116
+ - Looks like phishing (urgent, account, verify, click now)
1117
+ - High volume from same number
1118
+ - Not using registered A2P 10DLC
1119
+ - Low sender reputation
1120
+
1121
+ Carriers don't tell Twilio why messages are filtered - they just
1122
+ silently drop them.
1123
+
1124
+ Recommended fix:
1125
+
1126
+ ## Register for A2P 10DLC (US requirement)
1127
+
1128
+ ```
1129
+ 1. Go to Twilio Console > Messaging > Trust Hub
1130
+ 2. Register your business brand
1131
+ 3. Create a messaging campaign (describes use case)
1132
+ 4. Wait for approval (can take days)
1133
+ 5. Associate phone numbers with campaign
1134
+ ```
1135
+
1136
+ ## Message content best practices
1137
+
1138
+ ```python
1139
+ def sanitize_message(text: str) -> str:
1140
+ """Make message less likely to be filtered."""
1141
+ # Avoid URL shorteners - use full domain
1142
+ # Avoid spam trigger words
1143
+ # Keep it conversational, not promotional
1144
+
1145
+ # Example: Instead of this
1146
+ bad = "URGENT: Verify your account now! Click: bit.ly/abc"
1147
+
1148
+ # Do this
1149
+ good = "Hi! Your order #1234 is ready. Questions? Reply here."
1150
+
1151
+ return text
1152
+
1153
+ # Use toll-free or short code for high volume
1154
+ # 10DLC is for <10K msg/day
1155
+ # Toll-free: up to 10K msg/day
1156
+ # Short code: 100K+ msg/day
1157
+ ```
1158
+
1159
+ ## Monitor delivery rates
1160
+
1161
+ ```python
1162
+ def track_delivery_rate():
1163
+ sent = get_messages_with_status("sent")
1164
+ delivered = get_messages_with_status("delivered")
1165
+
1166
+ rate = len(delivered) / len(sent) * 100
1167
+
1168
+ if rate < 95:
1169
+ alert_team(f"Delivery rate dropped to {rate}%")
1170
+ ```
1171
+
1172
+ ### Not Validating Webhook Signatures
1173
+
1174
+ Severity: CRITICAL
1175
+
1176
+ Situation: Receiving Twilio webhook callbacks
1177
+
1178
+ Symptoms:
1179
+ Attackers send fake webhooks to your endpoint. Fraudulent transactions
1180
+ processed. Spoofed incoming messages trigger actions.
1181
+
1182
+ Why this breaks:
1183
+ Twilio signs all webhook requests with X-Twilio-Signature header.
1184
+ If you don't validate this, anyone who knows your webhook URL can
1185
+ send fake requests pretending to be Twilio.
1186
+
1187
+ This can lead to:
1188
+ - Fake message delivery confirmations
1189
+ - Spoofed incoming messages
1190
+ - Fraudulent verification approvals
1191
+
1192
+ Recommended fix:
1193
+
1194
+ ## ALWAYS validate the signature
1195
+
1196
+ ```python
1197
+ from twilio.request_validator import RequestValidator
1198
+ from flask import Flask, request, abort
1199
+ from functools import wraps
1200
+ import os
1201
+
1202
+ def require_twilio_signature(f):
1203
+ """Decorator to validate Twilio webhook requests."""
1204
+ @wraps(f)
1205
+ def wrapper(*args, **kwargs):
1206
+ validator = RequestValidator(os.environ["TWILIO_AUTH_TOKEN"])
1207
+
1208
+ # Full URL including query string
1209
+ url = request.url
1210
+
1211
+ # POST body as dict
1212
+ params = request.form.to_dict()
1213
+
1214
+ # Signature header
1215
+ signature = request.headers.get("X-Twilio-Signature", "")
1216
+
1217
+ if not validator.validate(url, params, signature):
1218
+ abort(403)
1219
+
1220
+ return f(*args, **kwargs)
1221
+ return wrapper
1222
+
1223
+ @app.route("/webhooks/twilio", methods=["POST"])
1224
+ @require_twilio_signature # ALWAYS use this
1225
+ def twilio_webhook():
1226
+ # Safe to process
1227
+ pass
1228
+ ```
1229
+
1230
+ ## Common validation gotchas
1231
+
1232
+ ```python
1233
+ # URL must match EXACTLY what Twilio called
1234
+ # If behind proxy, you might need:
1235
+ url = request.headers.get("X-Forwarded-Proto", "http") + "://" + \
1236
+ request.headers.get("X-Forwarded-Host", request.host) + \
1237
+ request.path
1238
+
1239
+ # If using ngrok, URL changes each restart
1240
+ # Use consistent URL in production
1241
+ ```
1242
+
1243
+ ### WhatsApp Message Outside 24-Hour Window (Error 63016)
1244
+
1245
+ Severity: HIGH
1246
+
1247
+ Situation: Sending WhatsApp message to a user
1248
+
1249
+ Symptoms:
1250
+ Message fails with error 63016. "Message is outside the allowed window."
1251
+ Template messages work, but regular messages fail.
1252
+
1253
+ Why this breaks:
1254
+ WhatsApp has strict rules about unsolicited messages:
1255
+ - Users must message you first
1256
+ - You can only reply within 24 hours of their last message
1257
+ - After 24 hours, you must use pre-approved template messages
1258
+
1259
+ This prevents spam and maintains WhatsApp's trust as a platform.
1260
+
1261
+ Recommended fix:
1262
+
1263
+ ## Track session windows per user
1264
+
1265
+ ```python
1266
+ from datetime import datetime, timedelta
1267
+
1268
+ class WhatsAppSession:
1269
+ def __init__(self, redis_client):
1270
+ self.redis = redis_client
1271
+ self.window_hours = 24
1272
+
1273
+ def start_session(self, phone: str):
1274
+ """Start/refresh 24-hour session on incoming message."""
1275
+ key = f"wa_session:{phone}"
1276
+ expires = datetime.now() + timedelta(hours=self.window_hours)
1277
+ self.redis.set(key, expires.isoformat(), ex=self.window_hours * 3600)
1278
+
1279
+ def can_send_freeform(self, phone: str) -> bool:
1280
+ """Check if we can send non-template message."""
1281
+ key = f"wa_session:{phone}"
1282
+ expires_str = self.redis.get(key)
1283
+
1284
+ if not expires_str:
1285
+ return False
1286
+
1287
+ expires = datetime.fromisoformat(expires_str)
1288
+ return datetime.now() < expires
1289
+
1290
+ def send_message(self, phone: str, body: str, template_sid: str = None):
1291
+ """Send message, using template if outside window."""
1292
+ if self.can_send_freeform(phone):
1293
+ return send_whatsapp_message(phone, body)
1294
+ elif template_sid:
1295
+ return send_whatsapp_template(phone, template_sid)
1296
+ else:
1297
+ return {
1298
+ "success": False,
1299
+ "error": "Outside session window, template required"
1300
+ }
1301
+ ```
1302
+
1303
+ ## Incoming message webhook
1304
+
1305
+ ```python
1306
+ @app.route("/webhooks/whatsapp", methods=["POST"])
1307
+ def whatsapp_incoming():
1308
+ from_phone = request.form.get("From").replace("whatsapp:", "")
1309
+
1310
+ # Start/refresh session
1311
+ session.start_session(from_phone)
1312
+
1313
+ # Process message...
1314
+ ```
1315
+
1316
+ ## Create approved templates for common messages
1317
+
285
1318
  ```
1319
+ 1. Twilio Console > Content Template Builder
1320
+ 2. Create template with {{1}} placeholders
1321
+ 3. Submit for WhatsApp approval (takes 24-48 hours)
1322
+ 4. Use content_sid to send
1323
+ ```
1324
+
1325
+ ### Exposed Account SID or Auth Token
1326
+
1327
+ Severity: CRITICAL
1328
+
1329
+ Situation: Deploying Twilio integration
1330
+
1331
+ Symptoms:
1332
+ Unauthorized charges on Twilio account. Messages sent you didn't send.
1333
+ Phone numbers purchased without authorization.
1334
+
1335
+ Why this breaks:
1336
+ If attackers get your Account SID + Auth Token, they have FULL access
1337
+ to your Twilio account. They can:
1338
+ - Send messages (charging your account)
1339
+ - Buy phone numbers
1340
+ - Access call recordings
1341
+ - Modify your configuration
1342
+
1343
+ Common exposure points:
1344
+ - Hardcoded in source code (pushed to GitHub)
1345
+ - In client-side JavaScript
1346
+ - In Docker images
1347
+ - In logs
1348
+
1349
+ Recommended fix:
1350
+
1351
+ ## Never hardcode credentials
1352
+
1353
+ ```python
1354
+ # BAD - never do this
1355
+ client = Client("AC1234...", "abc123...")
1356
+
1357
+ # GOOD - environment variables
1358
+ client = Client(
1359
+ os.environ["TWILIO_ACCOUNT_SID"],
1360
+ os.environ["TWILIO_AUTH_TOKEN"]
1361
+ )
1362
+
1363
+ # GOOD - secrets manager
1364
+ from aws_secretsmanager import get_secret
1365
+ creds = get_secret("twilio-credentials")
1366
+ client = Client(creds["sid"], creds["token"])
1367
+ ```
1368
+
1369
+ ## Use API Key instead of Auth Token
1370
+
1371
+ ```python
1372
+ # Auth Token has full account access
1373
+ # API Keys can be scoped and revoked
1374
+
1375
+ # Create API Key in Twilio Console
1376
+ client = Client(
1377
+ os.environ["TWILIO_API_KEY_SID"],
1378
+ os.environ["TWILIO_API_KEY_SECRET"],
1379
+ os.environ["TWILIO_ACCOUNT_SID"]
1380
+ )
1381
+
1382
+ # If compromised, revoke just that key
1383
+ ```
1384
+
1385
+ ## Rotate tokens immediately if exposed
1386
+
1387
+ ```
1388
+ 1. Twilio Console > Account > API credentials
1389
+ 2. Rotate Auth Token
1390
+ 3. Update all deployments with new token
1391
+ 4. Review account activity for unauthorized use
1392
+ ```
1393
+
1394
+ ### Verify Rate Limit Exceeded (Error 60203)
1395
+
1396
+ Severity: MEDIUM
1397
+
1398
+ Situation: Sending verification codes
1399
+
1400
+ Symptoms:
1401
+ Verification request fails with error 60203.
1402
+ "Max send attempts reached for this phone number."
1403
+
1404
+ Why this breaks:
1405
+ Twilio Verify has built-in rate limits to prevent abuse:
1406
+ - 5 verification attempts per phone number per service per 10 minutes
1407
+ - Helps prevent SMS pumping fraud
1408
+ - Protects against brute-force attacks
1409
+
1410
+ If users legitimately need more attempts, you may have UX issues.
1411
+
1412
+ Recommended fix:
1413
+
1414
+ ## Implement application-level rate limiting too
1415
+
1416
+ ```python
1417
+ from datetime import datetime, timedelta
1418
+ import redis
1419
+
1420
+ class VerifyRateLimiter:
1421
+ def __init__(self, redis_client):
1422
+ self.redis = redis_client
1423
+ # Stricter than Twilio's limit
1424
+ self.max_attempts = 3
1425
+ self.window_minutes = 10
1426
+
1427
+ def can_request(self, phone: str) -> bool:
1428
+ key = f"verify_rate:{phone}"
1429
+ attempts = self.redis.get(key)
286
1430
 
287
- ## ⚠️ Sharp Edges
1431
+ if attempts and int(attempts) >= self.max_attempts:
1432
+ return False
288
1433
 
289
- | Issue | Severity | Solution |
290
- |-------|----------|----------|
291
- | Issue | high | ## Track opt-out status in your database |
292
- | Issue | medium | ## Implement retry logic for transient failures |
293
- | Issue | high | ## Register for A2P 10DLC (US requirement) |
294
- | Issue | critical | ## ALWAYS validate the signature |
295
- | Issue | high | ## Track session windows per user |
296
- | Issue | critical | ## Never hardcode credentials |
297
- | Issue | medium | ## Implement application-level rate limiting too |
1434
+ return True
1435
+
1436
+ def record_attempt(self, phone: str):
1437
+ key = f"verify_rate:{phone}"
1438
+ pipe = self.redis.pipeline()
1439
+ pipe.incr(key)
1440
+ pipe.expire(key, self.window_minutes * 60)
1441
+ pipe.execute()
1442
+
1443
+ def get_wait_time(self, phone: str) -> int:
1444
+ """Return seconds until user can request again."""
1445
+ key = f"verify_rate:{phone}"
1446
+ ttl = self.redis.ttl(key)
1447
+ return max(0, ttl)
1448
+
1449
+ # Usage
1450
+ limiter = VerifyRateLimiter(redis_client)
1451
+
1452
+ @app.route("/verify/send", methods=["POST"])
1453
+ def send_verification():
1454
+ phone = request.json["phone"]
1455
+
1456
+ if not limiter.can_request(phone):
1457
+ wait = limiter.get_wait_time(phone)
1458
+ return {
1459
+ "error": f"Too many attempts. Try again in {wait} seconds."
1460
+ }, 429
1461
+
1462
+ result = twilio_verify.send_verification(phone)
1463
+
1464
+ if result["success"]:
1465
+ limiter.record_attempt(phone)
1466
+
1467
+ return result
1468
+ ```
1469
+
1470
+ ## Provide clear user feedback
1471
+
1472
+ ```python
1473
+ # Show remaining attempts
1474
+ # Show countdown timer
1475
+ # Offer alternative (voice call, email)
1476
+ ```
1477
+
1478
+ ## Validation Checks
1479
+
1480
+ ### Hardcoded Twilio Credentials
1481
+
1482
+ Severity: ERROR
1483
+
1484
+ Twilio credentials must never be hardcoded
1485
+
1486
+ Message: Hardcoded Twilio SID detected. Use environment variables.
1487
+
1488
+ ### Auth Token in Source Code
1489
+
1490
+ Severity: ERROR
1491
+
1492
+ Auth tokens should be in environment variables
1493
+
1494
+ Message: Hardcoded auth token. Use os.environ['TWILIO_AUTH_TOKEN'].
1495
+
1496
+ ### Webhook Without Signature Validation
1497
+
1498
+ Severity: ERROR
1499
+
1500
+ Twilio webhooks must validate X-Twilio-Signature
1501
+
1502
+ Message: Webhook without signature validation. Add RequestValidator check.
1503
+
1504
+ ### Twilio Credentials in Client-Side Code
1505
+
1506
+ Severity: ERROR
1507
+
1508
+ Never expose Twilio credentials to browsers
1509
+
1510
+ Message: Twilio credentials exposed client-side. Only use server-side.
1511
+
1512
+ ### No E.164 Phone Number Validation
1513
+
1514
+ Severity: WARNING
1515
+
1516
+ Phone numbers should be validated before sending
1517
+
1518
+ Message: Sending to phone without E.164 validation.
1519
+
1520
+ ### Hardcoded Phone Numbers
1521
+
1522
+ Severity: WARNING
1523
+
1524
+ Phone numbers should come from config or database
1525
+
1526
+ Message: Hardcoded phone number. Use config or environment variable.
1527
+
1528
+ ### No Twilio Exception Handling
1529
+
1530
+ Severity: WARNING
1531
+
1532
+ Twilio calls should handle TwilioRestException
1533
+
1534
+ Message: Twilio API call without error handling. Catch TwilioRestException.
1535
+
1536
+ ### Not Handling Specific Error Codes
1537
+
1538
+ Severity: INFO
1539
+
1540
+ Handle common Twilio error codes specifically
1541
+
1542
+ Message: Consider handling specific error codes (21610, 30003, etc.).
1543
+
1544
+ ### No Opt-Out Keyword Handling
1545
+
1546
+ Severity: WARNING
1547
+
1548
+ SMS systems must handle STOP/UNSUBSCRIBE keywords
1549
+
1550
+ Message: No opt-out handling. Check for STOP/UNSUBSCRIBE keywords.
1551
+
1552
+ ### Not Checking Opt-Out Before Sending
1553
+
1554
+ Severity: WARNING
1555
+
1556
+ Check if user has opted out before sending SMS
1557
+
1558
+ Message: Consider checking opt-out status before sending.
1559
+
1560
+ ## Collaboration
1561
+
1562
+ ### Delegation Triggers
1563
+
1564
+ - user needs AI voice assistant -> voice-agents (Twilio provides telephony, voice-agents skill for AI conversation)
1565
+ - user needs Slack notifications -> slack-bot-builder (Integrate SMS alerts with Slack notifications)
1566
+ - user needs full auth system -> auth-specialist (Twilio Verify is one component of broader auth)
1567
+ - user needs workflow automation -> workflow-automation (Trigger SMS/calls from automated workflows)
1568
+ - user needs high-volume messaging -> devops (Scale webhooks, monitor delivery rates)
298
1569
 
299
1570
  ## When to Use
300
- This skill is applicable to execute the workflow or actions described in the overview.
1571
+
1572
+ - User mentions or implies: twilio
1573
+ - User mentions or implies: send SMS
1574
+ - User mentions or implies: text message
1575
+ - User mentions or implies: voice call
1576
+ - User mentions or implies: phone verification
1577
+ - User mentions or implies: 2FA SMS
1578
+ - User mentions or implies: WhatsApp API
1579
+ - User mentions or implies: programmable messaging
1580
+ - User mentions or implies: IVR system
1581
+ - User mentions or implies: TwiML
1582
+ - User mentions or implies: phone number verification