-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
117 lines (99 loc) · 2.93 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
process.env['NTBA_FIX_319'] = 1
require('dotenv-extended').load({
encoding: 'utf8',
silent: true,
path: '.env',
defaults: '.env.defaults',
schema: '.env.schema',
errorOnMissing: false,
errorOnExtra: false,
includeProcessEnv: false,
assignToProcessEnv: true,
overrideProcessEnv: false,
})
const createError = require('http-errors')
const express = require('express')
const cookieParser = require('cookie-parser')
const logger = require('morgan')
const TelegramBot = require('node-telegram-bot-api')
const axios = require('axios')
const indexRouter = require('./routes/index')
const app = express()
app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use('/', indexRouter)
// catch 404 and forward to error handler
app.use((req, res, next) => {
next(createError(404))
})
// error handler
app.use((err, req, res) => {
// set locals, only providing error in development
res.locals.message = err.message
res.locals.error = req.app.get('env') === 'development' ? err : {}
// send the error page
res.status(err.status || 500)
res.send('error')
})
// Bot usage
const token = process.env.BOTTOKEN
const bot = new TelegramBot(token, { polling: true })
axios.defaults.baseURL = 'http://localhost:3000'
let tmpMenu = []
bot.onText(/\/help/, (msg) => {
const chatId = msg.chat.id
const syntax = [
'/myorder',
'/menu',
'/checkPayment',
]
bot.sendMessage(chatId, `Use this for order: ${syntax[0]} , this one for list menu ${syntax[1]} , and this one for check your payment status ${syntax[2]}`)
})
bot.onText(/\/menu/, async(msg) => {
const menus = () => {
return axios.get('/menu').then((item) => {
let data = []
item.data.map(item =>{
data.push(`${item.name} = Rp.${item.price}`)
})
return data
})
}
const result = await menus()
const response = {
reply_to_message_id: msg.message_id,
reply_markup: JSON.stringify({
keyboard: [ result ],
}),
}
bot.sendMessage(msg.chat.id, 'What do you want to order?', response)
bot.onText(/(.+)/, (msg, match) => {
const resp = match[0]
result.find(item => {
if (item === resp) {
tmpMenu.push(resp)
bot.sendMessage(msg.chat.id, `Noted ${msg.chat.first_name}: ${resp}`)
}
})
if (tmpMenu.length > 3) {
bot.sendMessage(msg.chat.id, `${msg.chat.first_name}, type /done if you don't want to order anymore!`)
}
})
// console.log(ifOrderExist())
return response.reply_markup.keyboard = []
})
bot.onText(/\/myorder/, (msg) => {
if (tmpMenu.length != 0){
const text = `
Here's Your order for today:
${tmpMenu.map(item => {
return `${item} `
})}`
bot.sendMessage(msg.chat.id, text.replace(/(^,)|(,$)/g, ''), tmpMenu)
} else {
bot.sendMessage(msg.chat.id, `${msg.chat.first_name}, you still not ordering yet. Type /menu to start order and /done to finish.`)
}
})
module.exports = app