├── .gitignore ├── db.js ├── models.js ├── package.json ├── options.js └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | .idea 3 | -------------------------------------------------------------------------------- /db.js: -------------------------------------------------------------------------------- 1 | const {Sequelize} = require('sequelize'); 2 | 3 | module.exports = new Sequelize( 4 | 'telega_bot', 5 | 'root', 6 | 'root', 7 | { 8 | host: '5.188.128.98', 9 | port: '6432', 10 | dialect: 'postgres' 11 | } 12 | ) 13 | -------------------------------------------------------------------------------- /models.js: -------------------------------------------------------------------------------- 1 | const sequelize = require('./db'); 2 | const {DataTypes} = require('sequelize'); 3 | 4 | const User = sequelize.define('user', { 5 | id: {type: DataTypes.INTEGER, primaryKey: true, unique: true, autoIncrement: true}, 6 | chatId: {type: DataTypes.STRING, unique: true}, 7 | right: {type: DataTypes.INTEGER, defaultValue: 0}, 8 | wrong: {type: DataTypes.INTEGER, defaultValue: 0}, 9 | }) 10 | 11 | module.exports = User; 12 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "telegram-bot-course", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "dev": "nodemon index.js", 8 | "start": "node index.js" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC", 13 | "dependencies": { 14 | "node-telegram-bot-api": "^0.52.0", 15 | "nodemon": "^2.0.7", 16 | "pg": "^8.6.0", 17 | "pg-hstore": "^2.3.3", 18 | "sequelize": "^6.6.2" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /options.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | gameOptions: { 3 | reply_markup: JSON.stringify({ 4 | inline_keyboard: [ 5 | [{text: '1', callback_data: '1'}, {text: '2', callback_data: '2'}, {text: '3', callback_data: '3'}], 6 | [{text: '4', callback_data: '4'}, {text: '5', callback_data: '5'}, {text: '6', callback_data: '6'}], 7 | [{text: '7', callback_data: '7'}, {text: '8', callback_data: '8'}, {text: '9', callback_data: '9'}], 8 | [{text: '0', callback_data: '0'}], 9 | ] 10 | }) 11 | }, 12 | 13 | againOptions: { 14 | reply_markup: JSON.stringify({ 15 | inline_keyboard: [ 16 | [{text: 'Играть еще раз', callback_data: '/again'}], 17 | ] 18 | }) 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const TelegramApi = require('node-telegram-bot-api') 2 | const {gameOptions, againOptions} = require('./options') 3 | const sequelize = require('./db'); 4 | const UserModel = require('./models'); 5 | 6 | const token = '1702630643:AAHXiY0MRDKeF1XcuGTxWmpgdcCdAgzt4gE' 7 | 8 | const bot = new TelegramApi(token, {polling: true}) 9 | 10 | const chats = {} 11 | 12 | 13 | const startGame = async (chatId) => { 14 | await bot.sendMessage(chatId, `Сейчас я загадаю цифру от 0 до 9, а ты должен ее угадать!`); 15 | const randomNumber = Math.floor(Math.random() * 10) 16 | chats[chatId] = randomNumber; 17 | await bot.sendMessage(chatId, 'Отгадывай', gameOptions); 18 | } 19 | 20 | const start = async () => { 21 | 22 | try { 23 | await sequelize.authenticate() 24 | await sequelize.sync() 25 | } catch (e) { 26 | console.log('Подключение к бд сломалось', e) 27 | } 28 | 29 | bot.setMyCommands([ 30 | {command: '/start', description: 'Начальное приветствие'}, 31 | {command: '/info', description: 'Получить информацию о пользователе'}, 32 | {command: '/game', description: 'Игра угадай цифру'}, 33 | ]) 34 | 35 | bot.on('message', async msg => { 36 | const text = msg.text; 37 | const chatId = msg.chat.id; 38 | 39 | try { 40 | if (text === '/start') { 41 | await UserModel.create({chatId}) 42 | await bot.sendSticker(chatId, 'https://tlgrm.ru/_/stickers/ea5/382/ea53826d-c192-376a-b766-e5abc535f1c9/7.webp') 43 | return bot.sendMessage(chatId, `Добро пожаловать в телеграм бот автора ютуб канала ULBI TV`); 44 | } 45 | if (text === '/info') { 46 | const user = await UserModel.findOne({chatId}) 47 | return bot.sendMessage(chatId, `Тебя зовут ${msg.from.first_name} ${msg.from.last_name}, в игре у тебя правильных ответов ${user.right}, неправильных ${user.wrong}`); 48 | } 49 | if (text === '/game') { 50 | return startGame(chatId); 51 | } 52 | return bot.sendMessage(chatId, 'Я тебя не понимаю, попробуй еще раз!)'); 53 | } catch (e) { 54 | return bot.sendMessage(chatId, 'Произошла какая то ошибочка!)'); 55 | } 56 | 57 | }) 58 | 59 | bot.on('callback_query', async msg => { 60 | const data = msg.data; 61 | const chatId = msg.message.chat.id; 62 | if (data === '/again') { 63 | return startGame(chatId) 64 | } 65 | const user = await UserModel.findOne({chatId}) 66 | if (data == chats[chatId]) { 67 | user.right += 1; 68 | await bot.sendMessage(chatId, `Поздравляю, ты отгадал цифру ${chats[chatId]}`, againOptions); 69 | } else { 70 | user.wrong += 1; 71 | await bot.sendMessage(chatId, `К сожалению ты не угадал, бот загадал цифру ${chats[chatId]}`, againOptions); 72 | } 73 | await user.save(); 74 | }) 75 | } 76 | 77 | start() 78 | --------------------------------------------------------------------------------