@warriorteam/messenger-sdk 1.3.0 → 1.4.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/dist/index.d.ts CHANGED
@@ -336,32 +336,114 @@ declare class Messenger {
336
336
  }
337
337
 
338
338
  /**
339
- * Facebook Messenger Platform - Message Edits Webhook Types
339
+ * Facebook Messenger Platform - Base Webhook Types
340
340
  *
341
- * These types represent the webhook event structure for message_edits events.
342
- * Triggered when a user edits a previously sent message.
341
+ * This file contains the common base types and interfaces shared across
342
+ * all webhook event types. It provides the foundation for a properly
343
+ * structured type system with inheritance and discriminated unions.
343
344
  *
344
- * @see https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/message-edits/
345
+ * @module webhooks/base-types
345
346
  */
346
347
  /**
347
- * Represents the sender information in a message edit webhook event
348
+ * Common sender interface for all webhook events.
349
+ * Represents the user who initiated the action.
348
350
  */
349
- interface MessageEditSender {
350
- /** Page-scoped ID (PSID) of the user who sent the message */
351
+ interface WebhookSender {
352
+ /**
353
+ * Page-scoped ID (PSID) of the user.
354
+ * This is the unique identifier for the user within the context of your page.
355
+ */
351
356
  id: string;
352
357
  /**
353
358
  * User reference provided by the chat plugin, if applicable.
354
- * Only present for chat plugin events.
359
+ * Only present for chat plugin events where the user hasn't been identified yet.
355
360
  */
356
361
  user_ref?: string;
357
362
  }
358
363
  /**
359
- * Represents the recipient information in a message edit webhook event
364
+ * Common recipient interface for all webhook events.
365
+ * Represents the Facebook Page that received the event.
366
+ */
367
+ interface WebhookRecipient {
368
+ /** Facebook Page ID that received the event */
369
+ id: string;
370
+ }
371
+ /**
372
+ * Base interface for all webhook events.
373
+ * Contains the common properties shared by all event types.
374
+ */
375
+ interface BaseWebhookEvent {
376
+ /** Information about the user who initiated the event */
377
+ sender: WebhookSender;
378
+ /** Information about the page that received the event */
379
+ recipient: WebhookRecipient;
380
+ /**
381
+ * Unix timestamp when the event occurred (in milliseconds since epoch).
382
+ * Represents when the action was performed, not when the webhook was sent.
383
+ */
384
+ timestamp: number;
385
+ }
386
+ /**
387
+ * Webhook event types enum with discriminator values.
388
+ * Used for type narrowing in discriminated unions.
389
+ */
390
+ declare enum WebhookEventType {
391
+ MESSAGE = "message",
392
+ MESSAGE_EDIT = "message_edit",
393
+ MESSAGE_REACTION = "reaction",
394
+ MESSAGE_READ = "read",
395
+ MESSAGING_FEEDBACK = "messaging_feedback",
396
+ MESSAGING_POSTBACK = "postback"
397
+ }
398
+ /**
399
+ * Generic webhook entry structure.
400
+ * Represents a single entry in the webhook payload.
360
401
  */
361
- interface MessageEditRecipient {
362
- /** Page ID that received the message */
402
+ interface WebhookEntry<T extends BaseWebhookEvent = BaseWebhookEvent> {
403
+ /** Unique ID of the page */
363
404
  id: string;
405
+ /** Time of update (epoch time in milliseconds) */
406
+ time: number;
407
+ /** Array of messaging events */
408
+ messaging: T[];
409
+ }
410
+ /**
411
+ * Generic webhook payload structure.
412
+ * This is the top-level structure received from Facebook webhooks.
413
+ */
414
+ interface WebhookPayload<T extends BaseWebhookEvent = BaseWebhookEvent> {
415
+ /** Always 'page' for Messenger webhooks */
416
+ object: 'page';
417
+ /** Array of entry objects containing the actual events */
418
+ entry: WebhookEntry<T>[];
419
+ }
420
+ /**
421
+ * Common processing context for all webhook events
422
+ */
423
+ interface BaseProcessingContext {
424
+ /** The user who initiated the event (PSID or user_ref) */
425
+ senderId?: string;
426
+ /** User reference for anonymous users */
427
+ userRef?: string;
428
+ /** The page that received the event */
429
+ recipientId: string;
430
+ /** When the event occurred */
431
+ timestamp: number;
432
+ /** Whether the sender is identified (has PSID) */
433
+ isIdentifiedUser: boolean;
434
+ /** Human-readable datetime for the event timestamp */
435
+ eventDate: Date;
364
436
  }
437
+
438
+ /**
439
+ * Facebook Messenger Platform - Message Edits Webhook Types
440
+ *
441
+ * These types represent the webhook event structure for message_edits events.
442
+ * Triggered when a user edits a previously sent message.
443
+ *
444
+ * @see https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/message-edits/
445
+ */
446
+
365
447
  /**
366
448
  * Represents the edited message information
367
449
  */
@@ -382,7 +464,7 @@ interface MessageEdit {
382
464
  num_edit: number;
383
465
  }
384
466
  /**
385
- * Main webhook event structure for message edits
467
+ * Main webhook event structure for message edits with discriminator
386
468
  *
387
469
  * This event is triggered when a user edits a previously sent message.
388
470
  * The webhook provides the updated message content and edit count.
@@ -390,6 +472,7 @@ interface MessageEdit {
390
472
  * @example
391
473
  * ```json
392
474
  * {
475
+ * "type": "message_edit",
393
476
  * "sender": {
394
477
  * "id": "1234567890123456"
395
478
  * },
@@ -405,16 +488,9 @@ interface MessageEdit {
405
488
  * }
406
489
  * ```
407
490
  */
408
- interface MessageEditWebhookEvent {
409
- /** Information about the user who edited the message */
410
- sender: MessageEditSender;
411
- /** Information about the page that received the message */
412
- recipient: MessageEditRecipient;
413
- /**
414
- * Timestamp when the edit occurred (in milliseconds since epoch).
415
- * Represents the time the edit was made, not the original message time.
416
- */
417
- timestamp: number;
491
+ interface MessageEditWebhookEvent extends BaseWebhookEvent {
492
+ /** Discriminator for type narrowing */
493
+ type: WebhookEventType.MESSAGE_EDIT;
418
494
  /** Details about the edited message */
419
495
  message_edit: MessageEdit;
420
496
  }
@@ -422,18 +498,7 @@ interface MessageEditWebhookEvent {
422
498
  * Complete webhook payload structure that includes the message edit event
423
499
  * along with other webhook metadata
424
500
  */
425
- interface MessageEditWebhookPayload {
426
- /** The webhook object type - always "page" for Messenger */
427
- object: 'page';
428
- /** Array of webhook entries containing the actual events */
429
- entry: Array<{
430
- /** Page ID */
431
- id: string;
432
- /** Timestamp of the webhook entry */
433
- time: number;
434
- /** Array of messaging events - contains the message edit event */
435
- messaging: MessageEditWebhookEvent[];
436
- }>;
501
+ interface MessageEditWebhookPayload extends WebhookPayload<MessageEditWebhookEvent> {
437
502
  }
438
503
  /**
439
504
  * Type guard to check if a webhook event is a message edit event
@@ -453,19 +518,13 @@ declare function isMessageEditEvent(event: any): event is MessageEditWebhookEven
453
518
  /**
454
519
  * Utility type for common message edit properties that might be used in processing
455
520
  */
456
- interface MessageEditProcessingContext {
521
+ interface MessageEditProcessingContext extends BaseProcessingContext {
457
522
  /** The edited message ID */
458
523
  messageId: string;
459
- /** The user who made the edit */
460
- senderId: string;
461
- /** The page that received the edit */
462
- recipientId: string;
463
524
  /** Updated message content */
464
525
  updatedText: string;
465
526
  /** Number of edits made */
466
527
  editCount: number;
467
- /** When the edit occurred */
468
- editTimestamp: number;
469
528
  }
470
529
  /**
471
530
  * Helper function to extract processing context from a message edit event
@@ -498,6 +557,7 @@ declare const MESSAGE_EDIT_CONSTANTS: {
498
557
  *
499
558
  * @see https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/message-reactions
500
559
  */
560
+
501
561
  /**
502
562
  * Types of reactions that can be applied to messages in Messenger.
503
563
  * These are the predefined reaction types supported by Facebook.
@@ -529,26 +589,7 @@ declare enum MessageReactionAction {
529
589
  /** Removing a reaction from a message */
530
590
  UNREACT = "unreact"
531
591
  }
532
- /**
533
- * Represents the sender of the reaction.
534
- * Contains the Page-Scoped User ID (PSID) of the user who reacted.
535
- */
536
- interface MessageReactionSender {
537
- /** Page-Scoped User ID (PSID) of the user who reacted */
538
- id: string;
539
- /**
540
- * User reference for chat plugin events (optional)
541
- * Used when the reaction comes from a chat plugin integration
542
- */
543
- user_ref?: string;
544
- }
545
- /**
546
- * Represents the recipient (page) that received the reaction.
547
- */
548
- interface MessageReactionRecipient {
549
- /** Page ID of the Facebook page that received the reaction */
550
- id: string;
551
- }
592
+
552
593
  /**
553
594
  * Contains the detailed information about the reaction.
554
595
  */
@@ -574,19 +615,12 @@ interface MessageReactionData {
574
615
  mid: string;
575
616
  }
576
617
  /**
577
- * Complete webhook event structure for message reactions.
618
+ * Complete webhook event structure for message reactions with discriminator.
578
619
  * This is the main payload received when a user reacts to a message.
579
620
  */
580
- interface MessageReactionWebhookEvent {
581
- /** Information about the user who performed the reaction */
582
- sender: MessageReactionSender;
583
- /** Information about the page that received the reaction */
584
- recipient: MessageReactionRecipient;
585
- /**
586
- * Unix timestamp (in milliseconds) when the reaction occurred.
587
- * Example: 1458668856463
588
- */
589
- timestamp: number;
621
+ interface MessageReactionWebhookEvent extends BaseWebhookEvent {
622
+ /** Discriminator for type narrowing */
623
+ type: WebhookEventType.MESSAGE_REACTION;
590
624
  /** Detailed information about the reaction */
591
625
  reaction: MessageReactionData;
592
626
  }
@@ -594,18 +628,7 @@ interface MessageReactionWebhookEvent {
594
628
  * The complete webhook payload containing the message reaction event.
595
629
  * This matches the structure of the webhook POST request body.
596
630
  */
597
- interface MessageReactionWebhookPayload {
598
- /** The page object containing the reaction events */
599
- object: 'page';
600
- /** Array of page entries, each containing reaction events */
601
- entry: Array<{
602
- /** Page ID */
603
- id: string;
604
- /** Unix timestamp when the webhook was triggered */
605
- time: number;
606
- /** Array of messaging events containing the reactions */
607
- messaging: MessageReactionWebhookEvent[];
608
- }>;
631
+ interface MessageReactionWebhookPayload extends WebhookPayload<MessageReactionWebhookEvent> {
609
632
  }
610
633
  /**
611
634
  * Context object for processing message reaction webhooks.
@@ -670,25 +693,7 @@ interface MessageReactionWebhookConfig {
670
693
  *
671
694
  * @see https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/message-reads/
672
695
  */
673
- /**
674
- * Represents the sender information in a message reads webhook event
675
- */
676
- interface MessageReadsSender {
677
- /** Page-scoped ID (PSID) of the user who read the messages */
678
- id: string;
679
- /**
680
- * User reference provided by the chat plugin, if applicable.
681
- * Only present for chat plugin events.
682
- */
683
- user_ref?: string;
684
- }
685
- /**
686
- * Represents the recipient information in a message reads webhook event
687
- */
688
- interface MessageReadsRecipient {
689
- /** Page ID that sent the original messages */
690
- id: string;
691
- }
696
+
692
697
  /**
693
698
  * Represents the read receipt information
694
699
  */
@@ -701,7 +706,7 @@ interface MessageRead {
701
706
  watermark: number;
702
707
  }
703
708
  /**
704
- * Main webhook event structure for message reads
709
+ * Main webhook event structure for message reads with discriminator
705
710
  *
706
711
  * This event is triggered when a user reads messages sent by a Page.
707
712
  * The watermark indicates the timestamp up to which all messages have been read.
@@ -709,6 +714,7 @@ interface MessageRead {
709
714
  * @example
710
715
  * ```json
711
716
  * {
717
+ * "type": "read",
712
718
  * "sender": {
713
719
  * "id": "1234567890123456"
714
720
  * },
@@ -722,16 +728,9 @@ interface MessageRead {
722
728
  * }
723
729
  * ```
724
730
  */
725
- interface MessageReadsWebhookEvent {
726
- /** Information about the user who read the messages */
727
- sender: MessageReadsSender;
728
- /** Information about the page that sent the messages */
729
- recipient: MessageReadsRecipient;
730
- /**
731
- * Timestamp when the read event occurred (in milliseconds since epoch).
732
- * Represents when the read receipt was generated.
733
- */
734
- timestamp: number;
731
+ interface MessageReadsWebhookEvent extends BaseWebhookEvent {
732
+ /** Discriminator for type narrowing */
733
+ type: WebhookEventType.MESSAGE_READ;
735
734
  /** Read receipt information containing the watermark */
736
735
  read: MessageRead;
737
736
  }
@@ -739,18 +738,7 @@ interface MessageReadsWebhookEvent {
739
738
  * Complete webhook payload structure that includes the message reads event
740
739
  * along with other webhook metadata
741
740
  */
742
- interface MessageReadsWebhookPayload {
743
- /** The webhook object type - always "page" for Messenger */
744
- object: 'page';
745
- /** Array of webhook entries containing the actual events */
746
- entry: Array<{
747
- /** Page ID */
748
- id: string;
749
- /** Timestamp of the webhook entry */
750
- time: number;
751
- /** Array of messaging events - contains the message reads event */
752
- messaging: MessageReadsWebhookEvent[];
753
- }>;
741
+ interface MessageReadsWebhookPayload extends WebhookPayload<MessageReadsWebhookEvent> {
754
742
  }
755
743
  /**
756
744
  * Type guard to check if a webhook event is a message reads event
@@ -895,28 +883,7 @@ type WatermarkTimestamp = number;
895
883
  *
896
884
  * @see https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/messaging_postbacks
897
885
  */
898
- /**
899
- * Represents the sender information in a messaging postback webhook event
900
- */
901
- interface PostbackSender {
902
- /**
903
- * Page-scoped ID (PSID) of the user who triggered the postback.
904
- * This is the unique identifier for the user within the context of your page.
905
- */
906
- id?: string;
907
- /**
908
- * User reference provided by the chat plugin, if applicable.
909
- * Only present for chat plugin events where the user hasn't been identified yet.
910
- */
911
- user_ref?: string;
912
- }
913
- /**
914
- * Represents the recipient information in a messaging postback webhook event
915
- */
916
- interface PostbackRecipient {
917
- /** Facebook Page ID that received the postback */
918
- id: string;
919
- }
886
+
920
887
  /**
921
888
  * Represents referral information for postbacks that originated from external sources
922
889
  *
@@ -972,7 +939,7 @@ interface PostbackData {
972
939
  referral?: PostbackReferral;
973
940
  }
974
941
  /**
975
- * Main webhook event structure for messaging postbacks
942
+ * Main webhook event structure for messaging postbacks with discriminator
976
943
  *
977
944
  * This event is triggered when a user interacts with postback buttons,
978
945
  * including regular postback buttons, Get Started button, and persistent menu items.
@@ -981,6 +948,7 @@ interface PostbackData {
981
948
  * Basic postback from a button click:
982
949
  * ```json
983
950
  * {
951
+ * "type": "postback",
984
952
  * "sender": {
985
953
  * "id": "1234567890123456"
986
954
  * },
@@ -1000,6 +968,7 @@ interface PostbackData {
1000
968
  * Postback with referral data from m.me link:
1001
969
  * ```json
1002
970
  * {
971
+ * "type": "postback",
1003
972
  * "sender": {
1004
973
  * "user_ref": "unique_ref_param"
1005
974
  * },
@@ -1019,16 +988,9 @@ interface PostbackData {
1019
988
  * }
1020
989
  * ```
1021
990
  */
1022
- interface MessagingPostbackWebhookEvent {
1023
- /** Information about the user who triggered the postback */
1024
- sender: PostbackSender;
1025
- /** Information about the page that received the postback */
1026
- recipient: PostbackRecipient;
1027
- /**
1028
- * Unix timestamp when the postback was triggered (in milliseconds since epoch).
1029
- * Represents when the user clicked the button, not when the webhook was sent.
1030
- */
1031
- timestamp: number;
991
+ interface MessagingPostbackWebhookEvent extends BaseWebhookEvent {
992
+ /** Discriminator for type narrowing */
993
+ type: WebhookEventType.MESSAGING_POSTBACK;
1032
994
  /** Details about the postback that was triggered */
1033
995
  postback: PostbackData;
1034
996
  }
@@ -1036,18 +998,7 @@ interface MessagingPostbackWebhookEvent {
1036
998
  * Complete webhook payload structure that includes the messaging postback event
1037
999
  * along with other webhook metadata
1038
1000
  */
1039
- interface MessagingPostbackWebhookPayload {
1040
- /** The webhook object type - always "page" for Messenger */
1041
- object: 'page';
1042
- /** Array of webhook entries containing the actual events */
1043
- entry: Array<{
1044
- /** Page ID */
1045
- id: string;
1046
- /** Timestamp of the webhook entry */
1047
- time: number;
1048
- /** Array of messaging events - contains the postback event */
1049
- messaging: MessagingPostbackWebhookEvent[];
1050
- }>;
1001
+ interface MessagingPostbackWebhookPayload extends WebhookPayload<MessagingPostbackWebhookEvent> {
1051
1002
  }
1052
1003
  /**
1053
1004
  * Type guard to check if a webhook event is a messaging postback event
@@ -1102,7 +1053,7 @@ declare function hasReferralData(event: MessagingPostbackWebhookEvent): event is
1102
1053
  * }
1103
1054
  * ```
1104
1055
  */
1105
- declare function isIdentifiedSender(sender: PostbackSender): sender is PostbackSender & {
1056
+ declare function isIdentifiedSender(sender: WebhookSender): sender is WebhookSender & {
1106
1057
  id: string;
1107
1058
  };
1108
1059
  /**
@@ -1223,6 +1174,7 @@ type ReferralType$1 = typeof POSTBACK_CONSTANTS.REFERRAL_TYPES[keyof typeof POST
1223
1174
  *
1224
1175
  * @see https://developers.facebook.com/docs/messenger-platform/send-messages/templates/customer-feedback-template
1225
1176
  */
1177
+
1226
1178
  /**
1227
1179
  * Available feedback types for customer feedback templates
1228
1180
  */
@@ -1266,25 +1218,7 @@ declare enum FollowUpType {
1266
1218
  /** Free-form text input (max 400 characters) */
1267
1219
  FREE_FORM = "free_form"
1268
1220
  }
1269
- /**
1270
- * Represents the sender information in a messaging feedback webhook event
1271
- */
1272
- interface MessagingFeedbackSender {
1273
- /** Page-scoped ID (PSID) of the user who submitted the feedback */
1274
- id: string;
1275
- /**
1276
- * User reference provided by the chat plugin, if applicable.
1277
- * Only present for chat plugin events.
1278
- */
1279
- user_ref?: string;
1280
- }
1281
- /**
1282
- * Represents the recipient information in a messaging feedback webhook event
1283
- */
1284
- interface MessagingFeedbackRecipient {
1285
- /** Page ID that received the feedback */
1286
- id: string;
1287
- }
1221
+
1288
1222
  /**
1289
1223
  * Represents optional follow-up feedback data
1290
1224
  */
@@ -1346,7 +1280,7 @@ interface MessagingFeedbackData {
1346
1280
  feedback_screens: FeedbackScreen[];
1347
1281
  }
1348
1282
  /**
1349
- * Main webhook event structure for messaging feedback
1283
+ * Main webhook event structure for messaging feedback with discriminator
1350
1284
  *
1351
1285
  * This event is triggered when a user submits feedback through a
1352
1286
  * Customer Feedback Template. The webhook provides the user's
@@ -1355,6 +1289,7 @@ interface MessagingFeedbackData {
1355
1289
  * @example
1356
1290
  * ```json
1357
1291
  * {
1292
+ * "type": "messaging_feedback",
1358
1293
  * "sender": {
1359
1294
  * "id": "1234567890123456"
1360
1295
  * },
@@ -1380,16 +1315,9 @@ interface MessagingFeedbackData {
1380
1315
  * }
1381
1316
  * ```
1382
1317
  */
1383
- interface MessagingFeedbackWebhookEvent {
1384
- /** Information about the user who submitted the feedback */
1385
- sender: MessagingFeedbackSender;
1386
- /** Information about the page that received the feedback */
1387
- recipient: MessagingFeedbackRecipient;
1388
- /**
1389
- * Timestamp when the feedback was submitted (in milliseconds since epoch).
1390
- * Represents when the user completed and submitted the feedback form.
1391
- */
1392
- timestamp: number;
1318
+ interface MessagingFeedbackWebhookEvent extends BaseWebhookEvent {
1319
+ /** Discriminator for type narrowing */
1320
+ type: WebhookEventType.MESSAGING_FEEDBACK;
1393
1321
  /** The actual feedback data containing user responses */
1394
1322
  messaging_feedback: MessagingFeedbackData;
1395
1323
  }
@@ -1397,18 +1325,7 @@ interface MessagingFeedbackWebhookEvent {
1397
1325
  * Complete webhook payload structure that includes the messaging feedback event
1398
1326
  * along with other webhook metadata
1399
1327
  */
1400
- interface MessagingFeedbackWebhookPayload {
1401
- /** The webhook object type - always "page" for Messenger */
1402
- object: 'page';
1403
- /** Array of webhook entries containing the actual events */
1404
- entry: Array<{
1405
- /** Page ID */
1406
- id: string;
1407
- /** Timestamp of the webhook entry */
1408
- time: number;
1409
- /** Array of messaging events - contains the feedback event */
1410
- messaging: MessagingFeedbackWebhookEvent[];
1411
- }>;
1328
+ interface MessagingFeedbackWebhookPayload extends WebhookPayload<MessagingFeedbackWebhookEvent> {
1412
1329
  }
1413
1330
  /**
1414
1331
  * Type guard to check if a webhook event is a messaging feedback event
@@ -1587,6 +1504,7 @@ declare function isValidTextFeedback(textFeedback: string): boolean;
1587
1504
  *
1588
1505
  * @see https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/messages
1589
1506
  */
1507
+
1590
1508
  /**
1591
1509
  * Enumeration of attachment types supported by the Messenger Platform
1592
1510
  */
@@ -1617,25 +1535,7 @@ declare enum ReferralSource {
1617
1535
  SHORTLINK = "SHORTLINK",
1618
1536
  CUSTOMER_CHAT_PLUGIN = "CUSTOMER_CHAT_PLUGIN"
1619
1537
  }
1620
- /**
1621
- * Represents the sender information in a message webhook event
1622
- */
1623
- interface MessageSender {
1624
- /** Page-scoped ID (PSID) of the user who sent the message */
1625
- id: string;
1626
- /**
1627
- * User reference provided by the chat plugin, if applicable.
1628
- * Only present for chat plugin events.
1629
- */
1630
- user_ref?: string;
1631
- }
1632
- /**
1633
- * Represents the recipient information in a message webhook event
1634
- */
1635
- interface MessageRecipient {
1636
- /** Page ID that received the message */
1637
- id: string;
1638
- }
1538
+
1639
1539
  /**
1640
1540
  * Represents a quick reply payload in a message
1641
1541
  */
@@ -1803,7 +1703,7 @@ interface Message {
1803
1703
  commands?: MessageCommand[];
1804
1704
  }
1805
1705
  /**
1806
- * Main webhook event structure for messages
1706
+ * Main webhook event structure for messages with discriminator
1807
1707
  *
1808
1708
  * This event is triggered when a user sends a message to your page.
1809
1709
  * The webhook provides the message content and metadata.
@@ -1811,6 +1711,7 @@ interface Message {
1811
1711
  * @example Text message:
1812
1712
  * ```json
1813
1713
  * {
1714
+ * "type": "message",
1814
1715
  * "sender": {
1815
1716
  * "id": "1234567890123456"
1816
1717
  * },
@@ -1828,6 +1729,7 @@ interface Message {
1828
1729
  * @example Message with attachment:
1829
1730
  * ```json
1830
1731
  * {
1732
+ * "type": "message",
1831
1733
  * "sender": {
1832
1734
  * "id": "1234567890123456"
1833
1735
  * },
@@ -1852,6 +1754,7 @@ interface Message {
1852
1754
  * @example Message with quick reply:
1853
1755
  * ```json
1854
1756
  * {
1757
+ * "type": "message",
1855
1758
  * "sender": {
1856
1759
  * "id": "1234567890123456"
1857
1760
  * },
@@ -1869,16 +1772,9 @@ interface Message {
1869
1772
  * }
1870
1773
  * ```
1871
1774
  */
1872
- interface MessageWebhookEvent {
1873
- /** Information about the user who sent the message */
1874
- sender: MessageSender;
1875
- /** Information about the page that received the message */
1876
- recipient: MessageRecipient;
1877
- /**
1878
- * Timestamp when the message was sent (in milliseconds since epoch).
1879
- * Note: This represents when the message was sent, not when it was received.
1880
- */
1881
- timestamp: number;
1775
+ interface MessageWebhookEvent extends BaseWebhookEvent {
1776
+ /** Discriminator for type narrowing */
1777
+ type: WebhookEventType.MESSAGE;
1882
1778
  /** The message content and metadata */
1883
1779
  message: Message;
1884
1780
  }
@@ -1886,18 +1782,7 @@ interface MessageWebhookEvent {
1886
1782
  * Complete webhook payload structure that includes the message event
1887
1783
  * along with other webhook metadata
1888
1784
  */
1889
- interface MessageWebhookPayload {
1890
- /** The webhook object type - always "page" for Messenger */
1891
- object: 'page';
1892
- /** Array of webhook entries containing the actual events */
1893
- entry: Array<{
1894
- /** Page ID */
1895
- id: string;
1896
- /** Timestamp of the webhook entry */
1897
- time: number;
1898
- /** Array of messaging events - contains the message event */
1899
- messaging: MessageWebhookEvent[];
1900
- }>;
1785
+ interface MessageWebhookPayload extends WebhookPayload<MessageWebhookEvent> {
1901
1786
  }
1902
1787
  /**
1903
1788
  * Type guard to check if a webhook event is a message event
@@ -1972,13 +1857,9 @@ declare function isAttachmentType<T extends AttachmentType>(attachment: MessageA
1972
1857
  /**
1973
1858
  * Utility type for common message properties that might be used in processing
1974
1859
  */
1975
- interface MessageProcessingContext {
1860
+ interface MessageProcessingContext extends BaseProcessingContext {
1976
1861
  /** The message ID */
1977
1862
  messageId: string;
1978
- /** The user who sent the message */
1979
- senderId: string;
1980
- /** The page that received the message */
1981
- recipientId: string;
1982
1863
  /** Message text content, if available */
1983
1864
  text?: string;
1984
1865
  /** Whether the message has attachments */
@@ -1989,8 +1870,6 @@ interface MessageProcessingContext {
1989
1870
  isReply: boolean;
1990
1871
  /** Whether the message has referral data */
1991
1872
  hasReferral: boolean;
1992
- /** When the message was sent */
1993
- timestamp: number;
1994
1873
  /** Quick reply payload, if available */
1995
1874
  quickReplyPayload?: string;
1996
1875
  /** Replied message ID, if this is a reply */
@@ -2065,17 +1944,18 @@ declare const ATTACHMENT_MIME_TYPES: {
2065
1944
  };
2066
1945
 
2067
1946
  /**
2068
- * Facebook Messenger Webhook Events
1947
+ * Facebook Messenger Webhook Events - Main Entry Point
2069
1948
  *
2070
- * This file provides unified types for all webhook events supported by the Messenger Platform.
2071
- * It includes a discriminated union type that allows TypeScript to narrow the event type
2072
- * based on the event properties.
1949
+ * This file provides the unified type system for all webhook events supported
1950
+ * by the Messenger Platform. It uses discriminated unions with proper type
1951
+ * narrowing to ensure type safety.
2073
1952
  *
2074
1953
  * @module webhooks/webhook-events
2075
1954
  */
2076
1955
 
2077
1956
  /**
2078
- * Union type of all possible webhook events
1957
+ * Discriminated union type of all possible webhook events.
1958
+ * Each event has a 'type' field that allows TypeScript to narrow the type.
2079
1959
  */
2080
1960
  type MessengerWebhookEvent = MessageEditWebhookEvent | MessageReactionWebhookEvent | MessageWebhookEvent | MessageReadsWebhookEvent | MessagingFeedbackWebhookEvent | MessagingPostbackWebhookEvent;
2081
1961
  /**
@@ -2083,46 +1963,59 @@ type MessengerWebhookEvent = MessageEditWebhookEvent | MessageReactionWebhookEve
2083
1963
  */
2084
1964
  type MessengerWebhookPayload = MessageEditWebhookPayload | MessageReactionWebhookPayload | MessageWebhookPayload | MessageReadsWebhookPayload | MessagingFeedbackWebhookPayload | MessagingPostbackWebhookPayload;
2085
1965
  /**
2086
- * Generic webhook entry structure
2087
- */
2088
- interface WebhookEntry {
2089
- /** Unique ID of the page */
2090
- id: string;
2091
- /** Time of update (epoch time in milliseconds) */
2092
- time: number;
2093
- /** Array of messaging events */
2094
- messaging: MessengerWebhookEvent[];
2095
- }
2096
- /**
2097
- * Generic webhook payload structure
2098
- */
2099
- interface GenericWebhookPayload {
2100
- /** Always 'page' for Messenger webhooks */
2101
- object: 'page';
2102
- /** Array of entry objects */
2103
- entry: WebhookEntry[];
2104
- }
2105
- /**
2106
- * Webhook event types enum
2107
- */
2108
- declare enum WebhookEventType {
2109
- MESSAGE = "message",
2110
- MESSAGE_EDIT = "message_edit",
2111
- MESSAGE_REACTION = "reaction",
2112
- MESSAGE_READ = "read",
2113
- MESSAGING_FEEDBACK = "messaging_feedback",
2114
- MESSAGING_POSTBACK = "postback"
2115
- }
2116
- /**
2117
- * Type guard to check webhook event type
1966
+ * Type guard to check webhook event type for a SINGLE event.
1967
+ * This function now properly handles individual events from the messaging array.
1968
+ *
1969
+ * @param event - A single webhook event from the messaging array
1970
+ * @returns The event type or null if unknown
1971
+ *
1972
+ * @example
1973
+ * ```typescript
1974
+ * const events = extractWebhookEvents(payload);
1975
+ * for (const event of events) {
1976
+ * const eventType = getWebhookEventType(event);
1977
+ * console.log(`Event type: ${eventType}`);
1978
+ * }
1979
+ * ```
2118
1980
  */
2119
- declare function getWebhookEventType(event: MessengerWebhookEvent): WebhookEventType | null;
1981
+ declare function getWebhookEventType(event: MessengerWebhookEvent | any): WebhookEventType | null;
2120
1982
  /**
2121
- * Extract all events from a webhook payload
1983
+ * Extract all events from a webhook payload.
1984
+ * This properly extracts individual events from the nested structure.
1985
+ *
1986
+ * @param payload - The complete webhook payload
1987
+ * @returns Array of individual webhook events
1988
+ *
1989
+ * @example
1990
+ * ```typescript
1991
+ * const events = extractWebhookEvents(payload);
1992
+ * events.forEach(event => {
1993
+ * const type = getWebhookEventType(event);
1994
+ * console.log(`Processing ${type} event`);
1995
+ * });
1996
+ * ```
2122
1997
  */
2123
- declare function extractWebhookEvents(payload: GenericWebhookPayload): MessengerWebhookEvent[];
1998
+ declare function extractWebhookEvents(payload: WebhookPayload): MessengerWebhookEvent[];
2124
1999
  /**
2125
- * Process webhook events by type
2000
+ * Process webhook events by type with proper type narrowing.
2001
+ * This uses the discriminated union for automatic type narrowing.
2002
+ *
2003
+ * @param payload - The complete webhook payload
2004
+ * @param handlers - Object with handler functions for each event type
2005
+ *
2006
+ * @example
2007
+ * ```typescript
2008
+ * processWebhookEvents(payload, {
2009
+ * onMessage: async (event) => {
2010
+ * // TypeScript knows event is MessageWebhookEvent
2011
+ * console.log(`Message: ${event.message.text}`);
2012
+ * },
2013
+ * onMessageEdit: async (event) => {
2014
+ * // TypeScript knows event is MessageEditWebhookEvent
2015
+ * console.log(`Edited to: ${event.message_edit.text}`);
2016
+ * }
2017
+ * });
2018
+ * ```
2126
2019
  */
2127
2020
  interface WebhookEventHandlers {
2128
2021
  onMessage?: (event: MessageWebhookEvent) => void | Promise<void>;
@@ -2133,12 +2026,9 @@ interface WebhookEventHandlers {
2133
2026
  onMessagingPostback?: (event: MessagingPostbackWebhookEvent) => void | Promise<void>;
2134
2027
  onUnknown?: (event: any) => void | Promise<void>;
2135
2028
  }
2029
+ declare function processWebhookEvents(payload: WebhookPayload, handlers: WebhookEventHandlers): Promise<void>;
2136
2030
  /**
2137
- * Process webhook events with type-safe handlers
2138
- */
2139
- declare function processWebhookEvents(payload: GenericWebhookPayload, handlers: WebhookEventHandlers): Promise<void>;
2140
- /**
2141
- * Webhook verification parameters
2031
+ * Webhook verification parameters for subscription verification
2142
2032
  */
2143
2033
  interface WebhookVerificationParams {
2144
2034
  'hub.mode': string;
@@ -2146,7 +2036,23 @@ interface WebhookVerificationParams {
2146
2036
  'hub.challenge': string;
2147
2037
  }
2148
2038
  /**
2149
- * Verify webhook subscription
2039
+ * Verify webhook subscription during initial setup
2040
+ *
2041
+ * @param params - Query parameters from Facebook's verification request
2042
+ * @param verifyToken - Your webhook verify token
2043
+ * @returns The challenge string if valid, null if invalid
2044
+ *
2045
+ * @example
2046
+ * ```typescript
2047
+ * app.get('/webhook', (req, res) => {
2048
+ * const challenge = verifyWebhookSubscription(req.query, process.env.VERIFY_TOKEN);
2049
+ * if (challenge) {
2050
+ * res.send(challenge);
2051
+ * } else {
2052
+ * res.status(403).send('Forbidden');
2053
+ * }
2054
+ * });
2055
+ * ```
2150
2056
  */
2151
2057
  declare function verifyWebhookSubscription(params: WebhookVerificationParams, verifyToken: string): string | null;
2152
2058
 
@@ -2200,4 +2106,4 @@ declare const TEMPLATE_LIMITS: {
2200
2106
  readonly MEDIA_BUTTONS_MAX_COUNT: 3;
2201
2107
  };
2202
2108
 
2203
- export { ATTACHMENT_LIMITS, ATTACHMENT_MIME_TYPES, type AttachmentPayload, type AttachmentType$1 as AttachmentType, type AttachmentUploadRequest, type AttachmentUploadResponse, AttachmentsAPI, type Button, type ButtonTemplatePayload, CESDisplayOption, COMMON_POSTBACK_PAYLOADS, CSATDisplayOption, type DefaultAction, type ErrorResponse, type FallbackAttachmentPayload, type FeedbackFollowUp, type FeedbackQuestion, type FeedbackScreen, FeedbackType, FollowUpType, type GenericTemplateElement, type GenericTemplatePayload, type GenericWebhookPayload, type GetProfileRequest, MESSAGE_CONSTANTS, MESSAGE_EDIT_CONSTANTS, MESSAGE_LIMITS, MESSAGE_READS_CONSTANTS, MESSAGING_FEEDBACK_CONSTANTS, type MediaAttachmentPayload, type MediaTemplateElement, type MediaTemplatePayload, type Message$1 as Message, type MessageAttachment, type MessageCommand, type MessageEdit, type MessageEditProcessingContext, type MessageEditRecipient, type MessageEditSender, type MessageEditWebhookEvent, type MessageEditWebhookPayload, type MessageProcessingContext, MessageReactionAction, type MessageReactionData, type MessageReactionProcessingContext, type MessageReactionRecipient, type MessageReactionSender, type MessageReactionStats, MessageReactionType, type MessageReactionWebhookConfig, type MessageReactionWebhookEvent, type MessageReactionWebhookPayload, type MessageReadData, type MessageReadsProcessingContext, type MessageReadsWebhookEvent, type MessageReadsWebhookPayload, type MessageRecipient, type MessageReferral, ReferralSource as MessageReferralSource, ReferralType as MessageReferralType, type MessageSender, MessageValidationError, type MessageWebhookEvent, type MessageWebhookPayload, type MessagingFeedbackData, type MessagingFeedbackProcessingContext, type MessagingFeedbackWebhookEvent, type MessagingFeedbackWebhookPayload, type MessagingPostbackWebhookEvent, type MessagingPostbackWebhookPayload, type MessagingType, Messenger, MessengerAPIError, type MessengerConfig, MessengerConfigError, type MessengerError, MessengerNetworkError, MessengerTimeoutError, type MessengerWebhookEvent, type MessengerWebhookPayload, type ModerateConversationsRequest, type ModerateConversationsResponse, ModerationAPI, type ModerationAction, NPSDisplayOption, POSTBACK_CONSTANTS, type PostbackData, type PostbackEventData, type PostbackPayload, type PostbackProcessingContext, type PostbackRecipient, type PostbackReferral, type PostbackSender, type ProductTemplateElement, type ProductTemplatePayload, ProfileAPI, type ProfileField, type QuickReply$1 as QuickReply, type Recipient, type ReelAttachmentPayload, type ReferralSource$1 as ReferralSource, type ReferralType$1 as ReferralType, type ReplyTo, SendAPI, type SendMessageRequest, type SendMessageResponse, type SenderAction, type StickerAttachmentPayload, TEMPLATE_LIMITS, TemplateValidationError, TemplatesAPI, type UserId, type UserProfile, type WatermarkTimestamp, AttachmentType as WebhookAttachmentType, type WebhookEntry, type WebhookEventHandlers, WebhookEventType, type Message as WebhookMessage, type QuickReply as WebhookQuickReply, type WebhookVerificationParams, extractMessageContext, extractMessageEditContext, extractMessageReadsContext, extractMessagingFeedbackContext, extractPostbackContext, extractTextFeedback, extractWebhookEvents, getAttachmentUrls, getAttachmentsByType, getFeedbackScoresByType, getReadMessageCount, getReadMessages, getWebhookEventType, hasAttachments, hasQuickReply, hasReferral, hasReferralData, isAttachmentType, isIdentifiedSender, isMessageEditEvent, isMessageEvent, isMessageRead, isMessageReadsEvent, isMessagingFeedbackEvent, isMessagingPostbackEvent, isReplyMessage, isTextMessage, isValidFeedbackScore, isValidQuestionId, isValidTextFeedback, processWebhookEvents, verifyWebhookSubscription };
2109
+ export { ATTACHMENT_LIMITS, ATTACHMENT_MIME_TYPES, type AttachmentPayload, type AttachmentType$1 as AttachmentType, type AttachmentUploadRequest, type AttachmentUploadResponse, AttachmentsAPI, type Button, type ButtonTemplatePayload, CESDisplayOption, COMMON_POSTBACK_PAYLOADS, CSATDisplayOption, type DefaultAction, type ErrorResponse, type FallbackAttachmentPayload, type FeedbackFollowUp, type FeedbackQuestion, type FeedbackScreen, FeedbackType, FollowUpType, type GenericTemplateElement, type GenericTemplatePayload, type WebhookPayload as GenericWebhookPayload, type GetProfileRequest, MESSAGE_CONSTANTS, MESSAGE_EDIT_CONSTANTS, MESSAGE_LIMITS, MESSAGE_READS_CONSTANTS, MESSAGING_FEEDBACK_CONSTANTS, type MediaAttachmentPayload, type MediaTemplateElement, type MediaTemplatePayload, type Message$1 as Message, type MessageAttachment, type MessageCommand, type MessageEdit, type MessageEditProcessingContext, type WebhookRecipient as MessageEditRecipient, type WebhookSender as MessageEditSender, type MessageEditWebhookEvent, type MessageEditWebhookPayload, type MessageProcessingContext, MessageReactionAction, type MessageReactionData, type MessageReactionProcessingContext, type WebhookRecipient as MessageReactionRecipient, type WebhookSender as MessageReactionSender, type MessageReactionStats, MessageReactionType, type MessageReactionWebhookConfig, type MessageReactionWebhookEvent, type MessageReactionWebhookPayload, type MessageReadData, type MessageReadsProcessingContext, type MessageReadsWebhookEvent, type MessageReadsWebhookPayload, type WebhookRecipient as MessageRecipient, type MessageReferral, ReferralSource as MessageReferralSource, ReferralType as MessageReferralType, type WebhookSender as MessageSender, MessageValidationError, type MessageWebhookEvent, type MessageWebhookPayload, type MessagingFeedbackData, type MessagingFeedbackProcessingContext, type MessagingFeedbackWebhookEvent, type MessagingFeedbackWebhookPayload, type MessagingPostbackWebhookEvent, type MessagingPostbackWebhookPayload, type MessagingType, Messenger, MessengerAPIError, type MessengerConfig, MessengerConfigError, type MessengerError, MessengerNetworkError, MessengerTimeoutError, type MessengerWebhookEvent, type MessengerWebhookPayload, type ModerateConversationsRequest, type ModerateConversationsResponse, ModerationAPI, type ModerationAction, NPSDisplayOption, POSTBACK_CONSTANTS, type PostbackData, type PostbackEventData, type PostbackPayload, type PostbackProcessingContext, type WebhookRecipient as PostbackRecipient, type PostbackReferral, type WebhookSender as PostbackSender, type ProductTemplateElement, type ProductTemplatePayload, ProfileAPI, type ProfileField, type QuickReply$1 as QuickReply, type Recipient, type ReelAttachmentPayload, type ReferralSource$1 as ReferralSource, type ReferralType$1 as ReferralType, type ReplyTo, SendAPI, type SendMessageRequest, type SendMessageResponse, type SenderAction, type StickerAttachmentPayload, TEMPLATE_LIMITS, TemplateValidationError, TemplatesAPI, type UserId, type UserProfile, type WatermarkTimestamp, AttachmentType as WebhookAttachmentType, type WebhookEntry, type WebhookEventHandlers, WebhookEventType, type Message as WebhookMessage, type QuickReply as WebhookQuickReply, type WebhookVerificationParams, extractMessageContext, extractMessageEditContext, extractMessageReadsContext, extractMessagingFeedbackContext, extractPostbackContext, extractTextFeedback, extractWebhookEvents, getAttachmentUrls, getAttachmentsByType, getFeedbackScoresByType, getReadMessageCount, getReadMessages, getWebhookEventType, hasAttachments, hasQuickReply, hasReferral, hasReferralData, isAttachmentType, isIdentifiedSender, isMessageEditEvent, isMessageEvent, isMessageRead, isMessageReadsEvent, isMessagingFeedbackEvent, isMessagingPostbackEvent, isReplyMessage, isTextMessage, isValidFeedbackScore, isValidQuestionId, isValidTextFeedback, processWebhookEvents, verifyWebhookSubscription };