-
Notifications
You must be signed in to change notification settings - Fork 5
/
Client.ts
245 lines (214 loc) · 8.2 KB
/
Client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import * as Line from '@line/bot-sdk';
export class Client {
private static messagingUrl = 'https://api.line.me/v2/bot/';
private static dataUrl = 'https://api-data.line.me/v2/bot/';
constructor(private config: Line.ClientConfig) {}
public pushMessage(
to: string,
messages: Line.Message | Line.Message[],
notificationDisabled = false
): Line.MessageAPIResponseBase {
return this.httpPost(this.pushUrl(), {
messages: this.toArray(messages),
to,
notificationDisabled,
});
}
public replyMessage(
replyToken: string,
messages: Line.Message | Line.Message[],
notificationDisabled = false
): Line.MessageAPIResponseBase {
return this.httpPost(this.replyUrl(), {
messages: this.toArray(messages),
replyToken,
notificationDisabled,
});
}
public multicast(
to: string[],
messages: Line.Message | Line.Message[],
notificationDisabled = false
): Line.MessageAPIResponseBase {
return this.httpPost(this.multicastUrl(), {
messages: this.toArray(messages),
to,
notificationDisabled,
});
}
public getProfile(userId: string): Line.Profile {
return this.httpGet(this.userProfileUrl(userId));
}
public getGroupMemberProfile(groupId: string, userId: string): Line.Profile {
return this.httpGet(this.groupMemberProfileUrl(groupId, userId));
}
public getRoomMemberProfile(roomId: string, userId: string): Line.Profile {
return this.httpGet(this.roomMemberProfileUrl(roomId, userId));
}
public getProfileWithEventSource(eventSource: Line.EventSource): Line.Profile {
return this.httpGet(this.profileUrl(eventSource));
}
public getGroupMemberIds(groupId: string): string[] {
return this.httpGet(this.groupMemberIdsUrl(groupId)).memberIds;
}
public getRoomMemberIds(roomId: string): string[] {
return this.httpGet(this.roomMemberIdsUrl(roomId)).memberIds;
}
public getMessageContent(messageId: string): GoogleAppsScript.Base.Blob {
return this.httpGetStream(this.contentUrl(messageId));
}
public leaveGroup(groupId: string): unknown {
return this.httpPost(this.leaveGroupUrl(groupId));
}
public leaveRoom(roomId: string): unknown {
return this.httpPost(this.leaveRoomUrl(roomId));
}
public leaveWithEventSource(eventSource: Line.EventSource): unknown {
return this.httpPost(this.leaveUrl(eventSource));
}
public getRichMenu(richMenuId: string): Line.RichMenuResponse {
return this.httpGet(this.richMenuUrl(richMenuId));
}
public createRichMenu(richMenu: Line.RichMenu): string {
return this.httpPost(this.richMenuUrl(), richMenu).richMenuId;
}
public deleteRichMenu(richMenuId: string): unknown {
return this.httpDelete(this.richMenuUrl(richMenuId));
}
public getRichMenuIdOfUser(userId: string): string {
return this.httpGet(this.userRichMenuUrl(userId)).richMenuId;
}
public linkRichMenuToUser(userId: string, richMenuId: string): unknown {
return this.httpPost(this.userRichMenuUrl(userId, richMenuId));
}
public unlinkRichMenuFromUser(userId: string): unknown {
return this.httpDelete(this.userRichMenuUrl(userId));
}
public getRichMenuImage(richMenuId: string): GoogleAppsScript.Base.Blob {
return this.httpGetStream(this.richMenuContentUrl(richMenuId));
}
public setRichMenuImage(
richMenuId: string,
data: GoogleAppsScript.Base.Blob,
contentType?: string
): unknown {
return this.httpPostBinary(this.richMenuContentUrl(richMenuId), data, contentType);
}
public getRichMenuList(): Line.RichMenuResponse[] {
return this.httpGet(this.richMenuListUrl()).richmenus;
}
public setDefaultRichMenu(richMenuId: string): Record<string, unknown> {
return this.httpPost(this.defaultRichMenuUrl(richMenuId));
}
public getDefaultRichMenuId(): string {
return this.httpGet(this.defaultRichMenuUrl()).richMenuId;
}
public deleteDefaultRichMenu(): Record<string, unknown> {
return this.httpDelete(this.defaultRichMenuUrl());
}
private messagingApiUrl = (path: string): string => `${Client.messagingUrl}${path}`;
private dataApiUrl = (path: string): string => `${Client.dataUrl}${path}`;
private pushUrl = () => this.messagingApiUrl('message/push');
private replyUrl = () => this.messagingApiUrl('message/reply');
private multicastUrl = () => this.messagingApiUrl('message/multicast');
private contentUrl = (messageId: string) => this.dataApiUrl(`message/${messageId}/content`);
private userProfileUrl = (userId: string) => this.messagingApiUrl(`profile/${userId}`);
private roomMemberProfileUrl = (roomId: string, userId = '') =>
this.messagingApiUrl(`room/${roomId}/member/${userId}`);
private groupMemberProfileUrl = (groupId: string, userId = '') =>
this.messagingApiUrl(`group/${groupId}/member/${userId}`);
private profileUrl = (eventSource: Line.EventSource) => {
switch (eventSource.type) {
case 'group':
return this.groupMemberProfileUrl(eventSource.groupId, eventSource.userId);
case 'room':
return this.roomMemberProfileUrl(eventSource.roomId, eventSource.userId);
default:
return this.userProfileUrl(eventSource.userId);
}
};
private groupMemberIdsUrl = (groupId: string) =>
this.messagingApiUrl(`group/${groupId}/members/ids`);
private roomMemberIdsUrl = (roomId: string) => this.messagingApiUrl(`room/${roomId}/members/ids`);
private leaveGroupUrl = (groupId: string) => this.messagingApiUrl(`group/${groupId}/leave`);
private leaveRoomUrl = (roomId: string) => this.messagingApiUrl(`room/${roomId}/leave`);
private leaveUrl = (eventSource: Line.EventSource) => {
switch (eventSource.type) {
case 'group':
return this.leaveGroupUrl(eventSource.groupId);
case 'room':
return this.leaveRoomUrl(eventSource.roomId);
default:
throw new Error('Unexpected eventSource.type to get leave url.');
}
};
private richMenuUrl = (richMenuId?: string) =>
this.messagingApiUrl(`richmenu${richMenuId ? `/${richMenuId}` : ''}`);
private richMenuListUrl = () => this.messagingApiUrl('richmenu/list');
private userRichMenuUrl = (userId: string, richMenuId?: string) =>
this.messagingApiUrl(`user/${userId}/richmenu${richMenuId ? `/${richMenuId}` : ''}`);
private richMenuContentUrl = (richMenuId: string) =>
this.dataApiUrl(`richmenu/${richMenuId}/content`);
private defaultRichMenuUrl = (richMenuId?: string) =>
this.messagingApiUrl(`user/all/richmenu${richMenuId ? `/${richMenuId}` : ''}`);
private authHeader = () => {
return {
Authorization: `Bearer ${this.config.channelAccessToken}`,
};
};
private httpGet = (url: string) => {
return JSON.parse(
UrlFetchApp.fetch(url, {
headers: this.authHeader(),
}).getContentText()
);
};
private httpGetStream = (url: string) => {
return UrlFetchApp.fetch(url, {
headers: this.authHeader(),
}).getBlob();
};
private httpPost = (url: string, payload?: Record<string, unknown>) => {
return this.parseHTTPResponse(
UrlFetchApp.fetch(url, {
contentType: 'application/json',
headers: this.authHeader(),
method: 'post',
payload: payload && JSON.stringify(payload),
})
);
};
private parseHTTPResponse = (response: GoogleAppsScript.URL_Fetch.HTTPResponse) => {
const resHeader = response.getHeaders() as Line.MessageAPIResponseBase;
const resBody = JSON.parse(response.getContentText());
return {
...resBody,
'x-line-request-id': resHeader['x-line-request-id'],
};
};
private httpPostBinary = (
url: string,
data: GoogleAppsScript.Base.Blob,
contentType?: string
) => {
return JSON.parse(
UrlFetchApp.fetch(url, {
headers: {
...this.authHeader(),
'Content-Type': contentType || data.getContentType(),
// 'Content-Length': data.getBytes().length,
},
method: 'post',
payload: data,
}).getContentText()
);
};
private httpDelete = (url: string) => {
return JSON.parse(
UrlFetchApp.fetch(url, { headers: this.authHeader(), method: 'delete' }).getContentText()
);
};
private toArray = (messages: Line.Message | Line.Message[]) => {
return Array.isArray(messages) ? messages : [messages];
};
}