-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
277 lines (254 loc) · 9.08 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
require('dotenv').config();
const Telegraf = require('telegraf');
const Telegram = require('telegraf/telegram');
const mongoose = require('mongoose');
const util = require('util');
const commandArgsMiddleware = require('./middleware/commandArg.middleware');
// const { sync_unreplied_people_db, get_unreplied_people_db } = require('./database/sync_unreplied_people_to_db');
const unreplied_people_model = require('./database/models/unreplied_people.models');
const {
start,
sticker,
contactus,
aboutus,
developer,
unreplied,
status,
history
} = require('./commands');
var unreplied_people = [];
//connect to mongodb
mongoose
.connect(process.env.MONGODB_URL, { useNewUrlParser: true, useFindAndModify: false })
.then(() => {
console.log("Connected to MongoDB...");
})
.catch(err => console.error("Could not connect to MongoDB..."));
const bot = new Telegraf(process.env.TELEGRAM_BOT_API);
const telegram = new Telegram(process.env.TELEGRAM_BOT_API, {
agent: null, // https.Agent instance, allows custom proxy, certificate, keep alive, etc.
webhookReply: true // Reply via webhook
});
async function syncDB() {
let data = await unreplied_people_model.find();
unreplied_people = data[0].unreplied_people;
}
async function sync_unreplied_people_db(unreplied_people) {
await unreplied_people_model.findOneAndUpdate({}, { unreplied_people: unreplied_people });
}
syncDB().then(() => {
console.log("from db:", unreplied_people)
}).catch(err => { console.log(err) });
bot.use(commandArgsMiddleware());
start(bot);
sticker(bot);
contactus(bot);
aboutus(bot);
developer(bot);
status(bot);
// history(bot, unreplied_people);
// unreplied(bot, unreplied_people);
bot.command('history', async (ctx) => {
try {
if ((ctx.update.message.chat.id == process.env.OUTREACH_GROUP_ID) && ctx.state.command.person) {
for (let i = 0; i < unreplied_people.length; i++) {
if (unreplied_people[i].name.toLowerCase() == ctx.state.command.person.toLowerCase()) {
ctx.replyWithMarkdown("Unreplied message from " + unreplied_people[i].name + "\n" + unreplied_people[i].message);
break;
}
if (unreplied_people[i].username.toLowerCase() == ctx.state.command.person.toLowerCase()) {
ctx.reply("Unreplied message from " + unreplied_people[i].username + "\n" + unreplied_people[i].message);
break;
}
}
} else {
ctx.reply("Unknown command. please follow this format /history username or /history name");
}
} catch (err) {
var errorMessage = `
*_WARNING_*\n
```
`${err}`
```
`;
telegram.sendMessage(process.env.OUTREACH_GROUP_ID, errorMessage).then().catch(err => { console.log(err) });
}
});
bot.command('unreplied', async (ctx) => {
try {
if (ctx.update.message.chat.id == process.env.OUTREACH_GROUP_ID) {
if (unreplied_people.length) {
var replyMessage = "List of unreplied individuals:\n";
unreplied_people.map((unreplied_people, index) => {
unreplied_people.username ?
replyMessage = replyMessage + (index + 1 + ". " + unreplied_people.name + " - @" + unreplied_people.username + "\n") :
replyMessage = replyMessage + (index + 1 + ". " + unreplied_people.name + "\n")
});
ctx.reply(replyMessage);
}
else {
ctx.reply("There is no unreplied message");
}
}
} catch (err) {
var errorMessage = `
*_WARNING_*\n
```
`${err}`
```
`;
telegram.sendMessage(process.env.OUTREACH_GROUP_ID, errorMessage).then().catch(err => { console.log(err) });
}
});
bot.command('reply', async (ctx) => {
try {
if ((ctx.update.message.chat.id == process.env.OUTREACH_GROUP_ID) && ctx.state.command.person) {
// console.log("reply", ctx.state.command);
var reply_id = "";
for (let i = 0; i < unreplied_people.length; i++) {
if (unreplied_people[i].name.toLowerCase() == ctx.state.command.person.toLowerCase()) {
reply_id = unreplied_people[i].chatId;
}
}
if (!reply_id) {
for (let i = 0; i < unreplied_people.length; i++) {
if (unreplied_people[i].username.toLowerCase() == ctx.state.command.person.toLowerCase()) {
reply_id = unreplied_people[i].chatId;
}
}
}
if (reply_id && ctx.state.command.message) {
telegram.sendMessage(reply_id, ctx.state.command.message).then().catch(err => { console.log(err) });
for (var i = 0; i < unreplied_people.length; i++) {
if (unreplied_people[i].chatId === reply_id) {
var currentTime = new Date()
var tempMessage = `\n*Replied \[${currentTime.toLocaleString()}\]: ${ctx.state.command.message}*\n`;
unreplied_people[i].message.push(tempMessage);
break;
}
}
}
await sync_unreplied_people_db(unreplied_people);
} else {
ctx.reply("Unknown command. please follow this format /reply username message or /reply name message");
}
} catch (err) {
var errorMessage = `
*_WARNING_*\n
```
`${err}`
```
`;
telegram.sendMessage(process.env.OUTREACH_GROUP_ID, errorMessage).then().catch(err => { console.log(err) });
}
});
bot.command('close', async (ctx) => {
try {
if ((ctx.update.message.chat.id == process.env.OUTREACH_GROUP_ID) && ctx.state.command.person) {
for (let i = 0; i < unreplied_people.length; i++) {
if ((unreplied_people[i].name.toLowerCase() == ctx.state.command.person.toLowerCase()) || (unreplied_people[i].username.toLowerCase() == ctx.state.command.person.toLowerCase())) {
unreplied_people.splice(i, 1);
if (unreplied_people.length) {
var replyMessage = "List of unreplied individuals:\n";
unreplied_people.map((unreplied_people, index) => {
unreplied_people.username ?
replyMessage = replyMessage + (index + 1 + ". " + unreplied_people.name + " - @" + unreplied_people.username + "\n") :
replyMessage = replyMessage + (index + 1 + ". " + unreplied_people.name + "\n")
});
ctx.reply(replyMessage);
}
else {
ctx.reply("There is no unreplied message");
}
break;
}
}
await sync_unreplied_people_db(unreplied_people);
} else {
ctx.reply("Unknown command. please follow this format /close username or /close name");
}
} catch (err) {
var errorMessage = `
*_WARNING_*\n
```
`${err}`
```
`;
telegram.sendMessage(process.env.OUTREACH_GROUP_ID, errorMessage).then().catch(err => { console.log(err) });
}
});
bot.command('fullhistory', async (ctx) => {
try {
if (ctx.update.message.chat.id == process.env.OUTREACH_GROUP_ID) {
ctx.reply("Full history: " + JSON.stringify(unreplied_people, null, 1));
}
} catch (err) {
var errorMessage = `
*_WARNING_*\n
```
`${err}`
```
`;
telegram.sendMessage(process.env.OUTREACH_GROUP_ID, errorMessage).then().catch(err => { console.log(err) });
}
});
bot.command('clearhistory', async (ctx) => {
try {
unreplied_people = [];
sync_unreplied_people_db(unreplied_people).then(() => {
ctx.reply("The history is cleared")
});
} catch (err) {
var errorMessage = `
*_WARNING_*\n
```
`${err}`
```
`;
telegram.sendMessage(process.env.OUTREACH_GROUP_ID, errorMessage).then().catch(err => { console.log(err) });
}
});
bot.hears((ctx) => { return true }, async (ctx) => {
// ctx.reply("I have forwarded your message to the ambassadors.");
// console.log(util.inspect(ctx, false, null, true));
try {
if (ctx.update.message.from.id != process.env.OUTREACH_BOT_ID && ctx.update.message.chat.id != process.env.OUTREACH_GROUP_ID) {
let new_query = { "name": ctx.update.message.from.first_name, "username": ctx.update.message.from.username, "chatId": ctx.update.message.from.id, "message": [] };
let new_income_message = new_query;
let new_message = "";
if (ctx.update) {
if (ctx.update.message) {
if (ctx.update.message.text) {
new_income_message.message.push(ctx.update.message.text);
new_message = ctx.update.message.text;
}
}
}
let hasFound = false;
for (var i = 0; i < unreplied_people.length; i++) {
if (unreplied_people[i].chatId === ctx.update.message.from.id) {
unreplied_people[i].message.push(new_message);
hasFound = true;
break;
}
}
if (!hasFound) {
unreplied_people.push(new_income_message);
}
telegram.forwardMessage(process.env.OUTREACH_GROUP_ID, ctx.update.message.from.id, ctx.update.message.message_id)
.then()
.catch(err => { console.log(err) });
await sync_unreplied_people_db(unreplied_people);
}
} catch (err) {
var errorMessage = `
*_WARNING_*\n
```
`${err}`
```
`;
telegram.sendMessage(process.env.OUTREACH_GROUP_ID, errorMessage).then().catch(err => { console.log(err) });
}
});
bot.launch();
console.log("The bot has started");