client.ts
5.58 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
import { Readable } from "stream";
import HTTPClient from "./http";
import * as Types from "./types";
import { JSONParseError } from "./exceptions";
function toArray<T>(maybeArr: T | T[]): T[] {
return Array.isArray(maybeArr) ? maybeArr : [maybeArr];
}
function checkJSON<T>(raw: T): T {
if (typeof raw === "object") {
return raw;
} else {
throw new JSONParseError("Failed to parse response body as JSON", raw);
}
}
type ChatType = "group" | "room";
export default class Client {
public config: Types.ClientConfig;
private http: HTTPClient;
constructor(config: Types.ClientConfig) {
if (!config.channelAccessToken) {
throw new Error("no channel access token");
}
this.config = config;
this.http = new HTTPClient(
process.env.API_BASE_URL || "https://api.line.me/v2/bot/",
{
Authorization: "Bearer " + this.config.channelAccessToken,
},
);
}
public pushMessage(
to: string,
messages: Types.Message | Types.Message[],
): Promise<any> {
return this.http.post("/message/push", {
messages: toArray(messages),
to,
});
}
public replyMessage(
replyToken: string,
messages: Types.Message | Types.Message[],
): Promise<any> {
return this.http.post("/message/reply", {
messages: toArray(messages),
replyToken,
});
}
public multicast(
to: string[],
messages: Types.Message | Types.Message[],
): Promise<any> {
return this.http.post("/message/multicast", {
messages: toArray(messages),
to,
});
}
public getProfile(userId: string): Promise<Types.Profile> {
return this.http.get<Types.Profile>(`/profile/${userId}`).then(checkJSON);
}
private getChatMemberProfile(
chatType: ChatType,
chatId: string,
userId: string,
): Promise<Types.Profile> {
return this.http
.get<Types.Profile>(`/${chatType}/${chatId}/member/${userId}`)
.then(checkJSON);
}
public getGroupMemberProfile(
groupId: string,
userId: string,
): Promise<Types.Profile> {
return this.getChatMemberProfile("group", groupId, userId);
}
public getRoomMemberProfile(
roomId: string,
userId: string,
): Promise<Types.Profile> {
return this.getChatMemberProfile("room", roomId, userId);
}
private getChatMemberIds(
chatType: ChatType,
chatId: string,
): Promise<string[]> {
const load = (start?: string): Promise<string[]> =>
this.http
.get(`/${chatType}/${chatId}/members/ids`, start ? { start } : null)
.then(checkJSON)
.then((res: { memberIds: string[]; next?: string }) => {
if (!res.next) {
return res.memberIds;
}
return load(res.next).then(extraIds =>
res.memberIds.concat(extraIds),
);
});
return load();
}
public getGroupMemberIds(groupId: string): Promise<string[]> {
return this.getChatMemberIds("group", groupId);
}
public getRoomMemberIds(roomId: string): Promise<string[]> {
return this.getChatMemberIds("room", roomId);
}
public getMessageContent(messageId: string): Promise<Readable> {
return this.http.getStream(`/message/${messageId}/content`);
}
private leaveChat(chatType: ChatType, chatId: string): Promise<any> {
return this.http.post(`/${chatType}/${chatId}/leave`);
}
public leaveGroup(groupId: string): Promise<any> {
return this.leaveChat("group", groupId);
}
public leaveRoom(roomId: string): Promise<any> {
return this.leaveChat("room", roomId);
}
public getRichMenu(richMenuId: string): Promise<Types.RichMenuResponse> {
return this.http
.get<Types.RichMenuResponse>(`/richmenu/${richMenuId}`)
.then(checkJSON);
}
public createRichMenu(richMenu: Types.RichMenu): Promise<string> {
return this.http
.post<any>("/richmenu", richMenu)
.then(checkJSON)
.then(res => res.richMenuId);
}
public deleteRichMenu(richMenuId: string): Promise<any> {
return this.http.delete(`/richmenu/${richMenuId}`);
}
public getRichMenuIdOfUser(userId: string): Promise<string> {
return this.http
.get<any>(`/user/${userId}/richmenu`)
.then(checkJSON)
.then(res => res.richMenuId);
}
public linkRichMenuToUser(userId: string, richMenuId: string): Promise<any> {
return this.http.post(`/user/${userId}/richmenu/${richMenuId}`);
}
public unlinkRichMenuFromUser(userId: string): Promise<any> {
return this.http.delete(`/user/${userId}/richmenu`);
}
public getRichMenuImage(richMenuId: string): Promise<Readable> {
return this.http.getStream(`/richmenu/${richMenuId}/content`);
}
public setRichMenuImage(
richMenuId: string,
data: Buffer | Readable,
contentType?: string,
): Promise<any> {
return this.http.postBinary(
`/richmenu/${richMenuId}/content`,
data,
contentType,
);
}
public getRichMenuList(): Promise<Array<Types.RichMenuResponse>> {
return this.http
.get<any>(`/richmenu/list`)
.then(checkJSON)
.then(res => res.richmenus);
}
public setDefaultRichMenu(richMenuId: string): Promise<{}> {
return this.http.post(`/user/all/richmenu/${richMenuId}`);
}
public getDefaultRichMenuId(): Promise<string> {
return this.http
.get<any>("/user/all/richmenu")
.then(checkJSON)
.then(res => res.richMenuId);
}
public deleteDefaultRichMenu(): Promise<{}> {
return this.http.delete("/user/all/richmenu");
}
public getLinkToken(userId: string): Promise<string> {
return this.http
.post<any>(`/user/${userId}/linkToken`)
.then(checkJSON)
.then(res => res.linkToken);
}
}