-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
116 lines (106 loc) · 3.77 KB
/
index.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
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
const TelegramBot = require("node-telegram-bot-api");
const mongoose = require("mongoose");
const { Notes } = require("./Modules/Notes");
const chuck = require("./Modules/chuck");
const greet = require("./Modules/greeting");
const introduction = require("./Modules/introduction");
const meetups = require("./Modules/meetups");
const quote = require("./Modules/quote");
const xkcd = require("./Modules/xkcd");
const cstrip = require("./Modules/commitstrip");
const events = require("./Modules/Events");
const cron = require("node-cron");
require("dotenv").config();
const token = process.env.TELEGRAM_API_TOKEN;
// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, { polling: true });
//Establish connection to the database
mongoose.connect("mongodb://localhost:27017/Odin_bot");
// Matches "/echo [whatever]"
bot.onText(/\/echo (.+)/, (msg, match) => {
// 'msg' is the received Message from Telegram
// 'match' is the result of executing the regexp above on the text content
// of the message
const chatId = msg.chat.id;
const resp = match[1]; // the captured "whatever"
// send back the matched "whatever" to the chat
bot.sendMessage(chatId, resp);
});
// Listen for any kind of message. There are different kinds of
// messages.
bot.onText(/\/(heyodin|start)/, msg => {
//heylisa script
let out =
"Hey, I am odin. You can interact with me using the following commands:\n";
let c0 = "/meetups - Find a list of upcoming meetups in NCR.\n";
let c1 = "/xkcd - I will send an awesome xkcd script all your way.\n";
let c2 = "/chuck - A fun Chuck Norris joke all your way.\n";
let c3 =
"/chuck <firstname> - A Chuck Norris joke with the named person as the main character.\n";
let c4 = "/quote - An inspirational quote all your way.\n";
let c5 = "/save <note-name> <note-content> - Saves note to the database.\n";
let c6 = "/saved - Sends the list of saved notes from the database.\n";
let c7 = "/delete <note-name>` - Deletes note from the database.\n";
out = out + c0 + c1 + c2 + c3 + c4 + c5 + c6 + c7;
bot.sendMessage(msg.chat.id, out);
});
bot.on("message", msg => {
greet(bot, msg);
introduction(bot, msg);
if (msg.text) {
chuck(bot, msg);
cstrip(bot, msg);
xkcd(bot, msg);
quote(bot, msg);
meetups(bot, msg);
Notes(bot, msg);
events(bot, msg);
}
});
//Check if new user introduced himself or not
//Cron Job to carry the check every 2 hour
cron.schedule("0 */2 * * *", async () => {
await introduction.NewUser.find({ introduction: false }, (err, users) => {
if (err) {
console.log(err);
}
users.map(usr => {
console.log(usr);
let d = new Date();
const DAY = 86400000;
if (d.getTime() - usr.JoinDate >= DAY) {
console.log("Kicking user ", usr.UserName);
bot.kickChatMember(usr.ChatID, usr.UserId);
} else if (d.getTime() - usr.JoinDate >= DAY / 2) {
bot.sendMessage(
usr.ChatID,
"WARNING for @" +
usr.UserName +
"\nKindly introduce yourself within the next 12 hours or you will be kicked"
);
}
});
});
});
//Check every at 12 for upcoming events
cron.schedule("0 12 * * *", async () => {
await events.db.find({}, (err, event) => {
if (err) {
console.log(err);
}
event.map(evt => {
let e = new Date(evt.Date);
let d = new Date();
const DAY = 86400000;
if (d.getTime() >= e.getTime() && d.getTime() - e.getTime() <= DAY) {
bot.sendMessage(
evt.ChatID,
"Meetup on " + evt.Name + " today at " + evt.Venue
);
} else if (d.getTime() - e.getTime() <= DAY) {
bot.sendMessage(evt.ChatID, "Meetup on " + evt.Name + " tomorrow");
}
});
});
});
bot.on("polling_error", err => console.log(err));