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