UNPKG

15.9 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 getGroupMembersCount(groupId) {
108 const groupMemberCount = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/group/${groupId}/members/count`);
109 return utils_1.ensureJSON(groupMemberCount);
110 }
111 async getRoomMembersCount(roomId) {
112 const roomMemberCount = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/room/${roomId}/members/count`);
113 return utils_1.ensureJSON(roomMemberCount);
114 }
115 async getGroupSummary(groupId) {
116 const groupSummary = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/group/${groupId}/summary`);
117 return utils_1.ensureJSON(groupSummary);
118 }
119 async getMessageContent(messageId) {
120 return this.http.getStream(`${endpoints_1.DATA_API_PREFIX}/message/${messageId}/content`);
121 }
122 leaveChat(chatType, chatId) {
123 return this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/${chatType}/${chatId}/leave`);
124 }
125 async leaveGroup(groupId) {
126 return this.leaveChat("group", groupId);
127 }
128 async leaveRoom(roomId) {
129 return this.leaveChat("room", roomId);
130 }
131 async getRichMenu(richMenuId) {
132 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/richmenu/${richMenuId}`);
133 return utils_1.ensureJSON(res);
134 }
135 async createRichMenu(richMenu) {
136 const res = await this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/richmenu`, richMenu);
137 return utils_1.ensureJSON(res).richMenuId;
138 }
139 async deleteRichMenu(richMenuId) {
140 return this.http.delete(`${endpoints_1.MESSAGING_API_PREFIX}/richmenu/${richMenuId}`);
141 }
142 async getRichMenuIdOfUser(userId) {
143 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/user/${userId}/richmenu`);
144 return utils_1.ensureJSON(res).richMenuId;
145 }
146 async linkRichMenuToUser(userId, richMenuId) {
147 return this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/user/${userId}/richmenu/${richMenuId}`);
148 }
149 async unlinkRichMenuFromUser(userId) {
150 return this.http.delete(`${endpoints_1.MESSAGING_API_PREFIX}/user/${userId}/richmenu`);
151 }
152 async linkRichMenuToMultipleUsers(richMenuId, userIds) {
153 return this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/richmenu/bulk/link`, {
154 richMenuId,
155 userIds,
156 });
157 }
158 async unlinkRichMenusFromMultipleUsers(userIds) {
159 return this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/richmenu/bulk/unlink`, {
160 userIds,
161 });
162 }
163 async getRichMenuImage(richMenuId) {
164 return this.http.getStream(`${endpoints_1.DATA_API_PREFIX}/richmenu/${richMenuId}/content`);
165 }
166 async setRichMenuImage(richMenuId, data, contentType) {
167 return this.http.postBinary(`${endpoints_1.DATA_API_PREFIX}/richmenu/${richMenuId}/content`, data, contentType);
168 }
169 async getRichMenuList() {
170 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/richmenu/list`);
171 return utils_1.ensureJSON(res).richmenus;
172 }
173 async setDefaultRichMenu(richMenuId) {
174 return this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/user/all/richmenu/${richMenuId}`);
175 }
176 async getDefaultRichMenuId() {
177 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/user/all/richmenu`);
178 return utils_1.ensureJSON(res).richMenuId;
179 }
180 async deleteDefaultRichMenu() {
181 return this.http.delete(`${endpoints_1.MESSAGING_API_PREFIX}/user/all/richmenu`);
182 }
183 async getLinkToken(userId) {
184 const res = await this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/user/${userId}/linkToken`);
185 return utils_1.ensureJSON(res).linkToken;
186 }
187 async getNumberOfSentReplyMessages(date) {
188 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/message/delivery/reply?date=${date}`);
189 return utils_1.ensureJSON(res);
190 }
191 async getNumberOfSentPushMessages(date) {
192 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/message/delivery/push?date=${date}`);
193 return utils_1.ensureJSON(res);
194 }
195 async getNumberOfSentMulticastMessages(date) {
196 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/message/delivery/multicast?date=${date}`);
197 return utils_1.ensureJSON(res);
198 }
199 async getNarrowcastProgress(requestId) {
200 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/message/progress/narrowcast?requestId=${requestId}`);
201 return utils_1.ensureJSON(res);
202 }
203 async getTargetLimitForAdditionalMessages() {
204 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/message/quota`);
205 return utils_1.ensureJSON(res);
206 }
207 async getNumberOfMessagesSentThisMonth() {
208 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/message/quota/consumption`);
209 return utils_1.ensureJSON(res);
210 }
211 async getNumberOfSentBroadcastMessages(date) {
212 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/message/delivery/broadcast?date=${date}`);
213 return utils_1.ensureJSON(res);
214 }
215 async getNumberOfMessageDeliveries(date) {
216 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/insight/message/delivery?date=${date}`);
217 return utils_1.ensureJSON(res);
218 }
219 async getNumberOfFollowers(date) {
220 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/insight/followers?date=${date}`);
221 return utils_1.ensureJSON(res);
222 }
223 async getFriendDemographics() {
224 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/insight/demographic`);
225 return utils_1.ensureJSON(res);
226 }
227 async getUserInteractionStatistics(requestId) {
228 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/insight/message/event?requestId=${requestId}`);
229 return utils_1.ensureJSON(res);
230 }
231 async createUploadAudienceGroup(uploadAudienceGroup) {
232 const res = await this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/audienceGroup/upload`, Object.assign({}, uploadAudienceGroup));
233 return utils_1.ensureJSON(res);
234 }
235 async createUploadAudienceGroupByFile(uploadAudienceGroup) {
236 const file = await this.http.toBuffer(uploadAudienceGroup.file);
237 const body = utils_1.createMultipartFormData(Object.assign(Object.assign({}, uploadAudienceGroup), { file }));
238 const res = await this.http.post(`${endpoints_1.DATA_API_PREFIX}/audienceGroup/upload/byFile`, body, {
239 headers: body.getHeaders(),
240 });
241 return utils_1.ensureJSON(res);
242 }
243 async updateUploadAudienceGroup(uploadAudienceGroup,
244 // for set request timeout
245 httpConfig) {
246 const res = await this.http.put(`${endpoints_1.MESSAGING_API_PREFIX}/audienceGroup/upload`, Object.assign({}, uploadAudienceGroup), httpConfig);
247 return utils_1.ensureJSON(res);
248 }
249 async updateUploadAudienceGroupByFile(uploadAudienceGroup,
250 // for set request timeout
251 httpConfig) {
252 const file = await this.http.toBuffer(uploadAudienceGroup.file);
253 const body = utils_1.createMultipartFormData(Object.assign(Object.assign({}, uploadAudienceGroup), { file }));
254 const res = await this.http.put(`${endpoints_1.DATA_API_PREFIX}/audienceGroup/upload/byFile`, body, Object.assign({ headers: body.getHeaders() }, httpConfig));
255 return utils_1.ensureJSON(res);
256 }
257 async createClickAudienceGroup(clickAudienceGroup) {
258 const res = await this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/audienceGroup/click`, Object.assign({}, clickAudienceGroup));
259 return utils_1.ensureJSON(res);
260 }
261 async createImpAudienceGroup(impAudienceGroup) {
262 const res = await this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/audienceGroup/imp`, Object.assign({}, impAudienceGroup));
263 return utils_1.ensureJSON(res);
264 }
265 async setDescriptionAudienceGroup(description, audienceGroupId) {
266 const res = await this.http.put(`${endpoints_1.MESSAGING_API_PREFIX}/audienceGroup/${audienceGroupId}/updateDescription`, {
267 description,
268 });
269 return utils_1.ensureJSON(res);
270 }
271 async deleteAudienceGroup(audienceGroupId) {
272 const res = await this.http.delete(`${endpoints_1.MESSAGING_API_PREFIX}/audienceGroup/${audienceGroupId}`);
273 return utils_1.ensureJSON(res);
274 }
275 async getAudienceGroup(audienceGroupId) {
276 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/audienceGroup/${audienceGroupId}`);
277 return utils_1.ensureJSON(res);
278 }
279 async getAudienceGroups(page, description, status, size, createRoute, includesExternalPublicGroups) {
280 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/audienceGroup/list`, {
281 page,
282 description,
283 status,
284 size,
285 createRoute,
286 includesExternalPublicGroups,
287 });
288 return utils_1.ensureJSON(res);
289 }
290 async getAudienceGroupAuthorityLevel() {
291 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/audienceGroup/authorityLevel`);
292 return utils_1.ensureJSON(res);
293 }
294 async changeAudienceGroupAuthorityLevel(authorityLevel) {
295 const res = await this.http.put(`${endpoints_1.MESSAGING_API_PREFIX}/audienceGroup/authorityLevel`, { authorityLevel });
296 return utils_1.ensureJSON(res);
297 }
298 async getBotInfo() {
299 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/info`);
300 return utils_1.ensureJSON(res);
301 }
302 async setWebhookEndpointUrl(endpoint) {
303 return this.http.put(`${endpoints_1.MESSAGING_API_PREFIX}/channel/webhook/endpoint`, { endpoint });
304 }
305 async getWebhookEndpointInfo() {
306 const res = await this.http.get(`${endpoints_1.MESSAGING_API_PREFIX}/channel/webhook/endpoint`);
307 return utils_1.ensureJSON(res);
308 }
309 async testWebhookEndpoint(endpoint) {
310 const res = await this.http.post(`${endpoints_1.MESSAGING_API_PREFIX}/channel/webhook/test`, { endpoint });
311 return utils_1.ensureJSON(res);
312 }
313}
314exports.default = Client;
315class OAuth {
316 constructor() {
317 this.http = new http_1.default();
318 }
319 issueAccessToken(client_id, client_secret) {
320 return this.http.postForm(`${endpoints_1.OAUTH_BASE_PREFIX}/accessToken`, {
321 grant_type: "client_credentials",
322 client_id,
323 client_secret,
324 });
325 }
326 revokeAccessToken(access_token) {
327 return this.http.postForm(`${endpoints_1.OAUTH_BASE_PREFIX}/revoke`, { access_token });
328 }
329 issueChannelAccessTokenV2_1(client_assertion) {
330 return this.http.postForm(`${endpoints_1.OAUTH_BASE_PREFIX_V2_1}/token`, {
331 grant_type: "client_credentials",
332 client_assertion_type: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
333 client_assertion,
334 });
335 }
336 getChannelAccessTokenKeyIdsV2_1(client_assertion) {
337 return this.http.get(`${endpoints_1.OAUTH_BASE_PREFIX_V2_1}/tokens/kid`, {
338 client_assertion_type: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
339 client_assertion,
340 });
341 }
342 revokeChannelAccessTokenV2_1(client_id, client_secret, access_token) {
343 return this.http.postForm(`${endpoints_1.OAUTH_BASE_PREFIX_V2_1}/revoke`, {
344 client_id,
345 client_secret,
346 access_token,
347 });
348 }
349}
350exports.OAuth = OAuth;