-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
69 lines (64 loc) · 1.8 KB
/
index.ts
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
import fetch from "node-fetch";
interface HashMap {
[key: string]: any;
}
const parseToFormUrlEncoded = (jsonObj: HashMap): string => {
let formBody: Array<string> = [];
for (const property in jsonObj) {
const encodedKey = encodeURIComponent(property);
const encodedValue = encodeURIComponent(jsonObj[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
const stringifyFormBody = formBody.join("&");
return stringifyFormBody;
};
const initLineApi = (apiKey: string) => {
return {
sendMessage: async (msg: string) => {
const res = await fetch("https://notify-api.line.me/api/notify", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization: apiKey,
},
body: parseToFormUrlEncoded({ message: msg }),
});
return res;
},
sendImage: async (msg: string, url: string) => {
const res = await fetch("https://notify-api.line.me/api/notify", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization: apiKey,
},
body: parseToFormUrlEncoded({
message: msg,
imageThumbnail: url,
imageFullsize: url,
}),
});
return res;
},
sendSticker: async (
msg: string,
stickerPackageId: number,
stickerId: number,
) => {
const res = await fetch("https://notify-api.line.me/api/notify", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization: apiKey,
},
body: parseToFormUrlEncoded({
message: msg,
stickerPackageId,
stickerId,
}),
});
return res;
},
};
};
export = initLineApi;