1+ import { PUBLIC_DOMAIN } from "@/config" ;
2+ import axios from "axios" ;
3+ import { Request , Response } from "express" ;
4+ import { BaseChannel } from "./base.channel" ;
5+
6+ export class LineChannel extends BaseChannel {
7+ pageToken : string ;
8+ linePostURL : 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 : LineChannel ;
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+ }
23+
24+ this . channelType = channelType ;
25+ this . linePostURL = `https://api.line.me/v2/bot` ;
26+ }
27+
28+ public async registerWebhook ( ) {
29+ try {
30+ await axios ( {
31+ method : 'PUT' ,
32+ url : this . linePostURL + '/channel/webhook/endpoint' ,
33+ data : { endpoint : PUBLIC_DOMAIN + '/webhook/' + this . contactId } ,
34+ headers : {
35+ Authorization : `Bearer ${ this . pageToken } ` ,
36+ 'Content-Type' : 'application/json' ,
37+ } ,
38+ } ) ;
39+ console . log ( `[LIN] Registered webhook for ${ this . channelType } - ${ this . contactName } ${ this . contactId } ` ) ;
40+ } catch ( e ) {
41+ console . log ( `[LIN] Can not register webhook for ${ this . channelType } - ${ this . contactName } ${ this . contactId } ` ) ;
42+
43+ }
44+ }
45+
46+ async getLineUserID ( ) {
47+ try {
48+ const { data } = await axios ( {
49+ method : 'GET' ,
50+ url : this . linePostURL + '/info' ,
51+ headers : {
52+ Authorization : `Bearer ${ this . pageToken } ` ,
53+ } ,
54+ } ) ;
55+ if ( ! data || ! data . userId ) throw new Error ( ) ;
56+
57+ return data . userId ;
58+ } catch ( e ) {
59+ console . log ( `[LIN] Can not get user ID for ${ this . channelType } - ${ this . contactName } ${ this . contactId } ` ) ;
60+ }
61+ }
62+
63+ async prepareMessage ( req : Request , res : Response ) {
64+ try {
65+ const { destination, events } = req . body ;
66+
67+ if ( ! ( events && events [ 0 ] && events [ 0 ] . type == 'message' ) ) return ;
68+
69+ const lineUserId = await this . getLineUserID ( ) ;
70+
71+ if ( destination == lineUserId ) {
72+ const { message, source } = events [ 0 ] ;
73+
74+ await this . postMessageToBot ( { userId : source . userId , message : message . text , data : null } ) ;
75+
76+ console . log ( `[LIN] Sent message: ${ message . text } from ${ lineUserId } to Bot` ) ;
77+ }
78+ } catch ( e ) {
79+ console . log ( `[LIN] ${ this . contactId } Can not send message to Bot - ${ e . message } ` ) ;
80+ }
81+ }
82+
83+ public async sendMessageToUser ( { userId, text } ) {
84+ const lineUserId = await this . getLineUserID ( ) ;
85+ try {
86+ if ( ! text ) return ;
87+
88+ await axios ( {
89+ method : 'POST' ,
90+ url : this . linePostURL + '/message/push' ,
91+ data : {
92+ to : userId ,
93+ messages : [ { type : 'text' , text } ] ,
94+ } ,
95+ headers : {
96+ Authorization : 'Bearer ' + this . pageToken ,
97+ } ,
98+ } )
99+ } catch ( e ) {
100+ console . log ( `[LIN] Send message to User ${ lineUserId } failed` ) ;
101+ }
102+ }
103+ }
0 commit comments