UNPKG

17.4 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) {
41 return this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/message/push`, {
42 messages: utils_1.toArray(messages),
43 to,
44 notificationDisabled,
45 }, this.generateRequestConfig());
46 }
47 replyMessage(replyToken, messages, notificationDisabled = false) {
48 return this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/message/reply`, {
49 messages: utils_1.toArray(messages),
50 replyToken,
51 notificationDisabled,
52 });
53 }
54 async multicast(to, messages, notificationDisabled = false) {
55 return this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/message/multicast`, {
56 messages: utils_1.toArray(messages),
57 to,
58 notificationDisabled,
59 }, this.generateRequestConfig());
60 }
61 async narrowcast(messages, recipient, filter, limit, notificationDisabled) {
62 return this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/message/narrowcast`, {
63 messages: utils_1.toArray(messages),
64 recipient,
65 filter,
66 limit,
67 notificationDisabled,
68 }, this.generateRequestConfig());
69 }
70 async broadcast(messages, notificationDisabled = false) {
71 return this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/message/broadcast`, {
72 messages: utils_1.toArray(messages),
73 notificationDisabled,
74 }, this.generateRequestConfig());
75 }
76 async getProfile(userId) {
77 const profile = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/profile/${userId}`);
78 return utils_1.ensureJSON(profile);
79 }
80 async getChatMemberProfile(chatType, chatId, userId) {
81 const profile = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/${chatType}/${chatId}/member/${userId}`);
82 return utils_1.ensureJSON(profile);
83 }
84 async getGroupMemberProfile(groupId, userId) {
85 return this.getChatMemberProfile("group", groupId, userId);
86 }
87 async getRoomMemberProfile(roomId, userId) {
88 return this.getChatMemberProfile("room", roomId, userId);
89 }
90 async getChatMemberIds(chatType, chatId) {
91 let memberIds = [];
92 let start;
93 do {
94 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/${chatType}/${chatId}/members/ids`, start ? { start } : null);
95 utils_1.ensureJSON(res);
96 memberIds = memberIds.concat(res.memberIds);
97 start = res.next;
98 } while (start);
99 return memberIds;
100 }
101 async getGroupMemberIds(groupId) {
102 return this.getChatMemberIds("group", groupId);
103 }
104 async getRoomMemberIds(roomId) {
105 return this.getChatMemberIds("room", roomId);
106 }
107 async getBotFollowersIds() {
108 let userIds = [];
109 let start;
110 do {
111 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/followers/ids`, start ? { start } : null);
112 utils_1.ensureJSON(res);
113 userIds = userIds.concat(res.userIds);
114 start = res.next;
115 } while (start);
116 return userIds;
117 }
118 async getGroupMembersCount(groupId) {
119 const groupMemberCount = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/group/${groupId}/members/count`);
120 return utils_1.ensureJSON(groupMemberCount);
121 }
122 async getRoomMembersCount(roomId) {
123 const roomMemberCount = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/room/${roomId}/members/count`);
124 return utils_1.ensureJSON(roomMemberCount);
125 }
126 async getGroupSummary(groupId) {
127 const groupSummary = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/group/${groupId}/summary`);
128 return utils_1.ensureJSON(groupSummary);
129 }
130 async getMessageContent(messageId) {
131 return this.http.getStream(`${endpoints_1.DATA_API_PREFIX}/message/${messageId}/content`);
132 }
133 leaveChat(chatType, chatId) {
134 return this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/${chatType}/${chatId}/leave`);
135 }
136 async leaveGroup(groupId) {
137 return this.leaveChat("group", groupId);
138 }
139 async leaveRoom(roomId) {
140 return this.leaveChat("room", roomId);
141 }
142 async getRichMenu(richMenuId) {
143 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/richmenu/${richMenuId}`);
144 return utils_1.ensureJSON(res);
145 }
146 async createRichMenu(richMenu) {
147 const res = await this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/richmenu`, richMenu);
148 return utils_1.ensureJSON(res).richMenuId;
149 }
150 async deleteRichMenu(richMenuId) {
151 return this.http.delete(`${endpoints_1.MESSAGING_API_PREFIX}/richmenu/${richMenuId}`);
152 }
153 async getRichMenuAliasList() {
154 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/richmenu/alias/list`);
155 return utils_1.ensureJSON(res);
156 }
157 async getRichMenuAlias(richMenuAliasId) {
158 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/richmenu/alias/${richMenuAliasId}`);
159 return utils_1.ensureJSON(res);
160 }
161 async createRichMenuAlias(richMenuId, richMenuAliasId) {
162 const res = await this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/richmenu/alias`, {
163 richMenuId,
164 richMenuAliasId,
165 });
166 return utils_1.ensureJSON(res);
167 }
168 async deleteRichMenuAlias(richMenuAliasId) {
169 const res = this.http.delete(`${endpoints_1.MESSAGING_API_PREFIX}/richmenu/alias/${richMenuAliasId}`);
170 return utils_1.ensureJSON(res);
171 }
172 async updateRichMenuAlias(richMenuAliasId, richMenuId) {
173 const res = await this.http.put(`${endpoints_1.MESSAGING_API_PREFIX}/richmenu/alias/${richMenuAliasId}`, {
174 richMenuId,
175 });
176 return utils_1.ensureJSON(res);
177 }
178 async getRichMenuIdOfUser(userId) {
179 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/user/${userId}/richmenu`);
180 return utils_1.ensureJSON(res).richMenuId;
181 }
182 async linkRichMenuToUser(userId, richMenuId) {
183 return this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/user/${userId}/richmenu/${richMenuId}`);
184 }
185 async unlinkRichMenuFromUser(userId) {
186 return this.http.delete(`${endpoints_1.MESSAGING_API_PREFIX}/user/${userId}/richmenu`);
187 }
188 async linkRichMenuToMultipleUsers(richMenuId, userIds) {
189 return this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/richmenu/bulk/link`, {
190 richMenuId,
191 userIds,
192 });
193 }
194 async unlinkRichMenusFromMultipleUsers(userIds) {
195 return this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/richmenu/bulk/unlink`, {
196 userIds,
197 });
198 }
199 async getRichMenuImage(richMenuId) {
200 return this.http.getStream(`${endpoints_1.DATA_API_PREFIX}/richmenu/${richMenuId}/content`);
201 }
202 async setRichMenuImage(richMenuId, data, contentType) {
203 return this.http.postBinary(`${endpoints_1.DATA_API_PREFIX}/richmenu/${richMenuId}/content`, data, contentType);
204 }
205 async getRichMenuList() {
206 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/richmenu/list`);
207 return utils_1.ensureJSON(res).richmenus;
208 }
209 async setDefaultRichMenu(richMenuId) {
210 return this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/user/all/richmenu/${richMenuId}`);
211 }
212 async getDefaultRichMenuId() {
213 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/user/all/richmenu`);
214 return utils_1.ensureJSON(res).richMenuId;
215 }
216 async deleteDefaultRichMenu() {
217 return this.http.delete(`${endpoints_1.MESSAGING_API_PREFIX}/user/all/richmenu`);
218 }
219 async getLinkToken(userId) {
220 const res = await this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/user/${userId}/linkToken`);
221 return utils_1.ensureJSON(res).linkToken;
222 }
223 async getNumberOfSentReplyMessages(date) {
224 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/message/delivery/reply?date=${date}`);
225 return utils_1.ensureJSON(res);
226 }
227 async getNumberOfSentPushMessages(date) {
228 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/message/delivery/push?date=${date}`);
229 return utils_1.ensureJSON(res);
230 }
231 async getNumberOfSentMulticastMessages(date) {
232 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/message/delivery/multicast?date=${date}`);
233 return utils_1.ensureJSON(res);
234 }
235 async getNarrowcastProgress(requestId) {
236 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/message/progress/narrowcast?requestId=${requestId}`);
237 return utils_1.ensureJSON(res);
238 }
239 async getTargetLimitForAdditionalMessages() {
240 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/message/quota`);
241 return utils_1.ensureJSON(res);
242 }
243 async getNumberOfMessagesSentThisMonth() {
244 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/message/quota/consumption`);
245 return utils_1.ensureJSON(res);
246 }
247 async getNumberOfSentBroadcastMessages(date) {
248 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/message/delivery/broadcast?date=${date}`);
249 return utils_1.ensureJSON(res);
250 }
251 async getNumberOfMessageDeliveries(date) {
252 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/insight/message/delivery?date=${date}`);
253 return utils_1.ensureJSON(res);
254 }
255 async getNumberOfFollowers(date) {
256 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/insight/followers?date=${date}`);
257 return utils_1.ensureJSON(res);
258 }
259 async getFriendDemographics() {
260 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/insight/demographic`);
261 return utils_1.ensureJSON(res);
262 }
263 async getUserInteractionStatistics(requestId) {
264 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/insight/message/event?requestId=${requestId}`);
265 return utils_1.ensureJSON(res);
266 }
267 async createUploadAudienceGroup(uploadAudienceGroup) {
268 const res = await this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/audienceGroup/upload`, Object.assign({}, uploadAudienceGroup));
269 return utils_1.ensureJSON(res);
270 }
271 async createUploadAudienceGroupByFile(uploadAudienceGroup) {
272 const file = await this.http.toBuffer(uploadAudienceGroup.file);
273 const body = utils_1.createMultipartFormData(Object.assign(Object.assign({}, uploadAudienceGroup), { file }));
274 const res = await this.http.post(`${endpoints_1.DATA_API_PREFIX}/audienceGroup/upload/byFile`, body, {
275 headers: body.getHeaders(),
276 });
277 return utils_1.ensureJSON(res);
278 }
279 async updateUploadAudienceGroup(uploadAudienceGroup,
280 // for set request timeout
281 httpConfig) {
282 const res = await this.http.put(`${endpoints_1.MESSAGING_API_PREFIX}/audienceGroup/upload`, Object.assign({}, uploadAudienceGroup), httpConfig);
283 return utils_1.ensureJSON(res);
284 }
285 async updateUploadAudienceGroupByFile(uploadAudienceGroup,
286 // for set request timeout
287 httpConfig) {
288 const file = await this.http.toBuffer(uploadAudienceGroup.file);
289 const body = utils_1.createMultipartFormData(Object.assign(Object.assign({}, uploadAudienceGroup), { file }));
290 const res = await this.http.put(`${endpoints_1.DATA_API_PREFIX}/audienceGroup/upload/byFile`, body, Object.assign({ headers: body.getHeaders() }, httpConfig));
291 return utils_1.ensureJSON(res);
292 }
293 async createClickAudienceGroup(clickAudienceGroup) {
294 const res = await this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/audienceGroup/click`, Object.assign({}, clickAudienceGroup));
295 return utils_1.ensureJSON(res);
296 }
297 async createImpAudienceGroup(impAudienceGroup) {
298 const res = await this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/audienceGroup/imp`, Object.assign({}, impAudienceGroup));
299 return utils_1.ensureJSON(res);
300 }
301 async setDescriptionAudienceGroup(description, audienceGroupId) {
302 const res = await this.http.put(`${endpoints_1.MESSAGING_API_PREFIX}/audienceGroup/${audienceGroupId}/updateDescription`, {
303 description,
304 });
305 return utils_1.ensureJSON(res);
306 }
307 async deleteAudienceGroup(audienceGroupId) {
308 const res = await this.http.delete(`${endpoints_1.MESSAGING_API_PREFIX}/audienceGroup/${audienceGroupId}`);
309 return utils_1.ensureJSON(res);
310 }
311 async getAudienceGroup(audienceGroupId) {
312 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/audienceGroup/${audienceGroupId}`);
313 return utils_1.ensureJSON(res);
314 }
315 async getAudienceGroups(page, description, status, size, createRoute, includesExternalPublicGroups) {
316 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/audienceGroup/list`, {
317 page,
318 description,
319 status,
320 size,
321 createRoute,
322 includesExternalPublicGroups,
323 });
324 return utils_1.ensureJSON(res);
325 }
326 async getAudienceGroupAuthorityLevel() {
327 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/audienceGroup/authorityLevel`);
328 return utils_1.ensureJSON(res);
329 }
330 async changeAudienceGroupAuthorityLevel(authorityLevel) {
331 const res = await this.http.put(`${endpoints_1.MESSAGING_API_PREFIX}/audienceGroup/authorityLevel`, { authorityLevel });
332 return utils_1.ensureJSON(res);
333 }
334 async getBotInfo() {
335 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/info`);
336 return utils_1.ensureJSON(res);
337 }
338 async setWebhookEndpointUrl(endpoint) {
339 return this.http.put(`${endpoints_1.MESSAGING_API_PREFIX}/channel/webhook/endpoint`, { endpoint });
340 }
341 async getWebhookEndpointInfo() {
342 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/channel/webhook/endpoint`);
343 return utils_1.ensureJSON(res);
344 }
345 async testWebhookEndpoint(endpoint) {
346 const res = await this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/channel/webhook/test`, { endpoint });
347 return utils_1.ensureJSON(res);
348 }
349}
350exports.default = Client;
351class OAuth {
352 constructor() {
353 this.http = new http_1.default();
354 }
355 issueAccessToken(client_id, client_secret) {
356 return this.http.postForm(`${endpoints_1.OAUTH_BASE_PREFIX}/accessToken`, {
357 grant_type: "client_credentials",
358 client_id,
359 client_secret,
360 });
361 }
362 revokeAccessToken(access_token) {
363 return this.http.postForm(`${endpoints_1.OAUTH_BASE_PREFIX}/revoke`, { access_token });
364 }
365 issueChannelAccessTokenV2_1(client_assertion) {
366 return this.http.postForm(`${endpoints_1.OAUTH_BASE_PREFIX_V2_1}/token`, {
367 grant_type: "client_credentials",
368 client_assertion_type: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
369 client_assertion,
370 });
371 }
372 getChannelAccessTokenKeyIdsV2_1(client_assertion) {
373 return this.http.get(`${endpoints_1.OAUTH_BASE_PREFIX_V2_1}/tokens/kid`, {
374 client_assertion_type: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
375 client_assertion,
376 });
377 }
378 revokeChannelAccessTokenV2_1(client_id, client_secret, access_token) {
379 return this.http.postForm(`${endpoints_1.OAUTH_BASE_PREFIX_V2_1}/revoke`, {
380 client_id,
381 client_secret,
382 access_token,
383 });
384 }
385}
386exports.OAuth = OAuth;