1 | import { Readable } from "stream";
|
2 | import HTTPClient from "./http";
|
3 | import * as Types from "./types";
|
4 | import { AxiosResponse, AxiosRequestConfig } from "axios";
|
5 | import { createMultipartFormData, ensureJSON, toArray } from "./utils";
|
6 |
|
7 | type ChatType = "group" | "room";
|
8 | type RequestOption = {
|
9 | retryKey: string;
|
10 | };
|
11 | import {
|
12 | MESSAGING_API_PREFIX,
|
13 | DATA_API_PREFIX,
|
14 | OAUTH_BASE_PREFIX,
|
15 | OAUTH_BASE_PREFIX_V2_1,
|
16 | } from "./endpoints";
|
17 |
|
18 | export default class Client {
|
19 | public config: Types.ClientConfig;
|
20 | private http: HTTPClient;
|
21 |
|
22 | private requestOption: Partial<RequestOption> = {};
|
23 |
|
24 | constructor(config: Types.ClientConfig) {
|
25 | if (!config.channelAccessToken) {
|
26 | throw new Error("no channel access token");
|
27 | }
|
28 |
|
29 | this.config = config;
|
30 | this.http = new HTTPClient({
|
31 | defaultHeaders: {
|
32 | Authorization: "Bearer " + this.config.channelAccessToken,
|
33 | },
|
34 | responseParser: this.parseHTTPResponse.bind(this),
|
35 | ...config.httpConfig,
|
36 | });
|
37 | }
|
38 | public setRequestOptionOnce(option: Partial<RequestOption>) {
|
39 | this.requestOption = option;
|
40 | }
|
41 |
|
42 | private generateRequestConfig(): Partial<AxiosRequestConfig> {
|
43 | const config: Partial<AxiosRequestConfig> = { headers: {} };
|
44 | if (this.requestOption.retryKey) {
|
45 | config.headers["X-Line-Retry-Key"] = this.requestOption.retryKey;
|
46 | }
|
47 |
|
48 |
|
49 | this.requestOption = {};
|
50 | return config;
|
51 | }
|
52 |
|
53 | private parseHTTPResponse(response: AxiosResponse) {
|
54 | const { LINE_REQUEST_ID_HTTP_HEADER_NAME } = Types;
|
55 | let resBody = {
|
56 | ...response.data,
|
57 | };
|
58 | if (response.headers[LINE_REQUEST_ID_HTTP_HEADER_NAME]) {
|
59 | resBody[LINE_REQUEST_ID_HTTP_HEADER_NAME] =
|
60 | response.headers[LINE_REQUEST_ID_HTTP_HEADER_NAME];
|
61 | }
|
62 | return resBody;
|
63 | }
|
64 |
|
65 | public pushMessage(
|
66 | to: string,
|
67 | messages: Types.Message | Types.Message[],
|
68 | notificationDisabled: boolean = false,
|
69 | ): Promise<Types.MessageAPIResponseBase> {
|
70 | return this.http.post(
|
71 | `${MESSAGING_API_PREFIX}/message/push`,
|
72 | {
|
73 | messages: toArray(messages),
|
74 | to,
|
75 | notificationDisabled,
|
76 | },
|
77 | this.generateRequestConfig(),
|
78 | );
|
79 | }
|
80 |
|
81 | public replyMessage(
|
82 | replyToken: string,
|
83 | messages: Types.Message | Types.Message[],
|
84 | notificationDisabled: boolean = false,
|
85 | ): Promise<Types.MessageAPIResponseBase> {
|
86 | return this.http.post(`${MESSAGING_API_PREFIX}/message/reply`, {
|
87 | messages: toArray(messages),
|
88 | replyToken,
|
89 | notificationDisabled,
|
90 | });
|
91 | }
|
92 |
|
93 | public async multicast(
|
94 | to: string[],
|
95 | messages: Types.Message | Types.Message[],
|
96 | notificationDisabled: boolean = false,
|
97 | ): Promise<Types.MessageAPIResponseBase> {
|
98 | return this.http.post(
|
99 | `${MESSAGING_API_PREFIX}/message/multicast`,
|
100 | {
|
101 | messages: toArray(messages),
|
102 | to,
|
103 | notificationDisabled,
|
104 | },
|
105 | this.generateRequestConfig(),
|
106 | );
|
107 | }
|
108 |
|
109 | public async narrowcast(
|
110 | messages: Types.Message | Types.Message[],
|
111 | recipient?: Types.ReceieptObject,
|
112 | filter?: { demographic: Types.DemographicFilterObject },
|
113 | limit?: { max?: number; upToRemainingQuota?: boolean },
|
114 | notificationDisabled?: boolean,
|
115 | ): Promise<Types.MessageAPIResponseBase> {
|
116 | return this.http.post(
|
117 | `${MESSAGING_API_PREFIX}/message/narrowcast`,
|
118 | {
|
119 | messages: toArray(messages),
|
120 | recipient,
|
121 | filter,
|
122 | limit,
|
123 | notificationDisabled,
|
124 | },
|
125 | this.generateRequestConfig(),
|
126 | );
|
127 | }
|
128 |
|
129 | public async broadcast(
|
130 | messages: Types.Message | Types.Message[],
|
131 | notificationDisabled: boolean = false,
|
132 | ): Promise<Types.MessageAPIResponseBase> {
|
133 | return this.http.post(
|
134 | `${MESSAGING_API_PREFIX}/message/broadcast`,
|
135 | {
|
136 | messages: toArray(messages),
|
137 | notificationDisabled,
|
138 | },
|
139 | this.generateRequestConfig(),
|
140 | );
|
141 | }
|
142 |
|
143 | public async getProfile(userId: string): Promise<Types.Profile> {
|
144 | const profile = await this.http.get<Types.Profile>(
|
145 | `${MESSAGING_API_PREFIX}/profile/${userId}`,
|
146 | );
|
147 | return ensureJSON(profile);
|
148 | }
|
149 |
|
150 | private async getChatMemberProfile(
|
151 | chatType: ChatType,
|
152 | chatId: string,
|
153 | userId: string,
|
154 | ): Promise<Types.Profile> {
|
155 | const profile = await this.http.get<Types.Profile>(
|
156 | `${MESSAGING_API_PREFIX}/${chatType}/${chatId}/member/${userId}`,
|
157 | );
|
158 | return ensureJSON(profile);
|
159 | }
|
160 |
|
161 | public async getGroupMemberProfile(
|
162 | groupId: string,
|
163 | userId: string,
|
164 | ): Promise<Types.Profile> {
|
165 | return this.getChatMemberProfile("group", groupId, userId);
|
166 | }
|
167 |
|
168 | public async getRoomMemberProfile(
|
169 | roomId: string,
|
170 | userId: string,
|
171 | ): Promise<Types.Profile> {
|
172 | return this.getChatMemberProfile("room", roomId, userId);
|
173 | }
|
174 |
|
175 | private async getChatMemberIds(
|
176 | chatType: ChatType,
|
177 | chatId: string,
|
178 | ): Promise<string[]> {
|
179 | let memberIds: string[] = [];
|
180 |
|
181 | let start: string;
|
182 | do {
|
183 | const res = await this.http.get<{ memberIds: string[]; next?: string }>(
|
184 | `${MESSAGING_API_PREFIX}/${chatType}/${chatId}/members/ids`,
|
185 | start ? { start } : null,
|
186 | );
|
187 | ensureJSON(res);
|
188 | memberIds = memberIds.concat(res.memberIds);
|
189 | start = res.next;
|
190 | } while (start);
|
191 |
|
192 | return memberIds;
|
193 | }
|
194 |
|
195 | public async getGroupMemberIds(groupId: string): Promise<string[]> {
|
196 | return this.getChatMemberIds("group", groupId);
|
197 | }
|
198 |
|
199 | public async getRoomMemberIds(roomId: string): Promise<string[]> {
|
200 | return this.getChatMemberIds("room", roomId);
|
201 | }
|
202 |
|
203 | public async getBotFollowersIds(): Promise<string[]> {
|
204 | let userIds: string[] = [];
|
205 |
|
206 | let start: string;
|
207 | do {
|
208 | const res = await this.http.get<{ userIds: string[]; next?: string }>(
|
209 | `${MESSAGING_API_PREFIX}/followers/ids`,
|
210 | start ? { start } : null,
|
211 | );
|
212 | ensureJSON(res);
|
213 | userIds = userIds.concat(res.userIds);
|
214 | start = res.next;
|
215 | } while (start);
|
216 |
|
217 | return userIds;
|
218 | }
|
219 |
|
220 | public async getGroupMembersCount(
|
221 | groupId: string,
|
222 | ): Promise<Types.MembersCountResponse> {
|
223 | const groupMemberCount = await this.http.get<Types.MembersCountResponse>(
|
224 | `${MESSAGING_API_PREFIX}/group/${groupId}/members/count`,
|
225 | );
|
226 | return ensureJSON(groupMemberCount);
|
227 | }
|
228 |
|
229 | public async getRoomMembersCount(
|
230 | roomId: string,
|
231 | ): Promise<Types.MembersCountResponse> {
|
232 | const roomMemberCount = await this.http.get<Types.MembersCountResponse>(
|
233 | `${MESSAGING_API_PREFIX}/room/${roomId}/members/count`,
|
234 | );
|
235 | return ensureJSON(roomMemberCount);
|
236 | }
|
237 |
|
238 | public async getGroupSummary(
|
239 | groupId: string,
|
240 | ): Promise<Types.GroupSummaryResponse> {
|
241 | const groupSummary = await this.http.get<Types.GroupSummaryResponse>(
|
242 | `${MESSAGING_API_PREFIX}/group/${groupId}/summary`,
|
243 | );
|
244 | return ensureJSON(groupSummary);
|
245 | }
|
246 |
|
247 | public async getMessageContent(messageId: string): Promise<Readable> {
|
248 | return this.http.getStream(
|
249 | `${DATA_API_PREFIX}/message/${messageId}/content`,
|
250 | );
|
251 | }
|
252 |
|
253 | private leaveChat(chatType: ChatType, chatId: string): Promise<any> {
|
254 | return this.http.post(
|
255 | `${MESSAGING_API_PREFIX}/${chatType}/${chatId}/leave`,
|
256 | );
|
257 | }
|
258 |
|
259 | public async leaveGroup(groupId: string): Promise<any> {
|
260 | return this.leaveChat("group", groupId);
|
261 | }
|
262 |
|
263 | public async leaveRoom(roomId: string): Promise<any> {
|
264 | return this.leaveChat("room", roomId);
|
265 | }
|
266 |
|
267 | public async getRichMenu(
|
268 | richMenuId: string,
|
269 | ): Promise<Types.RichMenuResponse> {
|
270 | const res = await this.http.get<Types.RichMenuResponse>(
|
271 | `${MESSAGING_API_PREFIX}/richmenu/${richMenuId}`,
|
272 | );
|
273 | return ensureJSON(res);
|
274 | }
|
275 |
|
276 | public async createRichMenu(richMenu: Types.RichMenu): Promise<string> {
|
277 | const res = await this.http.post<any>(
|
278 | `${MESSAGING_API_PREFIX}/richmenu`,
|
279 | richMenu,
|
280 | );
|
281 | return ensureJSON(res).richMenuId;
|
282 | }
|
283 |
|
284 | public async deleteRichMenu(richMenuId: string): Promise<any> {
|
285 | return this.http.delete(`${MESSAGING_API_PREFIX}/richmenu/${richMenuId}`);
|
286 | }
|
287 |
|
288 | public async getRichMenuAliasList(): Promise<Types.GetRichMenuAliasListResponse> {
|
289 | const res = await this.http.get<Types.GetRichMenuAliasListResponse>(
|
290 | `${MESSAGING_API_PREFIX}/richmenu/alias/list`,
|
291 | );
|
292 | return ensureJSON(res);
|
293 | }
|
294 |
|
295 | public async getRichMenuAlias(
|
296 | richMenuAliasId: string,
|
297 | ): Promise<Types.GetRichMenuAliasResponse> {
|
298 | const res = await this.http.get<Types.GetRichMenuAliasResponse>(
|
299 | `${MESSAGING_API_PREFIX}/richmenu/alias/${richMenuAliasId}`,
|
300 | );
|
301 | return ensureJSON(res);
|
302 | }
|
303 |
|
304 | public async createRichMenuAlias(
|
305 | richMenuId: string,
|
306 | richMenuAliasId: string,
|
307 | ): Promise<{}> {
|
308 | const res = await this.http.post<{}>(
|
309 | `${MESSAGING_API_PREFIX}/richmenu/alias`,
|
310 | {
|
311 | richMenuId,
|
312 | richMenuAliasId,
|
313 | },
|
314 | );
|
315 | return ensureJSON(res);
|
316 | }
|
317 |
|
318 | public async deleteRichMenuAlias(richMenuAliasId: string): Promise<{}> {
|
319 | const res = this.http.delete<{}>(
|
320 | `${MESSAGING_API_PREFIX}/richmenu/alias/${richMenuAliasId}`,
|
321 | );
|
322 | return ensureJSON(res);
|
323 | }
|
324 |
|
325 | public async updateRichMenuAlias(
|
326 | richMenuAliasId: string,
|
327 | richMenuId: string,
|
328 | ): Promise<{}> {
|
329 | const res = await this.http.put<{}>(
|
330 | `${MESSAGING_API_PREFIX}/richmenu/alias/${richMenuAliasId}`,
|
331 | {
|
332 | richMenuId,
|
333 | },
|
334 | );
|
335 | return ensureJSON(res);
|
336 | }
|
337 |
|
338 | public async getRichMenuIdOfUser(userId: string): Promise<string> {
|
339 | const res = await this.http.get<any>(
|
340 | `${MESSAGING_API_PREFIX}/user/${userId}/richmenu`,
|
341 | );
|
342 | return ensureJSON(res).richMenuId;
|
343 | }
|
344 |
|
345 | public async linkRichMenuToUser(
|
346 | userId: string,
|
347 | richMenuId: string,
|
348 | ): Promise<any> {
|
349 | return this.http.post(
|
350 | `${MESSAGING_API_PREFIX}/user/${userId}/richmenu/${richMenuId}`,
|
351 | );
|
352 | }
|
353 |
|
354 | public async unlinkRichMenuFromUser(userId: string): Promise<any> {
|
355 | return this.http.delete(`${MESSAGING_API_PREFIX}/user/${userId}/richmenu`);
|
356 | }
|
357 |
|
358 | public async linkRichMenuToMultipleUsers(
|
359 | richMenuId: string,
|
360 | userIds: string[],
|
361 | ): Promise<any> {
|
362 | return this.http.post(`${MESSAGING_API_PREFIX}/richmenu/bulk/link`, {
|
363 | richMenuId,
|
364 | userIds,
|
365 | });
|
366 | }
|
367 |
|
368 | public async unlinkRichMenusFromMultipleUsers(
|
369 | userIds: string[],
|
370 | ): Promise<any> {
|
371 | return this.http.post(`${MESSAGING_API_PREFIX}/richmenu/bulk/unlink`, {
|
372 | userIds,
|
373 | });
|
374 | }
|
375 |
|
376 | public async getRichMenuImage(richMenuId: string): Promise<Readable> {
|
377 | return this.http.getStream(
|
378 | `${DATA_API_PREFIX}/richmenu/${richMenuId}/content`,
|
379 | );
|
380 | }
|
381 |
|
382 | public async setRichMenuImage(
|
383 | richMenuId: string,
|
384 | data: Buffer | Readable,
|
385 | contentType?: string,
|
386 | ): Promise<any> {
|
387 | return this.http.postBinary(
|
388 | `${DATA_API_PREFIX}/richmenu/${richMenuId}/content`,
|
389 | data,
|
390 | contentType,
|
391 | );
|
392 | }
|
393 |
|
394 | public async getRichMenuList(): Promise<Array<Types.RichMenuResponse>> {
|
395 | const res = await this.http.get<any>(
|
396 | `${MESSAGING_API_PREFIX}/richmenu/list`,
|
397 | );
|
398 | return ensureJSON(res).richmenus;
|
399 | }
|
400 |
|
401 | public async setDefaultRichMenu(richMenuId: string): Promise<{}> {
|
402 | return this.http.post(
|
403 | `${MESSAGING_API_PREFIX}/user/all/richmenu/${richMenuId}`,
|
404 | );
|
405 | }
|
406 |
|
407 | public async getDefaultRichMenuId(): Promise<string> {
|
408 | const res = await this.http.get<any>(
|
409 | `${MESSAGING_API_PREFIX}/user/all/richmenu`,
|
410 | );
|
411 | return ensureJSON(res).richMenuId;
|
412 | }
|
413 |
|
414 | public async deleteDefaultRichMenu(): Promise<{}> {
|
415 | return this.http.delete(`${MESSAGING_API_PREFIX}/user/all/richmenu`);
|
416 | }
|
417 |
|
418 | public async getLinkToken(userId: string): Promise<string> {
|
419 | const res = await this.http.post<any>(
|
420 | `${MESSAGING_API_PREFIX}/user/${userId}/linkToken`,
|
421 | );
|
422 | return ensureJSON(res).linkToken;
|
423 | }
|
424 |
|
425 | public async getNumberOfSentReplyMessages(
|
426 | date: string,
|
427 | ): Promise<Types.NumberOfMessagesSentResponse> {
|
428 | const res = await this.http.get<Types.NumberOfMessagesSentResponse>(
|
429 | `${MESSAGING_API_PREFIX}/message/delivery/reply?date=${date}`,
|
430 | );
|
431 | return ensureJSON(res);
|
432 | }
|
433 |
|
434 | public async getNumberOfSentPushMessages(
|
435 | date: string,
|
436 | ): Promise<Types.NumberOfMessagesSentResponse> {
|
437 | const res = await this.http.get<Types.NumberOfMessagesSentResponse>(
|
438 | `${MESSAGING_API_PREFIX}/message/delivery/push?date=${date}`,
|
439 | );
|
440 | return ensureJSON(res);
|
441 | }
|
442 |
|
443 | public async getNumberOfSentMulticastMessages(
|
444 | date: string,
|
445 | ): Promise<Types.NumberOfMessagesSentResponse> {
|
446 | const res = await this.http.get<Types.NumberOfMessagesSentResponse>(
|
447 | `${MESSAGING_API_PREFIX}/message/delivery/multicast?date=${date}`,
|
448 | );
|
449 | return ensureJSON(res);
|
450 | }
|
451 |
|
452 | public async getNarrowcastProgress(
|
453 | requestId: string,
|
454 | ): Promise<Types.NarrowcastProgressResponse> {
|
455 | const res = await this.http.get<Types.NarrowcastProgressResponse>(
|
456 | `${MESSAGING_API_PREFIX}/message/progress/narrowcast?requestId=${requestId}`,
|
457 | );
|
458 | return ensureJSON(res);
|
459 | }
|
460 |
|
461 | public async getTargetLimitForAdditionalMessages(): Promise<Types.TargetLimitForAdditionalMessages> {
|
462 | const res = await this.http.get<Types.TargetLimitForAdditionalMessages>(
|
463 | `${MESSAGING_API_PREFIX}/message/quota`,
|
464 | );
|
465 | return ensureJSON(res);
|
466 | }
|
467 |
|
468 | public async getNumberOfMessagesSentThisMonth(): Promise<Types.NumberOfMessagesSentThisMonth> {
|
469 | const res = await this.http.get<Types.NumberOfMessagesSentThisMonth>(
|
470 | `${MESSAGING_API_PREFIX}/message/quota/consumption`,
|
471 | );
|
472 | return ensureJSON(res);
|
473 | }
|
474 |
|
475 | public async getNumberOfSentBroadcastMessages(
|
476 | date: string,
|
477 | ): Promise<Types.NumberOfMessagesSentResponse> {
|
478 | const res = await this.http.get<Types.NumberOfMessagesSentResponse>(
|
479 | `${MESSAGING_API_PREFIX}/message/delivery/broadcast?date=${date}`,
|
480 | );
|
481 | return ensureJSON(res);
|
482 | }
|
483 |
|
484 | public async getNumberOfMessageDeliveries(
|
485 | date: string,
|
486 | ): Promise<Types.NumberOfMessageDeliveriesResponse> {
|
487 | const res = await this.http.get<Types.NumberOfMessageDeliveriesResponse>(
|
488 | `${MESSAGING_API_PREFIX}/insight/message/delivery?date=${date}`,
|
489 | );
|
490 | return ensureJSON(res);
|
491 | }
|
492 |
|
493 | public async getNumberOfFollowers(
|
494 | date: string,
|
495 | ): Promise<Types.NumberOfFollowersResponse> {
|
496 | const res = await this.http.get<Types.NumberOfFollowersResponse>(
|
497 | `${MESSAGING_API_PREFIX}/insight/followers?date=${date}`,
|
498 | );
|
499 | return ensureJSON(res);
|
500 | }
|
501 |
|
502 | public async getFriendDemographics(): Promise<Types.FriendDemographics> {
|
503 | const res = await this.http.get<Types.FriendDemographics>(
|
504 | `${MESSAGING_API_PREFIX}/insight/demographic`,
|
505 | );
|
506 | return ensureJSON(res);
|
507 | }
|
508 |
|
509 | public async getUserInteractionStatistics(
|
510 | requestId: string,
|
511 | ): Promise<Types.UserInteractionStatistics> {
|
512 | const res = await this.http.get<Types.UserInteractionStatistics>(
|
513 | `${MESSAGING_API_PREFIX}/insight/message/event?requestId=${requestId}`,
|
514 | );
|
515 | return ensureJSON(res);
|
516 | }
|
517 |
|
518 | public async createUploadAudienceGroup(uploadAudienceGroup: {
|
519 | description: string;
|
520 | isIfaAudience?: boolean;
|
521 | audiences?: { id: string }[];
|
522 | uploadDescription?: string;
|
523 | }) {
|
524 | const res = await this.http.post<{
|
525 | audienceGroupId: number;
|
526 | type: string;
|
527 | description: string;
|
528 | created: number;
|
529 | }>(`${MESSAGING_API_PREFIX}/audienceGroup/upload`, {
|
530 | ...uploadAudienceGroup,
|
531 | });
|
532 | return ensureJSON(res);
|
533 | }
|
534 |
|
535 | public async createUploadAudienceGroupByFile(uploadAudienceGroup: {
|
536 | description: string;
|
537 | isIfaAudience?: boolean;
|
538 | uploadDescription?: string;
|
539 | file: Buffer | Readable;
|
540 | }) {
|
541 | const file = await this.http.toBuffer(uploadAudienceGroup.file);
|
542 | const body = createMultipartFormData({ ...uploadAudienceGroup, file });
|
543 | const res = await this.http.post<{
|
544 | audienceGroupId: number;
|
545 | type: "UPLOAD";
|
546 | description: string;
|
547 | created: number;
|
548 | }>(`${DATA_API_PREFIX}/audienceGroup/upload/byFile`, body, {
|
549 | headers: body.getHeaders(),
|
550 | });
|
551 | return ensureJSON(res);
|
552 | }
|
553 |
|
554 | public async updateUploadAudienceGroup(
|
555 | uploadAudienceGroup: {
|
556 | audienceGroupId: number;
|
557 | description?: string;
|
558 | uploadDescription?: string;
|
559 | audiences: { id: string }[];
|
560 | },
|
561 |
|
562 | httpConfig?: Partial<AxiosRequestConfig>,
|
563 | ) {
|
564 | const res = await this.http.put<{}>(
|
565 | `${MESSAGING_API_PREFIX}/audienceGroup/upload`,
|
566 | {
|
567 | ...uploadAudienceGroup,
|
568 | },
|
569 | httpConfig,
|
570 | );
|
571 | return ensureJSON(res);
|
572 | }
|
573 |
|
574 | public async updateUploadAudienceGroupByFile(
|
575 | uploadAudienceGroup: {
|
576 | audienceGroupId: number;
|
577 | uploadDescription?: string;
|
578 | file: Buffer | Readable;
|
579 | },
|
580 |
|
581 | httpConfig?: Partial<AxiosRequestConfig>,
|
582 | ) {
|
583 | const file = await this.http.toBuffer(uploadAudienceGroup.file);
|
584 | const body = createMultipartFormData({ ...uploadAudienceGroup, file });
|
585 |
|
586 | const res = await this.http.put<{}>(
|
587 | `${DATA_API_PREFIX}/audienceGroup/upload/byFile`,
|
588 | body,
|
589 | { headers: body.getHeaders(), ...httpConfig },
|
590 | );
|
591 | return ensureJSON(res);
|
592 | }
|
593 |
|
594 | public async createClickAudienceGroup(clickAudienceGroup: {
|
595 | description: string;
|
596 | requestId: string;
|
597 | clickUrl?: string;
|
598 | }) {
|
599 | const res = await this.http.post<
|
600 | {
|
601 | audienceGroupId: number;
|
602 | type: string;
|
603 | created: number;
|
604 | } & typeof clickAudienceGroup
|
605 | >(`${MESSAGING_API_PREFIX}/audienceGroup/click`, {
|
606 | ...clickAudienceGroup,
|
607 | });
|
608 | return ensureJSON(res);
|
609 | }
|
610 |
|
611 | public async createImpAudienceGroup(impAudienceGroup: {
|
612 | requestId: string;
|
613 | description: string;
|
614 | }) {
|
615 | const res = await this.http.post<
|
616 | {
|
617 | audienceGroupId: number;
|
618 | type: string;
|
619 | created: number;
|
620 | } & typeof impAudienceGroup
|
621 | >(`${MESSAGING_API_PREFIX}/audienceGroup/imp`, {
|
622 | ...impAudienceGroup,
|
623 | });
|
624 | return ensureJSON(res);
|
625 | }
|
626 |
|
627 | public async setDescriptionAudienceGroup(
|
628 | description: string,
|
629 | audienceGroupId: string,
|
630 | ) {
|
631 | const res = await this.http.put<{}>(
|
632 | `${MESSAGING_API_PREFIX}/audienceGroup/${audienceGroupId}/updateDescription`,
|
633 | {
|
634 | description,
|
635 | },
|
636 | );
|
637 | return ensureJSON(res);
|
638 | }
|
639 |
|
640 | public async deleteAudienceGroup(audienceGroupId: string) {
|
641 | const res = await this.http.delete<{}>(
|
642 | `${MESSAGING_API_PREFIX}/audienceGroup/${audienceGroupId}`,
|
643 | );
|
644 | return ensureJSON(res);
|
645 | }
|
646 |
|
647 | public async getAudienceGroup(audienceGroupId: string) {
|
648 | const res = await this.http.get<Types.AudienceGroup>(
|
649 | `${MESSAGING_API_PREFIX}/audienceGroup/${audienceGroupId}`,
|
650 | );
|
651 | return ensureJSON(res);
|
652 | }
|
653 |
|
654 | public async getAudienceGroups(
|
655 | page: number,
|
656 | description?: string,
|
657 | status?: Types.AudienceGroupStatus,
|
658 | size?: number,
|
659 | createRoute?: Types.AudienceGroupCreateRoute,
|
660 | includesExternalPublicGroups?: boolean,
|
661 | ) {
|
662 | const res = await this.http.get<{
|
663 | audienceGroups: Types.AudienceGroups;
|
664 | hasNextPage: boolean;
|
665 | totalCount: number;
|
666 | readWriteAudienceGroupTotalCount: number;
|
667 | page: number;
|
668 | size: number;
|
669 | }>(`${MESSAGING_API_PREFIX}/audienceGroup/list`, {
|
670 | page,
|
671 | description,
|
672 | status,
|
673 | size,
|
674 | createRoute,
|
675 | includesExternalPublicGroups,
|
676 | });
|
677 | return ensureJSON(res);
|
678 | }
|
679 |
|
680 | public async getAudienceGroupAuthorityLevel() {
|
681 | const res = await this.http.get<{
|
682 | authorityLevel: Types.AudienceGroupAuthorityLevel;
|
683 | }>(`${MESSAGING_API_PREFIX}/audienceGroup/authorityLevel`);
|
684 | return ensureJSON(res);
|
685 | }
|
686 |
|
687 | public async changeAudienceGroupAuthorityLevel(
|
688 | authorityLevel: Types.AudienceGroupAuthorityLevel,
|
689 | ) {
|
690 | const res = await this.http.put<{}>(
|
691 | `${MESSAGING_API_PREFIX}/audienceGroup/authorityLevel`,
|
692 | { authorityLevel },
|
693 | );
|
694 | return ensureJSON(res);
|
695 | }
|
696 |
|
697 | public async getBotInfo(): Promise<Types.BotInfoResponse> {
|
698 | const res = await this.http.get<Types.BotInfoResponse>(
|
699 | `${MESSAGING_API_PREFIX}/info`,
|
700 | );
|
701 | return ensureJSON(res);
|
702 | }
|
703 |
|
704 | public async setWebhookEndpointUrl(endpoint: string) {
|
705 | return this.http.put<{}>(
|
706 | `${MESSAGING_API_PREFIX}/channel/webhook/endpoint`,
|
707 | { endpoint },
|
708 | );
|
709 | }
|
710 |
|
711 | public async getWebhookEndpointInfo() {
|
712 | const res = await this.http.get<Types.WebhookEndpointInfoResponse>(
|
713 | `${MESSAGING_API_PREFIX}/channel/webhook/endpoint`,
|
714 | );
|
715 | return ensureJSON(res);
|
716 | }
|
717 |
|
718 | public async testWebhookEndpoint(endpoint?: string) {
|
719 | const res = await this.http.post<Types.TestWebhookEndpointResponse>(
|
720 | `${MESSAGING_API_PREFIX}/channel/webhook/test`,
|
721 | { endpoint },
|
722 | );
|
723 | return ensureJSON(res);
|
724 | }
|
725 | }
|
726 |
|
727 | export class OAuth {
|
728 | private http: HTTPClient;
|
729 |
|
730 | constructor() {
|
731 | this.http = new HTTPClient();
|
732 | }
|
733 |
|
734 | public issueAccessToken(
|
735 | client_id: string,
|
736 | client_secret: string,
|
737 | ): Promise<Types.ChannelAccessToken> {
|
738 | return this.http.postForm(`${OAUTH_BASE_PREFIX}/accessToken`, {
|
739 | grant_type: "client_credentials",
|
740 | client_id,
|
741 | client_secret,
|
742 | });
|
743 | }
|
744 |
|
745 | public revokeAccessToken(access_token: string): Promise<{}> {
|
746 | return this.http.postForm(`${OAUTH_BASE_PREFIX}/revoke`, { access_token });
|
747 | }
|
748 |
|
749 | public issueChannelAccessTokenV2_1(
|
750 | client_assertion: string,
|
751 | ): Promise<Types.ChannelAccessToken> {
|
752 | return this.http.postForm(`${OAUTH_BASE_PREFIX_V2_1}/token`, {
|
753 | grant_type: "client_credentials",
|
754 | client_assertion_type:
|
755 | "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
|
756 | client_assertion,
|
757 | });
|
758 | }
|
759 |
|
760 | public getChannelAccessTokenKeyIdsV2_1(
|
761 | client_assertion: string,
|
762 | ): Promise<{ key_ids: string[] }> {
|
763 | return this.http.get(`${OAUTH_BASE_PREFIX_V2_1}/tokens/kid`, {
|
764 | client_assertion_type:
|
765 | "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
|
766 | client_assertion,
|
767 | });
|
768 | }
|
769 |
|
770 | public revokeChannelAccessTokenV2_1(
|
771 | client_id: string,
|
772 | client_secret: string,
|
773 | access_token: string,
|
774 | ): Promise<{}> {
|
775 | return this.http.postForm(`${OAUTH_BASE_PREFIX_V2_1}/revoke`, {
|
776 | client_id,
|
777 | client_secret,
|
778 | access_token,
|
779 | });
|
780 | }
|
781 | }
|