This repository has been archived by the owner on Feb 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbot.js
293 lines (280 loc) · 14.1 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
require('dotenv').config()
const config = require('./config')
// Bot
const { Telegraf } = require('telegraf')
const bot = new Telegraf(process.env.BOT_TOKEN, { telegram: { webhookReply: false }})
const log = console.log;
// Custom helpers
const { getTranslations, getBooks, getChapters, getVerses, getTranslationsDefault } = require('./helpers/data-retrieve')
const { getBookDetails } = require('./helpers/ref-validator') // Not a perfect validator. But it do the job in good way.
const { writeIfNotExist, getDefault, updateDefault } = require('./helpers/firebase')
// ^^^^
// A simple small collection of functions which
//can be used to work with firebase.
// Basic commands and actions
bot.start((ctx) => {
ctx.reply(config.bot.start.msg, config.bot.start.opts)
writeIfNotExist('users', ctx.chat.id, { uid: ctx.chat.id, username: ctx.chat.username || "blob", def: 'akjv' })
})
bot.action('home', (ctx) => ctx.editMessageText(config.bot.start.msg, config.bot.start.opts))
bot.help((ctx) => ctx.reply(config.bot.help.msg, config.bot.help.opts))
bot.action([/help_(.+)/, 'help'], (ctx) => {
if(ctx.match[1] !== undefined){
ctx.editMessageText(config.bot.help.msg,
{ parse_mode: 'Markdown', disable_web_page_preview: true, reply_markup: { inline_keyboard: [[ { text: '📖 Start Reading →', callback_data: 'read_1' }, { text: '💬 About', callback_data: 'about' }], [{ text: '← Go back', callback_data: `${ctx.match[1]}` }]] }}
)
} else {
ctx.editMessageText(config.bot.help.msg, config.bot.help.opts)
}
})
bot.command('about', (ctx) => ctx.reply(config.bot.about.msg, config.bot.about.opts))
bot.action('about', (ctx) => ctx.editMessageText(config.bot.about.msg, config.bot.about.opts))
bot.command('read', async (ctx) => {
getTranslations(1)
.then((keyboard) => {
keyboard.push([{ text: '← Previous', callback_data: 'read_0' }, { text: 'Next →', callback_data: 'read_2' }], [{ text: '🏡 Home', callback_data: 'home' }])
ctx.reply('*Choose a version from below*. Browse all other versions using the next and previous buttons.',
{ parse_mode: 'Markdown', reply_markup: { inline_keyboard: keyboard } }
)
})
.catch((err) => {
ctx.reply(config.bot.error.translation,
{ reply_markup: { inline_keyboard: [[{ text: '🏡 Home', callback_data: 'home' }, { text: '↻ Try Again', callback_data: read_1 }]] } }
)
log(err)
})
})
bot.action(/read_(.+)/, async (ctx) => {
await ctx.editMessageText('Updating Translation List...')
const page = parseInt(ctx.match[1])
getTranslations(page)
.then((keyboard) => {
if(page == 3){
keyboard.push([{ text: '← Previous', callback_data: `read_2` }])
} else if(page == 0) {
keyboard.push([{ text: 'Next →', callback_data: `read_1` }])
} else {
keyboard.push([{ text: '← Previous', callback_data: `read_${page-1}` }, { text: 'Next →', callback_data: `read_${page+1}` }])
}
keyboard.push([{ text: '🏡 Home', callback_data: 'home' }])
ctx.editMessageText('*Choose a version from below*. Browse all other versions using the next and previous buttons.',
{ parse_mode: 'Markdown', reply_markup: { inline_keyboard: keyboard } }
)
})
.catch((err) => {
ctx.editMessageText(config.bot.errors.translation,
{ reply_markup: { inline_keyboard: [[{ text: '🏡 Home', callback_data: 'home' }, { text: '↻ Try Again', callback_data: ctx.match[0] }]] } }
)
log(err)
})
})
bot.action(/lang_(.+)/, async (ctx) => {
await ctx.editMessageText("Getting book list...")
const version = ctx.match[1].split('_')[0]
const testament = parseInt(ctx.match[1].split('_')[1])
const page = parseInt(ctx.match[1].split('_')[2])
getBooks(version, testament, page)
.then((result) => {
const keyboard = result.keyboard;
if(page == 0){
keyboard.push([{ text: 'Next →', callback_data: `lang_${version}_${testament}_${page + 1}` }])
} else if(page == 2){
keyboard.push([{ text: '← Previous', callback_data: `lang_${version}_${testament}_${page - 1}` }])
} else if(page == 1){
if(testament == 0){
keyboard.push([{ text: '← Previous', callback_data: `lang_${version}_${testament}_${page - 1}` }, { text: 'Next →', callback_data: `lang_${version}_${testament}_${page + 1}` }])
} else {
keyboard.push([{ text: '← Previous', callback_data: `lang_${version}_${testament}_${page - 1}` }])
}
}
var testamentName = 'New Testament'
switch(testament){
case 0:
keyboard.push([{ text: 'New Testament', callback_data: `lang_${version}_1_0` }])
break;
case 1:
testamentName = 'Old Testament'
keyboard.push([{ text: 'Old Testament', callback_data: `lang_${version}_0_0` }])
break;
default:
keyboard.push([{ text: 'New Testament', callback_data: `lang_${version}_1_0` }])
}
keyboard.push([{ text: '←', callback_data: `read_1` }, { text: '🏡 Home', callback_data: 'home' }, { text: '📚 Help', callback_data: `help_${ctx.match[0]}` }])
ctx.editMessageText(`📚 Here is the Book list of ${result.details.language} *${result.details.translation}*(${result.details.abbreviation.toUpperCase()})\n🗨️ Choose one from below or browse all others using next and previous buttons. Or, click on *${testamentName}* button to switch to ${testamentName}. '←' to go back.`,
{ parse_mode: 'Markdown', reply_markup: { inline_keyboard: keyboard } }
)
}).catch((err) => {
ctx.editMessageText(config.bot.error.books,
{ reply_markup: { inline_keyboard: [[{ text: '🏡 Home', callback_data: 'home' }, { text: '↻ Try Again', callback_data: ctx.match[0] }]] } }
)
log(err);
});
})
bot.action(/book_(.+)/, async (ctx) => {
const version = ctx.match[1].split('_')[0]
const bookno = parseInt(ctx.match[1].split('_')[1])
const page = parseInt(ctx.match[1].split('_')[2])
await ctx.editMessageText('Getting chapters...')
getChapters(version, bookno, page)
.then((result) => {
const keyboard = result.keyboard;
if(result.details.isPsalms){
if(page == 0){
keyboard.push([{ text: 'Next →', callback_data: `book_${version}_${bookno}_1` }])
} else if(page == 2){
keyboard.push([{ text: '← Previous', callback_data: `book_${version}_${bookno}_1` }])
} else {
keyboard.push([{ text: '← Previous', callback_data: `book_${version}_${bookno}_0` }, { text: 'Next →', callback_data: `book_${version}_${bookno}_2` }])
}
}
keyboard.push([{ text: '← Books', callback_data: `lang_${version}_0_0` }, { text: '🏡 Home', callback_data: 'home' }])
ctx.editMessageText(`*Chapters*: ${result.details.book_name}\nChoose a chapter to start reading.\nVersion: ${result.details.translation}\nTotal chapters: ${result.details.chapters}`,
{ parse_mode: 'Markdown', reply_markup: { inline_keyboard: keyboard } }
)
})
.catch((error) => {
ctx.editMessageText(config.bot.error.chapters,
{ reply_markup: { inline_keyboard: [[{ text: '🏡 Home', callback_data: 'home' }, { text: '↻ Try Again', callback_data: ctx.match[0] }]] } }
)
log(error)
})
})
bot.action(/chapter_(.+)/, (ctx) => {
const version = ctx.match[1].split('_')[0]
const bookno = parseInt(ctx.match[1].split('_')[1])
const chapter = parseInt(ctx.match[1].split('_')[2])
const page = parseInt(ctx.match[1].split('_')[3])
getVerses(version, bookno, chapter, page)
.then((res) => {
const keyboard = res.keyboard;
keyboard.push([{ text: '#', callback_data: `book_${version}_${bookno}_0` }, { text: '📚', callback_data: `lang_${version}_0_0` }, { text: '🏡 Home', callback_data: 'home' }])
ctx.editMessageText(res.message,
{ parse_mode: 'Markdown', reply_markup: { inline_keyboard: keyboard } }
)
})
.catch((err) => {
ctx.editMessageText(config.bot.error.verses,
{ reply_markup: { inline_keyboard: [[{ text: '🏡 Home', callback_data: 'home' }, { text: '↻ Try Again', callback_data: ctx.match[0] }]] } }
)
log(err)
})
})
bot.on('text', (ctx) => {
getDefault(ctx.chat.id)
.then((def) => {
const bookdetail = getBookDetails(ctx.message.text)
if(bookdetail.status){
var keyboard = []
if(bookdetail.verses == 'ALL'){
if(def == 'akjv'){
keyboard = [[{ text: 'AKJV', callback_data: `chapter_akjv_${bookdetail.nr}_${bookdetail.chapter}_0` }]]
} else {
keyboard = [[{ text: def.toUpperCase(), callback_data: `chapter_${def}_${bookdetail.nr}_${bookdetail.chapter}_0` }, { text: 'AKJV', callback_data: `chapter_akjv_${bookdetail.nr}_${bookdetail.chapter}_0` }]]
}
ctx.reply(`Choose a version to get the requested passage: *${ctx.message.text}*`,
{ parse_mode: 'Markdown', reply_markup: { inline_keyboard: keyboard } }
)
} else {
var part;
if((bookdetail.verses[0] % 10) == 0){
part = Math.floor( (parseInt(bookdetail.verses[0]) / 10) - 1)
} else {
part = Math.floor(parseInt(bookdetail.verses[0])/10)
}
if(def == 'akjv'){
keyboard = [[{ text: 'AKJV', callback_data: `chapter_akjv_${bookdetail.nr}_${bookdetail.chapter}_${part}` }]]
} else {
keyboard = [[{ text: def.toUpperCase(), callback_data: `chapter_${def}_${bookdetail.nr}_${bookdetail.chapter}_${part}` }, { text: 'AKJV', callback_data: `chapter_akjv_${bookdetail.nr}_${bookdetail.chapter}_${part}` }]]
}
ctx.reply(`Choose a version to get the requested passage: *${ctx.message.text}*`,
{ parse_mode: 'Markdown', reply_markup: { inline_keyboard: keyboard } }
)
}
} else {
ctx.reply('🤕 Sorry. The reference you entered is not valid as per our system. Please click the button below and find what are the valid book names.',
{ reply_markup: { inline_keyboard: [[{ text: 'Valid References', callback_data: 'valid_0' }]] } }
)
}
})
.catch((err) => {
ctx.reply(config.bot.errors.text)
console.log(err)
})
})
bot.action(/valid_(.+)/, (ctx) => {
if(ctx.match[1] == 0){
ctx.editMessageText('Only *King James Version(KJV)* book names and book names are valid. *These methods are only valid.*\n• Book name \n• Book name <Chapter>\n• Book name <Chapter>:<Verse>\n\n*The available abbreviations and book names given below.* It will be case insensitive, which means, you can enter it as UPPERCASE, lowercase, or like Camel Case.\n' + config.bot.valid.bookarray[0],
{ parse_mode: 'Markdown', reply_markup: { inline_keyboard: [[{ text: '🏡 Home', callback_data: 'home' }, { text: 'Next →', callback_data: 'valid_1' }]] } }
)
} else {
ctx.editMessageText(config.bot.valid.bookarray[1],
{ parse_mode: 'Markdown', reply_markup: { inline_keyboard: [[{ text: '← Previous', callback_data: 'valid_0' }, { text: '🏡 Home', callback_data: 'home' }]] } }
)
}
})
bot.action('default', async (ctx) => {
await getDefault(ctx.chat.id)
.then((def) => {
ctx.editMessageText(`*Setting a default version*\nThis bot have a default version setting feature, which can be used to set your own default version. This will be used to give you the passage when you request reference as a message. Send a reference to see the usage.\n\nCurrently your default version to *${def.toUpperCase()}*`,
{ parse_mode: 'Markdown', reply_markup: { inline_keyboard: [[{ text: 'Set default', callback_data: 'setd-list_1' }, { text: '🏡 Home', callback_data: 'home' }]] } }
)
})
.catch((err) => {
ctx.editMessageText(`*Setting a default version*\nThis bot have a default version setting feature, which can be used to set your own default version. This will be used to give you the passage when you request reference as a message. Send a reference to see the usage.`,
{ parse_mode: 'Markdown', reply_markup: { inline_keyboard: [[{ text: 'Set default', callback_data: 'setd-list_1' }, { text: '🏡 Home', callback_data: 'home' }]] } }
)
})
})
bot.action(/setd-list_(.+)/, (ctx) => {
const page = parseInt(ctx.match[1])
getTranslationsDefault(page)
.then((keyboard) => {
if(page == 3){
keyboard.push([{ text: '← Previous', callback_data: `setd-list_2` }])
} else if(page == 0) {
keyboard.push([{ text: 'Next →', callback_data: `setd-list_1` }])
} else {
keyboard.push([{ text: '← Previous', callback_data: `setd-list_${page-1}` }, { text: 'Next →', callback_data: `setd-list_${page+1}` }])
}
keyboard.push([{ text: '🏡 Home', callback_data: 'home' }])
ctx.editMessageText( 'Choose a version from below *to set as your default from-message version*. This default will be used to get the version buttons while you are requesting a reference by messaging.',
{ parse_mode: 'Markdown', reply_markup: { inline_keyboard: keyboard } }
)
})
.catch((err) => {
ctx.editMessageText(config.bot.errors.translation,
{ reply_markup: { inline_keyboard: [[{ text: '🏡 Home', callback_data: 'home' }, { text: '↻ Try Again', callback_data: ctx.match[0] }]] } }
)
log(err)
})
})
bot.action(/setd_(.+)/, async (ctx) => {
const version = ctx.match[1]
await ctx.editMessageText('Trying to update default version...')
await updateDefault(ctx.chat.id, version)
await getDefault(ctx.chat.id)
.then((def) => {
if(def == version){
ctx.editMessageText(`*Your default version has been updated to ${def.toUpperCase()}!*`,
{ parse_mode: 'Markdown', reply_markup: { inline_keyboard: [[{ text: '← Go back', callback_data: 'default' }, { text: '🏡 Home', callback_data: 'home' }]] } }
)
} else {
ctx.editMessageText('Sorry. There was an error while updating your default version. Please try again or come back later!',
{ reply_markup: { inline_keyboard: [[{ text: '🏡 Home', callback_data: 'home' }, { text: '↻ Try Again', callback_data: ctx.match[0] }]] } }
)
}
})
.catch((err) => {
ctx.editMessageText('Error while updating or getting the new default versions.',
{ reply_markup: { inline_keyboard: [[{ text: '🏡 Home', callback_data: 'home' }, { text: '↻ Try Again', callback_data: ctx.match[0] }]] }}
)
log(err)
})
})
log('No syntax errors. Running...')
bot.launch({
webhook: {
domain: process.env.BOT_DOMAIN,
port: process.env.PORT
}
})