-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.ts
120 lines (88 loc) · 3.69 KB
/
bot.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
import {Bot, config} from "./deps.ts";
import {commands} from "./commands.ts";
import {Youtube_service} from "./services/youtube_service.ts";
import {Redis_service} from "./services/redis_service.ts";
import {Cron_jobs_service} from "./services/cron_jobs_service.ts";
import {Types} from "./Interfaces/IDataChannelSaved.ts";
// Here we load the .env information
config();
const TOKEN = Deno.env.get("TELEGRAM_TOKEN");
if (!TOKEN) {
Deno.exit(0);
}
const bot: Bot = new Bot(TOKEN);
// Error handler
bot.use(async (ctx, next) => {
try {
await next(ctx);
} catch (err) {
console.error(err.message);
}
});
// Start the cron service
const cronService = new Cron_jobs_service();
await cronService.start(bot);
bot.on("text", async ctx => {
const text: string | undefined = ctx.message?.text;
const userId: number | undefined = ctx.from?.id;
let textSplit: Array<string> = (text) ? text.split(" "): [];
if (textSplit[0] === commands.start.command) {
await ctx.reply("hello, world");
} else if (textSplit[0] === commands.help.command) {
await ctx.reply(commands.help.text);
} else if (textSplit[0] === commands.addNewChannelByUsername.command || textSplit[0] === commands.addNewChannelByChannelId.command) {
let saved: boolean = false;
const youtubeService = new Youtube_service();
const type: Types = textSplit[0] === commands.addNewChannelByUsername.command ? Types.Username : Types.ChannelId;
const lastVideo: string | undefined = (type === Types.Username) ?
await youtubeService.getLastVideoPostedByUsername(textSplit[1]) :
await youtubeService.getLastVideoPostedByChannelId(textSplit[1]);
if (!lastVideo) await ctx.reply(`Can not find this channel, please check the name of the channel`);
const redisService: Redis_service = new Redis_service();
await redisService.connect();
let userData = (userId) ? await redisService.getUserData(userId!): null;
let data = (userData) ? JSON.parse(userData): null;
if (userData) {
let notExist = true;
for (const d of data) {
if (d.channel === textSplit[1]) {
await ctx.reply(`${d.channel} is already added`);
return;
}
if (d.channel === textSplit[1]) notExist = false;
}
if (userId && lastVideo && notExist) {
data.push({channel: textSplit[1], videoId: lastVideo, type});
saved = (await redisService.saveVideo(userId!, data)) ?? false;
}
if (saved) {
await ctx.reply(`Added ${textSplit[1]}!`);
return;
}
} else {
if (userId && lastVideo) saved = await redisService.saveVideo(userId!, [{channel: textSplit[1], videoId: lastVideo!, type}]) ?? false;
if (saved) {
await ctx.reply(`Added ${textSplit[1]}!`);
return;
}
else {
await ctx.reply(`Can't add this channel. Check the name of the channel please`);
return;
}
}
} else if (text === commands.listAllChannelsAdded.command) {
const redisService: Redis_service = new Redis_service();
await redisService.connect();
let userData = (userId) ? await redisService.getUserData(userId!): null;
if (userData) {
let data = JSON.parse(userData);
let channels: string = "";
for (const d of data) {
channels += `- ${d.channel} \n`;
}
await ctx.reply(channels);
return;
}
}
});
bot.launch();