1 | import HTTPClient from "./http-axios.js";
|
2 | import * as Types from "./types.js";
|
3 | import { createMultipartFormData, ensureJSON, toArray } from "./utils.js";
|
4 | import { DATA_API_PREFIX, MESSAGING_API_PREFIX, OAUTH_BASE_PREFIX, OAUTH_BASE_PREFIX_V2_1, } from "./endpoints.js";
|
5 |
|
6 |
|
7 |
|
8 | export default class Client {
|
9 | config;
|
10 | http;
|
11 | requestOption = {};
|
12 | constructor(config) {
|
13 | if (!config.channelAccessToken) {
|
14 | throw new Error("no channel access token");
|
15 | }
|
16 | this.config = config;
|
17 | this.http = new HTTPClient({
|
18 | defaultHeaders: {
|
19 | Authorization: "Bearer " + this.config.channelAccessToken,
|
20 | },
|
21 | responseParser: this.parseHTTPResponse.bind(this),
|
22 | ...config.httpConfig,
|
23 | });
|
24 | }
|
25 | setRequestOptionOnce(option) {
|
26 | this.requestOption = option;
|
27 | }
|
28 | generateRequestConfig() {
|
29 | const config = { headers: {} };
|
30 | if (this.requestOption.retryKey) {
|
31 | config.headers["X-Line-Retry-Key"] = this.requestOption.retryKey;
|
32 | }
|
33 |
|
34 | this.requestOption = {};
|
35 | return config;
|
36 | }
|
37 | parseHTTPResponse(response) {
|
38 | const { LINE_REQUEST_ID_HTTP_HEADER_NAME } = Types;
|
39 | let resBody = {
|
40 | ...response.data,
|
41 | };
|
42 | if (response.headers[LINE_REQUEST_ID_HTTP_HEADER_NAME]) {
|
43 | resBody[LINE_REQUEST_ID_HTTP_HEADER_NAME] =
|
44 | response.headers[LINE_REQUEST_ID_HTTP_HEADER_NAME];
|
45 | }
|
46 | return resBody;
|
47 | }
|
48 | pushMessage(to, messages, notificationDisabled = false, customAggregationUnits) {
|
49 | return this.http.post(`${MESSAGING_API_PREFIX}/message/push`, {
|
50 | messages: toArray(messages),
|
51 | to,
|
52 | notificationDisabled,
|
53 | customAggregationUnits,
|
54 | }, this.generateRequestConfig());
|
55 | }
|
56 | replyMessage(replyToken, messages, notificationDisabled = false) {
|
57 | return this.http.post(`${MESSAGING_API_PREFIX}/message/reply`, {
|
58 | messages: toArray(messages),
|
59 | replyToken,
|
60 | notificationDisabled,
|
61 | });
|
62 | }
|
63 | async multicast(to, messages, notificationDisabled = false, customAggregationUnits) {
|
64 | return this.http.post(`${MESSAGING_API_PREFIX}/message/multicast`, {
|
65 | messages: toArray(messages),
|
66 | to,
|
67 | notificationDisabled,
|
68 | customAggregationUnits,
|
69 | }, this.generateRequestConfig());
|
70 | }
|
71 | async narrowcast(messages, recipient, filter, limit, notificationDisabled) {
|
72 | return this.http.post(`${MESSAGING_API_PREFIX}/message/narrowcast`, {
|
73 | messages: toArray(messages),
|
74 | recipient,
|
75 | filter,
|
76 | limit,
|
77 | notificationDisabled,
|
78 | }, this.generateRequestConfig());
|
79 | }
|
80 | async broadcast(messages, notificationDisabled = false) {
|
81 | return this.http.post(`${MESSAGING_API_PREFIX}/message/broadcast`, {
|
82 | messages: toArray(messages),
|
83 | notificationDisabled,
|
84 | }, this.generateRequestConfig());
|
85 | }
|
86 | validatePushMessageObjects(messages) {
|
87 | return this.http.post(`${MESSAGING_API_PREFIX}/message/validate/push`, {
|
88 | messages: toArray(messages),
|
89 | }, this.generateRequestConfig());
|
90 | }
|
91 | validateReplyMessageObjects(messages) {
|
92 | return this.http.post(`${MESSAGING_API_PREFIX}/message/validate/reply`, {
|
93 | messages: toArray(messages),
|
94 | });
|
95 | }
|
96 | async validateMulticastMessageObjects(messages) {
|
97 | return this.http.post(`${MESSAGING_API_PREFIX}/message/validate/multicast`, {
|
98 | messages: toArray(messages),
|
99 | }, this.generateRequestConfig());
|
100 | }
|
101 | async validateNarrowcastMessageObjects(messages) {
|
102 | return this.http.post(`${MESSAGING_API_PREFIX}/message/validate/narrowcast`, {
|
103 | messages: toArray(messages),
|
104 | }, this.generateRequestConfig());
|
105 | }
|
106 | async validateBroadcastMessageObjects(messages) {
|
107 | return this.http.post(`${MESSAGING_API_PREFIX}/message/validate/broadcast`, {
|
108 | messages: toArray(messages),
|
109 | }, this.generateRequestConfig());
|
110 | }
|
111 | validateCustomAggregationUnits(units) {
|
112 | const messages = [];
|
113 | if (units.length > 1) {
|
114 | messages.push("customAggregationUnits can only contain one unit");
|
115 | }
|
116 | units.forEach((unit, index) => {
|
117 | if (unit.length > 30) {
|
118 | messages.push(`customAggregationUnits[${index}] must be less than or equal to 30 characters`);
|
119 | }
|
120 | if (!/^[a-zA-Z0-9_]+$/.test(unit)) {
|
121 | messages.push(`customAggregationUnits[${index}] must be alphanumeric characters or underscores`);
|
122 | }
|
123 | });
|
124 | return {
|
125 | messages,
|
126 | valid: messages.length === 0,
|
127 | };
|
128 | }
|
129 | async getProfile(userId) {
|
130 | const profile = await this.http.get(`${MESSAGING_API_PREFIX}/profile/${userId}`);
|
131 | return ensureJSON(profile);
|
132 | }
|
133 | async getChatMemberProfile(chatType, chatId, userId) {
|
134 | const profile = await this.http.get(`${MESSAGING_API_PREFIX}/${chatType}/${chatId}/member/${userId}`);
|
135 | return ensureJSON(profile);
|
136 | }
|
137 | async getGroupMemberProfile(groupId, userId) {
|
138 | return this.getChatMemberProfile("group", groupId, userId);
|
139 | }
|
140 | async getRoomMemberProfile(roomId, userId) {
|
141 | return this.getChatMemberProfile("room", roomId, userId);
|
142 | }
|
143 | async getChatMemberIds(chatType, chatId) {
|
144 | let memberIds = [];
|
145 | let start;
|
146 | do {
|
147 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/${chatType}/${chatId}/members/ids`, start ? { start } : null);
|
148 | ensureJSON(res);
|
149 | memberIds = memberIds.concat(res.memberIds);
|
150 | start = res.next;
|
151 | } while (start);
|
152 | return memberIds;
|
153 | }
|
154 | async getGroupMemberIds(groupId) {
|
155 | return this.getChatMemberIds("group", groupId);
|
156 | }
|
157 | async getRoomMemberIds(roomId) {
|
158 | return this.getChatMemberIds("room", roomId);
|
159 | }
|
160 | async getBotFollowersIds() {
|
161 | let userIds = [];
|
162 | let start;
|
163 | do {
|
164 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/followers/ids`, start ? { start, limit: 1000 } : { limit: 1000 });
|
165 | ensureJSON(res);
|
166 | userIds = userIds.concat(res.userIds);
|
167 | start = res.next;
|
168 | } while (start);
|
169 | return userIds;
|
170 | }
|
171 | async getGroupMembersCount(groupId) {
|
172 | const groupMemberCount = await this.http.get(`${MESSAGING_API_PREFIX}/group/${groupId}/members/count`);
|
173 | return ensureJSON(groupMemberCount);
|
174 | }
|
175 | async getRoomMembersCount(roomId) {
|
176 | const roomMemberCount = await this.http.get(`${MESSAGING_API_PREFIX}/room/${roomId}/members/count`);
|
177 | return ensureJSON(roomMemberCount);
|
178 | }
|
179 | async getGroupSummary(groupId) {
|
180 | const groupSummary = await this.http.get(`${MESSAGING_API_PREFIX}/group/${groupId}/summary`);
|
181 | return ensureJSON(groupSummary);
|
182 | }
|
183 | async getMessageContent(messageId) {
|
184 | return this.http.getStream(`${DATA_API_PREFIX}/message/${messageId}/content`);
|
185 | }
|
186 | leaveChat(chatType, chatId) {
|
187 | return this.http.post(`${MESSAGING_API_PREFIX}/${chatType}/${chatId}/leave`);
|
188 | }
|
189 | async leaveGroup(groupId) {
|
190 | return this.leaveChat("group", groupId);
|
191 | }
|
192 | async leaveRoom(roomId) {
|
193 | return this.leaveChat("room", roomId);
|
194 | }
|
195 | async getRichMenu(richMenuId) {
|
196 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/richmenu/${richMenuId}`);
|
197 | return ensureJSON(res);
|
198 | }
|
199 | async createRichMenu(richMenu) {
|
200 | const res = await this.http.post(`${MESSAGING_API_PREFIX}/richmenu`, richMenu);
|
201 | return ensureJSON(res).richMenuId;
|
202 | }
|
203 | async deleteRichMenu(richMenuId) {
|
204 | return this.http.delete(`${MESSAGING_API_PREFIX}/richmenu/${richMenuId}`);
|
205 | }
|
206 | async getRichMenuAliasList() {
|
207 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/richmenu/alias/list`);
|
208 | return ensureJSON(res);
|
209 | }
|
210 | async getRichMenuAlias(richMenuAliasId) {
|
211 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/richmenu/alias/${richMenuAliasId}`);
|
212 | return ensureJSON(res);
|
213 | }
|
214 | async createRichMenuAlias(richMenuId, richMenuAliasId) {
|
215 | const res = await this.http.post(`${MESSAGING_API_PREFIX}/richmenu/alias`, {
|
216 | richMenuId,
|
217 | richMenuAliasId,
|
218 | });
|
219 | return ensureJSON(res);
|
220 | }
|
221 | async deleteRichMenuAlias(richMenuAliasId) {
|
222 | const res = this.http.delete(`${MESSAGING_API_PREFIX}/richmenu/alias/${richMenuAliasId}`);
|
223 | return ensureJSON(res);
|
224 | }
|
225 | async updateRichMenuAlias(richMenuAliasId, richMenuId) {
|
226 | const res = await this.http.post(`${MESSAGING_API_PREFIX}/richmenu/alias/${richMenuAliasId}`, {
|
227 | richMenuId,
|
228 | });
|
229 | return ensureJSON(res);
|
230 | }
|
231 | async getRichMenuIdOfUser(userId) {
|
232 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/user/${userId}/richmenu`);
|
233 | return ensureJSON(res).richMenuId;
|
234 | }
|
235 | async linkRichMenuToUser(userId, richMenuId) {
|
236 | return this.http.post(`${MESSAGING_API_PREFIX}/user/${userId}/richmenu/${richMenuId}`);
|
237 | }
|
238 | async unlinkRichMenuFromUser(userId) {
|
239 | return this.http.delete(`${MESSAGING_API_PREFIX}/user/${userId}/richmenu`);
|
240 | }
|
241 | async linkRichMenuToMultipleUsers(richMenuId, userIds) {
|
242 | return this.http.post(`${MESSAGING_API_PREFIX}/richmenu/bulk/link`, {
|
243 | richMenuId,
|
244 | userIds,
|
245 | });
|
246 | }
|
247 | async unlinkRichMenusFromMultipleUsers(userIds) {
|
248 | return this.http.post(`${MESSAGING_API_PREFIX}/richmenu/bulk/unlink`, {
|
249 | userIds,
|
250 | });
|
251 | }
|
252 | async getRichMenuImage(richMenuId) {
|
253 | return this.http.getStream(`${DATA_API_PREFIX}/richmenu/${richMenuId}/content`);
|
254 | }
|
255 | async setRichMenuImage(richMenuId, data, contentType) {
|
256 | return this.http.postBinary(`${DATA_API_PREFIX}/richmenu/${richMenuId}/content`, data, contentType);
|
257 | }
|
258 | async getRichMenuList() {
|
259 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/richmenu/list`);
|
260 | return ensureJSON(res).richmenus;
|
261 | }
|
262 | async setDefaultRichMenu(richMenuId) {
|
263 | return this.http.post(`${MESSAGING_API_PREFIX}/user/all/richmenu/${richMenuId}`);
|
264 | }
|
265 | async getDefaultRichMenuId() {
|
266 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/user/all/richmenu`);
|
267 | return ensureJSON(res).richMenuId;
|
268 | }
|
269 | async deleteDefaultRichMenu() {
|
270 | return this.http.delete(`${MESSAGING_API_PREFIX}/user/all/richmenu`);
|
271 | }
|
272 | async getLinkToken(userId) {
|
273 | const res = await this.http.post(`${MESSAGING_API_PREFIX}/user/${userId}/linkToken`);
|
274 | return ensureJSON(res).linkToken;
|
275 | }
|
276 | async getNumberOfSentReplyMessages(date) {
|
277 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/message/delivery/reply?date=${date}`);
|
278 | return ensureJSON(res);
|
279 | }
|
280 | async getNumberOfSentPushMessages(date) {
|
281 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/message/delivery/push?date=${date}`);
|
282 | return ensureJSON(res);
|
283 | }
|
284 | async getNumberOfSentMulticastMessages(date) {
|
285 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/message/delivery/multicast?date=${date}`);
|
286 | return ensureJSON(res);
|
287 | }
|
288 | async getNarrowcastProgress(requestId) {
|
289 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/message/progress/narrowcast?requestId=${requestId}`);
|
290 | return ensureJSON(res);
|
291 | }
|
292 | async getTargetLimitForAdditionalMessages() {
|
293 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/message/quota`);
|
294 | return ensureJSON(res);
|
295 | }
|
296 | async getNumberOfMessagesSentThisMonth() {
|
297 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/message/quota/consumption`);
|
298 | return ensureJSON(res);
|
299 | }
|
300 | async getNumberOfSentBroadcastMessages(date) {
|
301 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/message/delivery/broadcast?date=${date}`);
|
302 | return ensureJSON(res);
|
303 | }
|
304 | async getNumberOfMessageDeliveries(date) {
|
305 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/insight/message/delivery?date=${date}`);
|
306 | return ensureJSON(res);
|
307 | }
|
308 | async getNumberOfFollowers(date) {
|
309 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/insight/followers?date=${date}`);
|
310 | return ensureJSON(res);
|
311 | }
|
312 | async getFriendDemographics() {
|
313 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/insight/demographic`);
|
314 | return ensureJSON(res);
|
315 | }
|
316 | async getUserInteractionStatistics(requestId) {
|
317 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/insight/message/event?requestId=${requestId}`);
|
318 | return ensureJSON(res);
|
319 | }
|
320 | async getStatisticsPerUnit(customAggregationUnit, from, to) {
|
321 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/insight/message/event/aggregation?customAggregationUnit=${customAggregationUnit}&from=${from}&to=${to}`);
|
322 | return ensureJSON(res);
|
323 | }
|
324 | async createUploadAudienceGroup(uploadAudienceGroup) {
|
325 | const res = await this.http.post(`${MESSAGING_API_PREFIX}/audienceGroup/upload`, {
|
326 | ...uploadAudienceGroup,
|
327 | });
|
328 | return ensureJSON(res);
|
329 | }
|
330 | async createUploadAudienceGroupByFile(uploadAudienceGroup) {
|
331 | const file = await this.http.toBuffer(uploadAudienceGroup.file);
|
332 | const body = createMultipartFormData({ ...uploadAudienceGroup, file });
|
333 | const res = await this.http.postFormMultipart(`${DATA_API_PREFIX}/audienceGroup/upload/byFile`, body);
|
334 | return ensureJSON(res);
|
335 | }
|
336 | async updateUploadAudienceGroup(uploadAudienceGroup,
|
337 |
|
338 | httpConfig) {
|
339 | const res = await this.http.put(`${MESSAGING_API_PREFIX}/audienceGroup/upload`, {
|
340 | ...uploadAudienceGroup,
|
341 | }, httpConfig);
|
342 | return ensureJSON(res);
|
343 | }
|
344 | async updateUploadAudienceGroupByFile(uploadAudienceGroup,
|
345 |
|
346 | httpConfig) {
|
347 | const file = await this.http.toBuffer(uploadAudienceGroup.file);
|
348 | const body = createMultipartFormData({ ...uploadAudienceGroup, file });
|
349 | const res = await this.http.putFormMultipart(`${DATA_API_PREFIX}/audienceGroup/upload/byFile`, body, httpConfig);
|
350 | return ensureJSON(res);
|
351 | }
|
352 | async createClickAudienceGroup(clickAudienceGroup) {
|
353 | const res = await this.http.post(`${MESSAGING_API_PREFIX}/audienceGroup/click`, {
|
354 | ...clickAudienceGroup,
|
355 | });
|
356 | return ensureJSON(res);
|
357 | }
|
358 | async createImpAudienceGroup(impAudienceGroup) {
|
359 | const res = await this.http.post(`${MESSAGING_API_PREFIX}/audienceGroup/imp`, {
|
360 | ...impAudienceGroup,
|
361 | });
|
362 | return ensureJSON(res);
|
363 | }
|
364 | async setDescriptionAudienceGroup(description, audienceGroupId) {
|
365 | const res = await this.http.put(`${MESSAGING_API_PREFIX}/audienceGroup/${audienceGroupId}/updateDescription`, {
|
366 | description,
|
367 | });
|
368 | return ensureJSON(res);
|
369 | }
|
370 | async deleteAudienceGroup(audienceGroupId) {
|
371 | const res = await this.http.delete(`${MESSAGING_API_PREFIX}/audienceGroup/${audienceGroupId}`);
|
372 | return ensureJSON(res);
|
373 | }
|
374 | async getAudienceGroup(audienceGroupId) {
|
375 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/audienceGroup/${audienceGroupId}`);
|
376 | return ensureJSON(res);
|
377 | }
|
378 | async getAudienceGroups(page, description, status, size, createRoute, includesExternalPublicGroups) {
|
379 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/audienceGroup/list`, {
|
380 | page,
|
381 | description,
|
382 | status,
|
383 | size,
|
384 | createRoute,
|
385 | includesExternalPublicGroups,
|
386 | });
|
387 | return ensureJSON(res);
|
388 | }
|
389 | async getAudienceGroupAuthorityLevel() {
|
390 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/audienceGroup/authorityLevel`);
|
391 | return ensureJSON(res);
|
392 | }
|
393 | async changeAudienceGroupAuthorityLevel(authorityLevel) {
|
394 | const res = await this.http.put(`${MESSAGING_API_PREFIX}/audienceGroup/authorityLevel`, { authorityLevel });
|
395 | return ensureJSON(res);
|
396 | }
|
397 | async getBotInfo() {
|
398 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/info`);
|
399 | return ensureJSON(res);
|
400 | }
|
401 | async setWebhookEndpointUrl(endpoint) {
|
402 | return this.http.put(`${MESSAGING_API_PREFIX}/channel/webhook/endpoint`, { endpoint });
|
403 | }
|
404 | async getWebhookEndpointInfo() {
|
405 | const res = await this.http.get(`${MESSAGING_API_PREFIX}/channel/webhook/endpoint`);
|
406 | return ensureJSON(res);
|
407 | }
|
408 | async testWebhookEndpoint(endpoint) {
|
409 | const res = await this.http.post(`${MESSAGING_API_PREFIX}/channel/webhook/test`, { endpoint });
|
410 | return ensureJSON(res);
|
411 | }
|
412 | async validateRichMenu(richMenu) {
|
413 | return await this.http.post(`${MESSAGING_API_PREFIX}/richmenu/validate`, richMenu);
|
414 | }
|
415 | }
|
416 | export class OAuth {
|
417 | http;
|
418 | constructor() {
|
419 | this.http = new HTTPClient();
|
420 | }
|
421 | issueAccessToken(client_id, client_secret) {
|
422 | return this.http.postForm(`${OAUTH_BASE_PREFIX}/accessToken`, {
|
423 | grant_type: "client_credentials",
|
424 | client_id,
|
425 | client_secret,
|
426 | });
|
427 | }
|
428 | revokeAccessToken(access_token) {
|
429 | return this.http.postForm(`${OAUTH_BASE_PREFIX}/revoke`, { access_token });
|
430 | }
|
431 | verifyAccessToken(access_token) {
|
432 | return this.http.get(`${OAUTH_BASE_PREFIX_V2_1}/verify`, { access_token });
|
433 | }
|
434 | verifyIdToken(id_token, client_id, nonce, user_id) {
|
435 | const body = {
|
436 | id_token,
|
437 | client_id,
|
438 | };
|
439 | if (nonce) {
|
440 | body.nonce = nonce;
|
441 | }
|
442 | if (user_id) {
|
443 | body.user_id = user_id;
|
444 | }
|
445 | return this.http.postForm(`${OAUTH_BASE_PREFIX_V2_1}/verify`, body);
|
446 | }
|
447 | issueChannelAccessTokenV2_1(client_assertion) {
|
448 | return this.http.postForm(`${OAUTH_BASE_PREFIX_V2_1}/token`, {
|
449 | grant_type: "client_credentials",
|
450 | client_assertion_type: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
|
451 | client_assertion,
|
452 | });
|
453 | }
|
454 | getChannelAccessTokenKeyIdsV2_1(client_assertion) {
|
455 | return this.http.get(`${OAUTH_BASE_PREFIX_V2_1}/tokens/kid`, {
|
456 | client_assertion_type: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
|
457 | client_assertion,
|
458 | });
|
459 | }
|
460 | revokeChannelAccessTokenV2_1(client_id, client_secret, access_token) {
|
461 | return this.http.postForm(`${OAUTH_BASE_PREFIX_V2_1}/revoke`, {
|
462 | client_id,
|
463 | client_secret,
|
464 | access_token,
|
465 | });
|
466 | }
|
467 | }
|
468 |
|
\ | No newline at end of file |