-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
134 lines (116 loc) · 3.52 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
process.env['NTBA_FIX_319'] = 1;
require('dotenv').config()
const TelegramBot = require('node-telegram-bot-api')
const got = require('got');
const _ = require('lodash')
const telegramToken = process.env.TELEGRAMAPITOKEN
const catToken = process.env.CATAPITOKEN
const baseUrl = 'https://api.thecatapi.com/v1'
const bot = new TelegramBot(telegramToken, {polling: true})
const inline_keyboard_categories = []
const inline_keyboard_breeds = []
const inline_keyboard = [ [
{
text: 'Random cat',
callback_data: 'random'
},
{
text: 'List of categories',
callback_data: 'categories'
},
{
text: 'List of breeds',
callback_data: 'breeds'
}
]]
function callACat(url, person, chatId, searchParams) {
got(url, {baseUrl, headers: {'x-api-key': catToken}, json: true, query: searchParams} ).then(res => {
if (res.body.length > 0 && res.body[0].url){
const imgUrl = res.body[0].url
bot.sendMessage(chatId, `${person} here is your cat ${imgUrl}`)
} else {
bot.sendMessage(chatId, `Sorry ${person} something went wrong. No cat for you =(`)
}
}).catch(e => {
console.error(`[*] Error Caught: ${e}`)
})
}
async function getCategories() {
resetKeyboard(inline_keyboard_categories)
try {
const response = await got('/categories', {baseUrl, json: true})
fillKeyboard(response.body, inline_keyboard_categories)
} catch (e) {
console.error(`[*] Error Caught: ${e}`)
}
}
async function getBreeds() {
resetKeyboard(inline_keyboard_breeds)
try {
const response = await got('/breeds', {baseUrl, json: true})
fillKeyboard(response.body, inline_keyboard_breeds)
} catch (e) {
console.error(`[*] Error Caught: ${e}`)
}
}
function resetKeyboard(keyboard) {
keyboard.length = 0
keyboard.push([])
}
function fillKeyboard(data, keyboard) {
let counter = 0
for (const [index, entry] of data.entries()) {
if(index%3 !== 0 || index === 0) {
keyboard[counter].push({text: entry.name, callback_data: entry.id.toString()})
} else {
keyboard.push([])
counter++
keyboard[counter].push({text: entry.name, callback_data: entry.id.toString()})
}
}
}
bot.on('callback_query', query => {
const {message: {chat}, from} = query
switch (query.data) {
case 'breeds': getBreeds().then(() => {
bot.sendMessage(chat.id, 'Available breeds', {
reply_markup: {
inline_keyboard: inline_keyboard_breeds
}
})
}); break
case 'categories': getCategories().then(() => {
bot.sendMessage(chat.id, 'Available categories', {
reply_markup: {
inline_keyboard: inline_keyboard_categories
}
})
}); break
case 'random': callACat('/images/search', from.first_name, chat.id); break
}
const category = _.flattenDeep(inline_keyboard_categories).find(elem => elem.callback_data === query.data)
if (category) {
const searchParams = new URLSearchParams([['category_ids', category.callback_data]])
callACat('/images/search', from.first_name, chat.id, searchParams)
}
const breed = _.flattenDeep(inline_keyboard_breeds).find(elem => elem.callback_data === query.data)
if (breed) {
const searchParams = new URLSearchParams([['breed_id', breed.callback_data]])
callACat('/images/search', from.first_name, chat.id, searchParams)
}
bot.answerCallbackQuery(query.id)
})
bot.onText(/^\/cat$/, (msg) => {
const chatId = msg.chat.id;
const person = msg.from.first_name
callACat('/images/search', person, chatId)
})
bot.onText(/\/help/, (msg) => {
const chatId = msg.chat.id;
const person = msg.from.first_name
bot.sendMessage(chatId, `Hey ${person} pick one option:`, {
reply_markup: {
inline_keyboard
}
})
})