client.ts
10.1 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import { Readable } from "stream";
import HTTPClient from "./http";
import * as Types from "./types";
import { JSONParseError } from "./exceptions";
import { AxiosResponse } from "axios";
function toArray<T>(maybeArr: T | T[]): T[] {
return Array.isArray(maybeArr) ? maybeArr : [maybeArr];
}
function ensureJSON<T>(raw: T): T {
if (typeof raw === "object") {
return raw;
} else {
throw new JSONParseError("Failed to parse response body as JSON", raw);
}
}
type ChatType = "group" | "room";
const API_HOST: string = process.env.API_BASE_URL || "https://api.line.me/v2/";
const BOT_BASE_URL: string = process.env.API_BASE_URL || `${API_HOST}bot/`;
const OAUTH_BASE_URL = `${API_HOST}oauth/`;
export default class Client {
public config: Types.ClientConfig;
private http: HTTPClient;
constructor(config: Types.ClientConfig) {
if (!config.channelAccessToken) {
throw new Error("no channel access token");
}
this.config = config;
this.http = new HTTPClient({
baseURL: BOT_BASE_URL,
defaultHeaders: {
Authorization: "Bearer " + this.config.channelAccessToken,
},
responseParser: this.parseHTTPResponse.bind(this),
});
}
private parseHTTPResponse(response: AxiosResponse) {
const { LINE_REQUEST_ID_HTTP_HEADER_NAME } = Types;
let resBody = {
...response.data,
};
if (response.headers[LINE_REQUEST_ID_HTTP_HEADER_NAME]) {
resBody[LINE_REQUEST_ID_HTTP_HEADER_NAME] =
response.headers[LINE_REQUEST_ID_HTTP_HEADER_NAME];
}
return resBody;
}
public pushMessage(
to: string,
messages: Types.Message | Types.Message[],
notificationDisabled: boolean = false,
): Promise<Types.MessageAPIResponseBase> {
return this.http.post("/message/push", {
messages: toArray(messages),
to,
notificationDisabled,
});
}
public replyMessage(
replyToken: string,
messages: Types.Message | Types.Message[],
notificationDisabled: boolean = false,
): Promise<Types.MessageAPIResponseBase> {
return this.http.post("/message/reply", {
messages: toArray(messages),
replyToken,
notificationDisabled,
});
}
public async multicast(
to: string[],
messages: Types.Message | Types.Message[],
notificationDisabled: boolean = false,
): Promise<Types.MessageAPIResponseBase> {
return this.http.post("/message/multicast", {
messages: toArray(messages),
to,
notificationDisabled,
});
}
public async broadcast(
messages: Types.Message | Types.Message[],
notificationDisabled: boolean = false,
): Promise<Types.MessageAPIResponseBase> {
return this.http.post("/message/broadcast", {
messages: toArray(messages),
notificationDisabled,
});
}
public async getProfile(userId: string): Promise<Types.Profile> {
const profile = await this.http.get<Types.Profile>(`/profile/${userId}`);
return ensureJSON(profile);
}
private async getChatMemberProfile(
chatType: ChatType,
chatId: string,
userId: string,
): Promise<Types.Profile> {
const profile = await this.http.get<Types.Profile>(
`/${chatType}/${chatId}/member/${userId}`,
);
return ensureJSON(profile);
}
public async getGroupMemberProfile(
groupId: string,
userId: string,
): Promise<Types.Profile> {
return this.getChatMemberProfile("group", groupId, userId);
}
public async getRoomMemberProfile(
roomId: string,
userId: string,
): Promise<Types.Profile> {
return this.getChatMemberProfile("room", roomId, userId);
}
private async getChatMemberIds(
chatType: ChatType,
chatId: string,
): Promise<string[]> {
let memberIds: string[] = [];
let start: string;
do {
const res = await this.http.get<{ memberIds: string[]; next?: string }>(
`/${chatType}/${chatId}/members/ids`,
start ? { start } : null,
);
ensureJSON(res);
memberIds = memberIds.concat(res.memberIds);
start = res.next;
} while (start);
return memberIds;
}
public async getGroupMemberIds(groupId: string): Promise<string[]> {
return this.getChatMemberIds("group", groupId);
}
public async getRoomMemberIds(roomId: string): Promise<string[]> {
return this.getChatMemberIds("room", roomId);
}
public async getMessageContent(messageId: string): Promise<Readable> {
return this.http.getStream(`/message/${messageId}/content`);
}
private leaveChat(chatType: ChatType, chatId: string): Promise<any> {
return this.http.post(`/${chatType}/${chatId}/leave`);
}
public async leaveGroup(groupId: string): Promise<any> {
return this.leaveChat("group", groupId);
}
public async leaveRoom(roomId: string): Promise<any> {
return this.leaveChat("room", roomId);
}
public async getRichMenu(
richMenuId: string,
): Promise<Types.RichMenuResponse> {
const res = await this.http.get<Types.RichMenuResponse>(
`/richmenu/${richMenuId}`,
);
return ensureJSON(res);
}
public async createRichMenu(richMenu: Types.RichMenu): Promise<string> {
const res = await this.http.post<any>("/richmenu", richMenu);
return ensureJSON(res).richMenuId;
}
public async deleteRichMenu(richMenuId: string): Promise<any> {
return this.http.delete(`/richmenu/${richMenuId}`);
}
public async getRichMenuIdOfUser(userId: string): Promise<string> {
const res = await this.http.get<any>(`/user/${userId}/richmenu`);
return ensureJSON(res).richMenuId;
}
public async linkRichMenuToUser(
userId: string,
richMenuId: string,
): Promise<any> {
return this.http.post(`/user/${userId}/richmenu/${richMenuId}`);
}
public async unlinkRichMenuFromUser(userId: string): Promise<any> {
return this.http.delete(`/user/${userId}/richmenu`);
}
public async linkRichMenuToMultipleUsers(
richMenuId: string,
userIds: string[],
): Promise<any> {
return this.http.post("/richmenu/bulk/link", {
richMenuId,
userIds,
});
}
public async unlinkRichMenusFromMultipleUsers(
userIds: string[],
): Promise<any> {
return this.http.post("/richmenu/bulk/unlink", {
userIds,
});
}
public async getRichMenuImage(richMenuId: string): Promise<Readable> {
return this.http.getStream(`/richmenu/${richMenuId}/content`);
}
public async setRichMenuImage(
richMenuId: string,
data: Buffer | Readable,
contentType?: string,
): Promise<any> {
return this.http.postBinary(
`/richmenu/${richMenuId}/content`,
data,
contentType,
);
}
public async getRichMenuList(): Promise<Array<Types.RichMenuResponse>> {
const res = await this.http.get<any>(`/richmenu/list`);
return ensureJSON(res).richmenus;
}
public async setDefaultRichMenu(richMenuId: string): Promise<{}> {
return this.http.post(`/user/all/richmenu/${richMenuId}`);
}
public async getDefaultRichMenuId(): Promise<string> {
const res = await this.http.get<any>("/user/all/richmenu");
return ensureJSON(res).richMenuId;
}
public async deleteDefaultRichMenu(): Promise<{}> {
return this.http.delete("/user/all/richmenu");
}
public async getLinkToken(userId: string): Promise<string> {
const res = await this.http.post<any>(`/user/${userId}/linkToken`);
return ensureJSON(res).linkToken;
}
public async getNumberOfSentReplyMessages(
date: string,
): Promise<Types.NumberOfMessagesSentResponse> {
const res = await this.http.get<Types.NumberOfMessagesSentResponse>(
`/message/delivery/reply?date=${date}`,
);
return ensureJSON(res);
}
public async getNumberOfSentPushMessages(
date: string,
): Promise<Types.NumberOfMessagesSentResponse> {
const res = await this.http.get<Types.NumberOfMessagesSentResponse>(
`/message/delivery/push?date=${date}`,
);
return ensureJSON(res);
}
public async getNumberOfSentMulticastMessages(
date: string,
): Promise<Types.NumberOfMessagesSentResponse> {
const res = await this.http.get<Types.NumberOfMessagesSentResponse>(
`/message/delivery/multicast?date=${date}`,
);
return ensureJSON(res);
}
public async getTargetLimitForAdditionalMessages(): Promise<
Types.TargetLimitForAdditionalMessages
> {
const res = await this.http.get<Types.TargetLimitForAdditionalMessages>(
"/message/quota",
);
return ensureJSON(res);
}
public async getNumberOfMessagesSentThisMonth(): Promise<
Types.NumberOfMessagesSentThisMonth
> {
const res = await this.http.get<Types.NumberOfMessagesSentThisMonth>(
"/message/quota/consumption",
);
return ensureJSON(res);
}
public async getNumberOfSentBroadcastMessages(
date: string,
): Promise<Types.NumberOfMessagesSentResponse> {
const res = await this.http.get<Types.NumberOfMessagesSentResponse>(
`/message/delivery/broadcast?date=${date}`,
);
return ensureJSON(res);
}
public async getNumberOfMessageDeliveries(
date: string,
): Promise<Types.NumberOfMessageDeliveriesResponse> {
const res = await this.http.get<Types.NumberOfMessageDeliveriesResponse>(
`/insight/message/delivery?date=${date}`,
);
return ensureJSON(res);
}
public async getNumberOfFollowers(
date: string,
): Promise<Types.NumberOfFollowersResponse> {
const res = await this.http.get<Types.NumberOfFollowersResponse>(
`/insight/followers?date=${date}`,
);
return ensureJSON(res);
}
public async getFriendDemographics(): Promise<Types.FriendDemoGraphics> {
const res = await this.http.get<Types.FriendDemoGraphics>(
`/insight/demographic`,
);
return ensureJSON(res);
}
}
export class OAuth {
private http: HTTPClient;
constructor() {
this.http = new HTTPClient({
baseURL: OAUTH_BASE_URL,
});
}
public issueAccessToken(
client_id: string,
client_secret: string,
): Promise<{
access_token: string;
expires_in: number;
token_type: "Bearer";
}> {
return this.http.postForm("/accessToken", {
grant_type: "client_credentials",
client_id,
client_secret,
});
}
public revokeAccessToken(access_token: string): Promise<{}> {
return this.http.postForm("/revoke", { access_token });
}
}