-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (61 loc) · 2.48 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
const TelegramBot = require('node-telegram-bot-api');
const express = require('express');
const cors = require('cors');
const token = "6806838194:AAHMauZsPj2FUM2gU13StdmKDtevWBEkM4o";
const webAppUrl = "https://hackthesky.ru/";
const bot = new TelegramBot(token, {polling: true});
const app = express();
app.use(express.json());
app.use(cors());
bot.on('message', async (msg) => {
const chatId = msg.chat.id;
const text = msg.text;
if(text === '/start') {
await bot.sendMessage(chatId, 'Ниже появится кнопка, заполни форму', {
reply_markup: {
keyboard: [
[{text: 'Заполнить форму', web_app: {url: webAppUrl + 'form'}}]
]
}
})
await bot.sendMessage(chatId, 'Заходи в наш интернет магазин по кнопке ниже', {
reply_markup: {
inline_keyboard: [
[{text: 'Сделать заказ', web_app: {url: webAppUrl}}]
]
}
})
}
if (msg && msg.web_app_data && msg.web_app_data.data) {
try {
const data = JSON.parse(msg && msg.web_app_data && msg.web_app_data.data)
console.log(data)
await bot.sendMessage(chatId, 'Спасибо за обратную связь!')
await bot.sendMessage(chatId, 'Ваша страна: ' + (data && data.country));
await bot.sendMessage(chatId, 'Ваша улица: ' + (data && data.street));
setTimeout(async () => {
await bot.sendMessage(chatId, 'Всю информацию вы получите в этом чате');
}, 3000)
} catch (e) {
console.log(e);
}
}
});
app.post('/web-data', async (req, res) => {
const {queryId, products = [], totalPrice} = req.body;
try {
await bot.answerWebAppQuery(queryId, {
type: 'article',
id: queryId,
title: 'Успешная покупка',
input_message_content: {
message_text: ` Поздравляю с покупкой, вы приобрели товар на сумму ${totalPrice}, ${products.map(item => item.title).join(', ')}`
}
})
return res.status(200).json({});
} catch (e) {
return res.status(500).json({})
}
})
const PORT = 8080;
app.listen(PORT, () => console.log('server started on PORT ' + PORT))