1+ import { Helper } from "@/utils/helper" ;
12import { Request , Response } from "express" ;
23import { BaseChannel } from "./base.channel" ;
34
@@ -7,7 +8,6 @@ export class MessengerChannel extends BaseChannel {
78 messengerPostURL : string ;
89 credentials : string ;
910
10-
1111 constructor ( id : string , contactId : string , contactName : string , channelType : string , credentials : string ) {
1212 super ( id , contactId , contactName , channelType ) ;
1313
@@ -36,11 +36,40 @@ export class MessengerChannel extends BaseChannel {
3636 return challenge ;
3737 } else {
3838 console . error ( `Verification channel ${ this . channelType } - ${ this . contactName } ${ this . contactId } failed!` ) ;
39- return null ;
39+ return ;
4040 }
4141 }
4242
4343 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+ }
4471
72+ sendAddressToBot ( { userId, address } ) {
73+ return this . postMessageToBot ( { userId, message : 'ADDRESS' , data : { USER_INFORMATION : Helper . arrayToObj ( address ) } } ) ;
4574 }
4675}
0 commit comments