-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
256 lines (231 loc) · 6.46 KB
/
main.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
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
import { pool } from "./db.ts";
import { NeonData, NeonIdData } from "./NeonData.ts";
import TurndownService from "turndown";
const conn = await pool.connect();
async function requestNotif() {
try {
const getId = await conn.queryObject<NeonIdData>`
SELECT post_id
FROM poeskoclokdon
ORDER BY id DESC
LIMIT 10
`;
const res = getId.rows;
const id: string[] = [];
for await (const r of res) id.push(r.post_id);
const f = await fetch(`${Deno.env.get("GTS_API")}`, {
method: "GET",
headers: {
"Content-Type": "Application/json",
Authorization: `Bearer ${Deno.env.get("GTS_TOKEN")}`,
},
});
const data = await f.json();
const data_id: string[] = [];
for await (const d of data) data_id.push(d.id);
for await (const i of id) {
const check = data_id.includes(i);
if (check) {
console.log("data sudah ada");
} else {
for (const d of data) {
const remark: string = "USEND";
let inreply: string;
let content: string;
if (d.type === "follow") {
inreply = "null";
content = "null";
} else {
inreply = d.status.in_reply_to_id;
content = d.status.content;
}
if (d.type === "follow") {
conn.queryObject<NeonData>`
INSERT INTO poestololdon
(post_id, created_at, handler, display_name, type, remark)
VALUES
(${d.id}, ${d.created_at}, ${d.account.acct}, ${d.account.display_name}, ${d.type}, ${remark})
ON CONFLICT (post_id) DO NOTHING
`;
} else {
conn.queryObject<NeonData>`
INSERT INTO poestololdon
(inreplyto, post_id, created_at, handler, display_name, type, status, url, remark)
VALUES
(${inreply}, ${d.id}, ${d.created_at}, ${d.account.acct}, ${d.account.display_name}, ${d.type}, ${content}, ${d.status.url}, ${remark})
ON CONFLICT (post_id) DO NOTHING
`;
}
await conn.queryObject<NeonIdData>`
INSERT INTO poeskoclokdon
(post_id)
VALUES
(${d.id})
ON CONFLICT (post_id) DO NOTHING
`;
}
} // else
}
} catch (err) {
console.log(err);
}
}
async function sendNotif() {
try {
const query = await conn.queryObject<NeonData>`
SELECT * FROM poestololdon
WHERE remark = 'USEND'
ORDER BY created_at ASC
`;
const data = query.rows;
//console.log(data);
for (const d of data) {
let flag;
if (d.type === "follow") {
flag = "✋";
} else if (d.type === "mention") {
flag = "💬";
} else if (d.type === "reblog") {
flag = "🚀";
} else if (d.type === "favourite") {
flag = "💖";
}
if (d.type === "follow") {
await fetch(
`https://api.telegram.org/bot${Deno.env.get("TELE_BOT")}/sendMessage`,
{
method: "POST",
headers: {
"Content-Type": "Application/json",
},
body: JSON.stringify({
chat_id: `${Deno.env.get("TELE_CHATID")}`,
parse_mode: "Markdown",
text: `*${d.display_name}*
_${d.handler}_
${flag} ${d.type} you!
`,
}),
},
);
} else {
//console.log("not follow");
let link: string;
if (d.type != "mention") {
link = d.url;
} else {
link = `https://kauaku.us/@poes/statuses/${d.inreplyto}`;
//link = `https://dev.phanpy.social/#/kauaku.us/s/${d.inreplyto}`;
}
const td = new TurndownService();
td.addRule("Remove link", {
filter: ["a"],
replacement: function (content: string) {
return "**" + content + "**";
},
});
const t_content = td.turndown(d.status);
//console.log(t_content);
await fetch(
`https://api.telegram.org/bot${Deno.env.get("TELE_BOT")}/sendMessage`,
{
method: "POST",
headers: {
"Content-Type": "Application/json",
},
body: JSON.stringify({
chat_id: `${Deno.env.get("TELE_CHATID")}`,
parse_mode: "Markdown",
text: `*${d.display_name}*
_${d.handler}_
${flag} ${d.type} your post!
❝${t_content}❞
[source](${link})
`,
}),
},
);
}
}
} catch (error) {
console.log(error);
}
}
async function markNotif() {
try {
await conn.queryObject<NeonData>`
UPDATE poestololdon
SET remark = 'SEND'
`;
} catch (err) {
console.log(err);
}
}
async function deleteNotif() {
try {
await conn.queryObject<NeonData>`
DROP TABLE poestololdon;
CREATE TABLE poestololdon (
id SERIAL PRIMARY KEY,
inreplyto VARCHAR(100),
post_id VARCHAR(100) UNIQUE,
created_at TIMESTAMP,
handler VARCHAR(50),
display_name VARCHAR(256),
type VARCHAR(20),
status VARCHAR(10000),
url VARCHAR(1000),
ctext VARCHAR(10000),
remark VARCHAR(10)),
media VARCHAR(1000),
`;
await conn.queryObject`
DROP TABLE poeskoclokdon;
CREATE TABLE poeskoclokdon(
id SERIAL PRIMARY KEY,
post_id VARCHAR(100) UNIQUE)
`;
await requestNotif();
await markNotif();
} catch (err) {
console.log(err);
}
}
async function sendLogTele(msg: string) {
try {
const kapan = Date();
await fetch(
`https://api.telegram.org/bot${Deno.env.get("TELE_BOT")}/sendMessage`,
{
method: "POST",
headers: {
"Content-Type": "Application/json",
},
body: JSON.stringify({
chat_id: `${Deno.env.get("TELE_CHATID")}`,
parse_mode: "Markdown",
text: `🔥 *Info*
${msg}
${kapan}
`,
}),
},
);
} catch (err) {
console.log(err);
}
}
// Deno.cron("Notifikasi", "*/3 * * * *", () => {
// requestNotif();
// setTimeout(() => {
// sendNotif();
// }, 10000);
// setTimeout(() => {
// markNotif();
// }, 30000);
// });
// Deno.cron("Bersih - bersih data", "0 0 1 * *", () => {
// deleteNotif();
// sendLogTele("Proses pembersihan database telah dilakukan");
// });
Deno.serve({ port: 80 }, (_req) => new Response("Avada Kenava!"));