1+ import { Helper } from "@/utils/helper" ;
2+ import { Request , Response } from "express" ;
3+ import { BaseChannel } from "./base.channel" ;
4+
5+ export class MessengerChannel extends BaseChannel {
6+ pageToken : string ;
7+ webhookSecret : string ;
8+ messengerPostURL : string ;
9+ credentials : string ;
10+
11+ constructor ( id : string , contactId : string , contactName : string , channelType : string , credentials : string ) {
12+ super ( id , contactId , contactName , channelType ) ;
13+
14+ let parseCredentials : MessengerChannel ;
15+
16+ this . credentials = credentials ;
17+
18+ if ( credentials && typeof credentials == 'string' ) parseCredentials = JSON . parse ( credentials ) ;
19+
20+ if ( parseCredentials ) {
21+ this . pageToken = parseCredentials . pageToken ;
22+ this . webhookSecret = parseCredentials . webhookSecret ;
23+ }
24+
25+ this . channelType = channelType ;
26+ this . messengerPostURL = `https://graph.facebook.com/v18.0/me/messages?access_token=` ;
27+ }
28+
29+ public verifyWebhook ( req : Request , res : Response ) {
30+ let mode = req . query [ 'hub.mode' ] ;
31+ let token = req . query [ 'hub.verify_token' ] ;
32+ let challenge = req . query [ 'hub.challenge' ] ;
33+
34+ if ( mode === 'subscribe' && this . webhookSecret == token ) {
35+ console . log ( `channel ${ this . channelType } - ${ this . contactName } ${ this . contactId } webhook verified!` ) ;
36+ return challenge ;
37+ } else {
38+ console . error ( `Verification channel ${ this . channelType } - ${ this . contactName } ${ this . contactId } failed!` ) ;
39+ return ;
40+ }
41+ }
42+
43+ public async prepareMessage ( req : Request , res : Response ) {
44+ const { object, entry } = req . body ;
45+
46+ if ( object != 'page' || ! Array . isArray ( entry ) ) return ;
47+
48+ entry . forEach ( pageEntry => {
49+ if ( ! Array . isArray ( pageEntry . messaging ) ) return ;
50+
51+ pageEntry . messaging . forEach ( async ( messagingEvent ) => {
52+ if ( messagingEvent . messaging_customer_information )
53+ return this . sendAddressToBot ( {
54+ userId : messagingEvent . sender . id ,
55+ address : messagingEvent . messaging_customer_information . screens [ 0 ] . responses ,
56+ } ) ;
57+
58+ if ( ! messagingEvent . message && ! messagingEvent . postback ) return ;
59+
60+ const senderId = messagingEvent . sender . id ;
61+ const messageText = messagingEvent . message && messagingEvent . message . text ;
62+ const payload = messagingEvent . postback && messagingEvent . postback . payload ;
63+ const quick_reply = messagingEvent . message && messagingEvent . message . quick_reply ;
64+
65+ if ( senderId == this . contactId ) return ; //Agent replied to user => skip
66+
67+ return this . postMessageToBot ( { userId : senderId , message : messageText || payload , data : null } ) ;
68+ } ) ;
69+ } ) ;
70+ }
71+
72+ sendAddressToBot ( { userId, address } ) {
73+ return this . postMessageToBot ( { userId, message : 'ADDRESS' , data : { USER_INFORMATION : Helper . arrayToObj ( address ) } } ) ;
74+ }
75+ }
0 commit comments