client.js
7.76 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const http_1 = require("./http");
const Types = require("./types");
const exceptions_1 = require("./exceptions");
function toArray(maybeArr) {
return Array.isArray(maybeArr) ? maybeArr : [maybeArr];
}
function ensureJSON(raw) {
if (typeof raw === "object") {
return raw;
}
else {
throw new exceptions_1.JSONParseError("Failed to parse response body as JSON", raw);
}
}
const API_HOST = process.env.API_BASE_URL || "https://api.line.me/v2/";
const BOT_BASE_URL = process.env.API_BASE_URL || `${API_HOST}bot/`;
const OAUTH_BASE_URL = `${API_HOST}oauth/`;
class Client {
constructor(config) {
if (!config.channelAccessToken) {
throw new Error("no channel access token");
}
this.config = config;
this.http = new http_1.default({
baseURL: BOT_BASE_URL,
defaultHeaders: {
Authorization: "Bearer " + this.config.channelAccessToken,
},
responseParser: this.parseHTTPResponse.bind(this),
});
}
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("/message/push", {
messages: toArray(messages),
to,
notificationDisabled,
});
}
replyMessage(replyToken, messages, notificationDisabled = false) {
return this.http.post("/message/reply", {
messages: toArray(messages),
replyToken,
notificationDisabled,
});
}
async multicast(to, messages, notificationDisabled = false) {
return this.http.post("/message/multicast", {
messages: toArray(messages),
to,
notificationDisabled,
});
}
async broadcast(messages, notificationDisabled = false) {
return this.http.post("/message/broadcast", {
messages: toArray(messages),
notificationDisabled,
});
}
async getProfile(userId) {
const profile = await this.http.get(`/profile/${userId}`);
return ensureJSON(profile);
}
async getChatMemberProfile(chatType, chatId, userId) {
const profile = await this.http.get(`/${chatType}/${chatId}/member/${userId}`);
return 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(`/${chatType}/${chatId}/members/ids`, start ? { start } : null);
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 getMessageContent(messageId) {
return this.http.getStream(`/message/${messageId}/content`);
}
leaveChat(chatType, chatId) {
return this.http.post(`/${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(`/richmenu/${richMenuId}`);
return ensureJSON(res);
}
async createRichMenu(richMenu) {
const res = await this.http.post("/richmenu", richMenu);
return ensureJSON(res).richMenuId;
}
async deleteRichMenu(richMenuId) {
return this.http.delete(`/richmenu/${richMenuId}`);
}
async getRichMenuIdOfUser(userId) {
const res = await this.http.get(`/user/${userId}/richmenu`);
return ensureJSON(res).richMenuId;
}
async linkRichMenuToUser(userId, richMenuId) {
return this.http.post(`/user/${userId}/richmenu/${richMenuId}`);
}
async unlinkRichMenuFromUser(userId) {
return this.http.delete(`/user/${userId}/richmenu`);
}
async linkRichMenuToMultipleUsers(richMenuId, userIds) {
return this.http.post("/richmenu/bulk/link", {
richMenuId,
userIds,
});
}
async unlinkRichMenusFromMultipleUsers(userIds) {
return this.http.post("/richmenu/bulk/unlink", {
userIds,
});
}
async getRichMenuImage(richMenuId) {
return this.http.getStream(`/richmenu/${richMenuId}/content`);
}
async setRichMenuImage(richMenuId, data, contentType) {
return this.http.postBinary(`/richmenu/${richMenuId}/content`, data, contentType);
}
async getRichMenuList() {
const res = await this.http.get(`/richmenu/list`);
return ensureJSON(res).richmenus;
}
async setDefaultRichMenu(richMenuId) {
return this.http.post(`/user/all/richmenu/${richMenuId}`);
}
async getDefaultRichMenuId() {
const res = await this.http.get("/user/all/richmenu");
return ensureJSON(res).richMenuId;
}
async deleteDefaultRichMenu() {
return this.http.delete("/user/all/richmenu");
}
async getLinkToken(userId) {
const res = await this.http.post(`/user/${userId}/linkToken`);
return ensureJSON(res).linkToken;
}
async getNumberOfSentReplyMessages(date) {
const res = await this.http.get(`/message/delivery/reply?date=${date}`);
return ensureJSON(res);
}
async getNumberOfSentPushMessages(date) {
const res = await this.http.get(`/message/delivery/push?date=${date}`);
return ensureJSON(res);
}
async getNumberOfSentMulticastMessages(date) {
const res = await this.http.get(`/message/delivery/multicast?date=${date}`);
return ensureJSON(res);
}
async getTargetLimitForAdditionalMessages() {
const res = await this.http.get("/message/quota");
return ensureJSON(res);
}
async getNumberOfMessagesSentThisMonth() {
const res = await this.http.get("/message/quota/consumption");
return ensureJSON(res);
}
async getNumberOfSentBroadcastMessages(date) {
const res = await this.http.get(`/message/delivery/broadcast?date=${date}`);
return ensureJSON(res);
}
async getNumberOfMessageDeliveries(date) {
const res = await this.http.get(`/insight/message/delivery?date=${date}`);
return ensureJSON(res);
}
async getNumberOfFollowers(date) {
const res = await this.http.get(`/insight/followers?date=${date}`);
return ensureJSON(res);
}
async getFriendDemographics() {
const res = await this.http.get(`/insight/demographic`);
return ensureJSON(res);
}
}
exports.default = Client;
class OAuth {
constructor() {
this.http = new http_1.default({
baseURL: OAUTH_BASE_URL,
});
}
issueAccessToken(client_id, client_secret) {
return this.http.postForm("/accessToken", {
grant_type: "client_credentials",
client_id,
client_secret,
});
}
revokeAccessToken(access_token) {
return this.http.postForm("/revoke", { access_token });
}
}
exports.OAuth = OAuth;