-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
168 lines (134 loc) · 4.9 KB
/
bot.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
const Telegraf = require('telegraf')
const TelegrafI18n = require('telegraf-i18n')
const path = require('path')
const mongo = require('mongodb').MongoClient
const data = require('./data')
const makeMessage = require('./makeMessage')
const bot = new Telegraf(data.token)
const i18n = new TelegrafI18n({
defaultLanguage: 'en',
allowMissing: false,
directory: path.resolve(__dirname, 'locales')
})
bot.use(Telegraf.session())
bot.use(i18n.middleware())
mongo.connect(data.mongoLink, {useNewUrlParser: true}, (err, client) => {
if (err) {
sendError(err)
}
db = client.db('lifedaysbot')
bot.startPolling()
})
bot.start(({ replyWithHTML, message, from, i18n }) => {
replyWithHTML(
i18n.t('start'),
{ reply_markup: { keyboard: [[i18n.t('buttons.stat'), i18n.t('buttons.source')]], resize_keyboard: true } }
)
updateUser(from, true)
})
bot.hears(/^[0-9]{2}\.[0-9]{2}\.[0-9]{4}$/, async ({ replyWithHTML, message, from, i18n }) => {
const text = await makeMessage.mess(replyWithHTML, message, i18n)
if (!text) return false
replyWithHTML(
text,
{
reply_markup: {
inline_keyboard: [[{text: '⤴️ Share', switch_inline_query: message.text}]]
},
disable_web_page_preview: true
}
)
updateStat('date')
updateUser(from, true)
})
// console.log(!'2'.match(/^[0-9]{2?}\.[0-9]{2}\.[0-9]{4}$/))
bot.on('inline_query', async ({ answerInlineQuery, update, i18n }) => {
const query = update.inline_query.query
const count = (await db.collection('statistic').find({genAct: 'date'}).toArray())[0].count + 1
if (!query.trim().match(/^[0-9]{2}\.[0-9]{2}\.[0-9]{4}$/)) {
return false
}
makeMessage.query(answerInlineQuery, update, i18n, count)
updateStat('date')
})
bot.hears(TelegrafI18n.match('buttons.stat'), async ({ replyWithHTML, replyWithChatAction, i18n }) => {
replyWithChatAction('typing')
const allUsers = (await db.collection('allUsers').find({}).toArray()).length
const activeUsers = (await db.collection('allUsers').find({status: 'active'}).toArray()).length
const blockedUsers = (await db.collection('allUsers').find({status: 'blocked'}).toArray()).length
const actions = await db.collection('statistic').find({genAct: 'date'}).toArray()
replyWithHTML(
i18n.t('statistic', {
allUsers: allUsers, activeUsers: activeUsers, blockedUsers: blockedUsers,
activeUsersPercent: Math.round((activeUsers / allUsers) * 100),
blockedUsersPercent: Math.round((blockedUsers / allUsers) * 100),
actions: actions[0].count
}),
{ reply_markup: { keyboard: [[i18n.t('buttons.stat'), i18n.t('buttons.source')]], resize_keyboard: true } }
)
})
bot.hears(TelegrafI18n.match('buttons.source'), ({ replyWithHTML, from, i18n }) => {
replyWithHTML(
i18n.t('source'),
{ reply_markup: { inline_keyboard: [[{text: '🔗 GitHub', url: 'https://github.com/Khuzha/lifedaysbot'}]] } }
)
updateUser(from, true)
})
bot.command('users', async (ctx) => {
let allUsers = await db.collection('allUsers').find({}).toArray()
let activeUsers = 0
let blockedUsers = 0
for (let key of allUsers) {
await bot.telegram.sendChatAction(key.userId, 'typing')
.then((res) => {
activeUsers++
})
.catch((err) => {
blockedUsers++
updateUser(key.userId, false)
})
}
ctx.reply(
`⭕️ Total users: ${allUsers.length} ` +
`\n✅ Active users: ${activeUsers} - ${Math.round((activeUsers / allUsers.length) * 100)}%` +
`\n❌ Blocked users: ${blockedUsers} - ${Math.round((blockedUsers / allUsers.length) * 100)}%`
)
})
bot.on('message', ({ replyWithHTML, from, i18n }) => {
replyWithHTML(
i18n.t('mistake'),
{ reply_markup: { keyboard: [[i18n.t('buttons.stat'), i18n.t('buttons.source')]], resize_keyboard: true } }
)
updateUser(from, true)
})
updateUser = (from, active) => {
typeof(from) == 'object' ? from = from.id : false
let jetzt = active ? 'active' : 'blocked'
db.collection('allUsers').updateOne({userId: from}, {$set: {status: jetzt}}, {upsert: true, new: true})
}
updateStat = (action) => {
if (action == 'button') {
return db.collection('statistic').updateOne({genAct: action}, {$inc: {count: 1}}, {new: true, upsert: true})
}
db.collection('statistic').updateOne({action: action}, {$inc: {[makeDate()]: 1}}, {new: true, upsert: true})
db.collection('statistic').updateOne({genAct: action}, {$inc: {count: 1}}, {new: true, upsert: true})
}
makeDate = () => {
const today = new Date()
const yyyy = today.getFullYear()
let mm = today.getMonth() + 1
let dd = today.getDate()
dd < 10 ? dd = '0' + dd : false
mm < 10 ? mm = '0' + mm : false
return `${mm}/${dd}/${yyyy}`
}
sendError = (err, ctx) => {
if (!ctx) {
return bot.telegram.sendMessage(data.dev, err)
}
bot.telegram.sendMessage(
data.dev,
`Error: \nUser: [${ctx.from.first_name}](tg://user?id=${ctx.from.id}) \nError's text: ${err}`
)
}
bot.catch((err) => console.log(err))