/
eventsub.js
77 lines (73 loc) · 1.97 KB
/
eventsub.js
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
require("dotenv").config();
const axios = require("axios");
const qs = require("qs");
const { get_app_access_token } = require("./twitch");
// create twitch eventsub subscription
const create_eventsub = async (type, user_id) => {
const token = await get_app_access_token();
const url = "https://api.twitch.tv/helix/eventsub/subscriptions";
const headers = {
"Client-ID": process.env.TWITCH_CLIENT_ID,
Authorization: `Bearer ${token}`,
};
const body = {
type: type,
version: "1",
condition: {
broadcaster_user_id: user_id,
},
transport: {
method: "webhook",
callback: "https://twapi.haenu.com/teswh/event",
secret: process.env.TWITCH_WEBHOOK_SECRET,
},
};
try {
const res = await axios.post(url, body, { headers: headers });
console.log("[CREATE] ", res.data);
return res.data;
} catch (error) {
console.log(error);
}
};
// get twitch eventsub subscriptions
const get_eventsub = async () => {
const token = await get_app_access_token();
const url = "https://api.twitch.tv/helix/eventsub/subscriptions";
const headers = {
"Client-ID": process.env.TWITCH_CLIENT_ID,
Authorization: `Bearer ${token}`,
};
try {
const res = await axios.get(url, { headers: headers });
return res.data;
} catch (error) {
console.log(error);
}
};
// delete twitch eventsub subscription
const delete_eventsub = async (id) => {
const token = await get_app_access_token();
const baseUrl = `https://api.twitch.tv/helix/eventsub/subscriptions`;
const query = {
id,
};
const url = `${baseUrl}?${qs.stringify(query)}`;
const headers = {
"Client-ID": process.env.TWITCH_CLIENT_ID,
Authorization: `Bearer ${token}`,
};
try {
const res = await axios.delete(url, { headers: headers });
console.log("[DELETE] ", res.data);
return res.data;
} catch (error) {
console.log(error);
}
};
// module export
module.exports = {
create_eventsub,
get_eventsub,
delete_eventsub,
};