11import { Helper } from "@/utils/helper" ;
2+ import axios from "axios" ;
23import { Request , Response } from "express" ;
34import { BaseChannel } from "./base.channel" ;
45
@@ -35,7 +36,7 @@ export class MessengerChannel extends BaseChannel {
3536 console . log ( `channel ${ this . channelType } - ${ this . contactName } ${ this . contactId } webhook verified!` ) ;
3637 return challenge ;
3738 } else {
38- console . error ( `Verification channel ${ this . channelType } - ${ this . contactName } ${ this . contactId } failed!` ) ;
39+ console . error ( `[MSG] Verification channel ${ this . channelType } - ${ this . contactName } ${ this . contactId } failed!` ) ;
3940 return ;
4041 }
4142 }
@@ -72,4 +73,49 @@ export class MessengerChannel extends BaseChannel {
7273 sendAddressToBot ( { userId, address } ) {
7374 return this . postMessageToBot ( { userId, message : 'ADDRESS' , data : { USER_INFORMATION : Helper . arrayToObj ( address ) } } ) ;
7475 }
76+
77+ public async sendMessageToUser ( { userId, text } ) {
78+ if ( ! text ) return ;
79+
80+ try {
81+ await axios ( {
82+ method : 'POST' ,
83+ url : this . messengerPostURL + this . pageToken ,
84+ data : {
85+ messaging_type : 'RESPONSE' ,
86+ recipient : {
87+ id : userId ,
88+ } ,
89+ message : { text } ,
90+ } ,
91+ } ) ;
92+ console . log ( `[MSG] Sent: ${ text } to ${ userId } ` ) ;
93+ } catch ( e ) {
94+ console . log ( `[MSG] ${ this . contactId } Can not send message to messenger ${ e . message } ` ) ;
95+ }
96+ }
97+
98+ public async sendActionToUser ( { userId, type } ) {
99+ const types = {
100+ typing : 'TYPING_ON' ,
101+ } ;
102+
103+ if ( ! types [ type ] ) return ;
104+
105+ try {
106+ await axios ( {
107+ method : 'POST' ,
108+ url : this . messengerPostURL + this . pageToken ,
109+ data : {
110+ messaging_type : 'RESPONSE' ,
111+ recipient : {
112+ id : userId ,
113+ } ,
114+ sender_action : types [ type ] ,
115+ } ,
116+ } ) ;
117+ } catch ( e ) {
118+ console . log ( '[MSG] Messenger can not send action to user' , e . message ) ;
119+ }
120+ }
75121}
0 commit comments