-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
72 lines (50 loc) · 1.77 KB
/
app.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
require('dotenv').config()
const { Telegraf } = require('telegraf')
const config = require('./app/config/database')
config();
const express = require('express')
const app = express();
//public folder
app.use(express.static('public'))
const botController = require('./app/controller/BotControllers')
const aplikasiController = require('./app/controller/AplikasiControllers')
const URL = process.env.BASE_URL_WEBHOOK || '';
const BOT_TOKEN = process.env.BOT_TOKEN || ''
const AUTHOR = process.env.AUTHOR
const PORT = process.env.PORT || 2002;
const bot = new Telegraf(BOT_TOKEN)
bot.telegram.setWebhook(`${URL}/bot${BOT_TOKEN}`)
app.use(bot.webhookCallback(`/bot${BOT_TOKEN}`));
bot.use((ctx, next) => {
console.log("[MESSAGE]", ctx.message);
if (ctx.chat.id > 0) return next()
return ctx.telegram
.getChatAdministrators(ctx.chat.id)
.then(function (data) {
if (!data || !data.length) return
ctx.chat._admins = data
ctx.from.isAdmin = data.some((adm) => adm.user.id === ctx.from.id)
console.log("Author OR Admin : ",ctx.message.from.id === AUTHOR || ctx.from.isAdmin);
})
.catch(console.log)
.then((_) => next(ctx))
})
bot.use(botController.main(bot))
bot.use(aplikasiController.main(bot))
//logging jika bot error akan mengirim ke author
bot.catch((err, ctx) => {
console.log(err);
ctx.telegram.sendMessage(AUTHOR, `[ ERROR ] Ooops, encountered an error for ${ctx.updateType} :` + err)
})
//konfigurasi untuk menjalankn bot
bot.launch().then(() => {
console.log("[BOT] Bot has been started");
});
process.once('SIGINT', () => bot.stop('SIGINT'))
process.once('SIGTERM', () => bot.stop('SIGTERM'))
app.get('/', (req, res) => {
res.send('[BOT] Bot running...\n');
});
app.listen(PORT, () => {
console.log(`[SERVER] Server running on port ${PORT}`);
});