├── .gitignore ├── package.json └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | .idea 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tg-web-app-bot", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "nodemon index.js" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "devDependencies": { 13 | "nodemon": "^2.0.19" 14 | }, 15 | "dependencies": { 16 | "cors": "^2.8.5", 17 | "express": "^4.18.1", 18 | "node-telegram-bot-api": "^0.58.0" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const TelegramBot = require('node-telegram-bot-api'); 2 | const express = require('express'); 3 | const cors = require('cors'); 4 | 5 | const token = '5336424335:AAGk0uyo0qqRCrKgvr2J7GrYKK1S0MF8878'; 6 | const webAppUrl = 'https://ornate-selkie-c27577.netlify.app'; 7 | 8 | const bot = new TelegramBot(token, {polling: true}); 9 | const app = express(); 10 | 11 | app.use(express.json()); 12 | app.use(cors()); 13 | 14 | bot.on('message', async (msg) => { 15 | const chatId = msg.chat.id; 16 | const text = msg.text; 17 | 18 | if(text === '/start') { 19 | await bot.sendMessage(chatId, 'Ниже появится кнопка, заполни форму', { 20 | reply_markup: { 21 | keyboard: [ 22 | [{text: 'Заполнить форму', web_app: {url: webAppUrl + '/form'}}] 23 | ] 24 | } 25 | }) 26 | 27 | await bot.sendMessage(chatId, 'Заходи в наш интернет магазин по кнопке ниже', { 28 | reply_markup: { 29 | inline_keyboard: [ 30 | [{text: 'Сделать заказ', web_app: {url: webAppUrl}}] 31 | ] 32 | } 33 | }) 34 | } 35 | 36 | if(msg?.web_app_data?.data) { 37 | try { 38 | const data = JSON.parse(msg?.web_app_data?.data) 39 | console.log(data) 40 | await bot.sendMessage(chatId, 'Спасибо за обратную связь!') 41 | await bot.sendMessage(chatId, 'Ваша страна: ' + data?.country); 42 | await bot.sendMessage(chatId, 'Ваша улица: ' + data?.street); 43 | 44 | setTimeout(async () => { 45 | await bot.sendMessage(chatId, 'Всю информацию вы получите в этом чате'); 46 | }, 3000) 47 | } catch (e) { 48 | console.log(e); 49 | } 50 | } 51 | }); 52 | 53 | app.post('/web-data', async (req, res) => { 54 | const {queryId, products = [], totalPrice} = req.body; 55 | try { 56 | await bot.answerWebAppQuery(queryId, { 57 | type: 'article', 58 | id: queryId, 59 | title: 'Успешная покупка', 60 | input_message_content: { 61 | message_text: ` Поздравляю с покупкой, вы приобрели товар на сумму ${totalPrice}, ${products.map(item => item.title).join(', ')}` 62 | } 63 | }) 64 | return res.status(200).json({}); 65 | } catch (e) { 66 | return res.status(500).json({}) 67 | } 68 | }) 69 | 70 | const PORT = 8000; 71 | 72 | app.listen(PORT, () => console.log('server started on PORT ' + PORT)) 73 | --------------------------------------------------------------------------------