-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
73 lines (59 loc) · 2.02 KB
/
server.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
require('dotenv').config()
const url = `https://${process.env.HEROKU_APP_NAME}.herokuapp.com:443`
const token = process.env.TOKEN
const TelegramBot = require('node-telegram-bot-api')
const scryfall = require('./scryfall.js')
const scraper = require('./scraper.js')
const utils = require('./utils.js')
// const options = {
// webHook: {
// port: process.env.PORT
// }
// }
// const bot = new TelegramBot(token, options)
// bot.setWebHook(`${url}/bot${token}`)
const bot = new TelegramBot(token, { polling: true } )
let sendMessage = (chatId) => {
return (message) => {
bot.sendMessage(chatId, message.message, message.options)
}
}
let sendErrorMessage = (chatId) =>
// (err) => bot.sendMessage(chatId, `Ocorreu um erro! Tente novamente mais tarde.`)
(err) => bot.sendMessage(chatId, err.message)
bot.on('message', (msg) => {
console.log(`User ${msg.chat.first_name} searched for: ${msg.text}.`)
scryfall.findCards(msg.text)
.then(utils.getResponseMessage)
.then(sendMessage(msg.chat.id))
.catch(sendErrorMessage(msg.chat.id))
})
bot.on("inline_query", (msg) => {
scryfall.findCards(msg.query)
.then(utils.getInlineOptions)
.then((options) => bot.answerInlineQuery(msg.id, options))
});
bot.on('chosen_inline_result', (chosen_result) => {
console.log(chosen_result)
})
bot.on('callback_query', function onCallbackQuery(choice){
const chosen_card = choice.data // returns which button the user clicked
console.log(choice);
if(choice.inline_message_id) {
scraper.findAndFetchPrice(chosen_card)
.then(utils.craftMessageFromCards)
.then((message) => {
bot.editMessageText(
message.message,
{ inline_message_id: choice.inline_message_id, parse_mode: 'Markdown' }
)
})
.catch(sendErrorMessage(choice.inline_message_id))
} else {
const chatId = choice.message.chat.id
scraper.findAndFetchPrice(chosen_card)
.then(utils.craftMessageFromCards)
.then(sendMessage(chatId))
.catch(sendErrorMessage(chatId))
}
})