-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
125 lines (103 loc) · 3.08 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
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
const Telegraf = require('telegraf')
const TelegrafInlineMenu = require('telegraf-inline-menu')
const db = require('./db')
const htmlFetcher = require('./htmlFetcher')
const {logOutMsg, logMsg} = require('./helpers')
const bot = new Telegraf(process.env.TELEGRAM_BOT_TOKEN)
// -------MIDDLEWARES----------
//logging
bot.use(async (ctx, next) => {
const start = new Date()
await next()
const ms = new Date() - start
console.log('%sms', ms) //response time
})
// -------------MENU-------------
const menu = new TelegrafInlineMenu(ctx => `Hey ${ctx.from.first_name}!`)
menu.setCommand('start')
// LIST
menu.simpleButton('List Checkers', 'listCheckers', {
doFunc: ctx => {
var checkers = db.getCheckers(ctx.from.id)
if(checkers.length < 1) {
ctx.answerCbQuery("No checkers found.")
return;
}
var reply = "*Checkers:* \n"
checkers.forEach( (checker, idx) => {
reply+= `${idx+1}. ${checker.name}\n`
})
ctx.replyWithMarkdown(reply)
return
}
})
// CREATE
menu.question('Create a new Checker', 'createCheckerMenu', {
questionText: "Enter HTML element to check: \n" + "Format: \n" + "name~~url~~selector",
setFunc: (ctx, ans) => {
var checkerElements = ans.split("~~")
var newChecker= {
name: checkerElements[0],
url: checkerElements[1],
selector: checkerElements[2]
}
if(!newChecker) ctx.answerCbQuery("Invalid Format")
db.addChecker(ctx, newChecker)
return
}
})
// REMOVE
menu.question('Remove Checker', 'notsohell', {
questionText: "Name/Index of Checker to remove",
setFunc: (ctx, key) => {
db.removeChecker(ctx, key)
return
}
})
// CHECK (MENU)
menu.simpleButton('Check!', 'checkForUpdates', {
doFunc: async ctx => {
console.log("checking .. ")
reply = await check(ctx)
if(!reply) return
ctx.reply("checking for updates... ")
ctx.replyWithMarkdown(reply)
return
}
})
bot.use(menu.init({
backButtonText: 'Back…',
mainMenuButtonText: 'Back to main menu…'
}))
//BOT COMMANDS
bot.start((ctx) => ctx.reply('Welcome!'))
bot.help((ctx) => ctx.reply('Send me a sticker'))
bot.on('sticker', (ctx) => ctx.reply('👍'))
bot.hears('hi', (ctx) => ctx.reply('Hey there'))
bot.command('/check', async (ctx) => {
logMsg(ctx)
reply = await check(ctx)
if(!reply) return
ctx.replyWithMarkdown(reply)
logOutMsg(ctx, reply)
})
async function check(ctx) {
var checkers = await db.getCheckers(ctx.from.id)
if(checkers.length <1) {
ctx.reply("No Checkers Found")
return
}
var promiseArray = []
checkers.forEach(checker => {
var fetcher = new htmlFetcher(checker.selector, checker.url)
promiseArray.push(fetcher.fetchData())
})
const data = await Promise.all(promiseArray)
var reply = "*Updates:* \n"
data.forEach((el, idx) => {
reply += `${idx+1}. *${checkers[idx].name}:* ${el}\n`
})
return reply;
}
// Launch Bot
bot.launch()