UNPKG

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