├── package.json ├── options.js └── index.js /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "telegram-bot", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "node index.js", 8 | "dev": "nodemon index.js" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC", 13 | "devDependencies": { 14 | "nodemon": "^3.0.1" 15 | }, 16 | "dependencies": { 17 | "node-telegram-bot-api": "^0.61.0" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /options.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | gameOptions: { 3 | reply_markup: { 4 | inline_keyboard: [ 5 | [ 6 | { 7 | text: '1', 8 | callback_data: '1', 9 | }, 10 | { 11 | text: '2', 12 | callback_data: '2', 13 | }, 14 | { 15 | text: '3', 16 | callback_data: '3', 17 | }, 18 | ], 19 | [ 20 | { 21 | text: '4', 22 | callback_data: '4', 23 | }, 24 | { 25 | text: '5', 26 | callback_data: '5', 27 | }, 28 | { 29 | text: '6', 30 | callback_data: '6', 31 | }, 32 | ], 33 | [ 34 | { 35 | text: '7', 36 | callback_data: '7', 37 | }, 38 | { 39 | text: '8', 40 | callback_data: '8', 41 | }, 42 | { 43 | text: '9', 44 | callback_data: '9', 45 | }, 46 | ], 47 | [ 48 | { 49 | text: '0', 50 | callback_data: '0', 51 | }, 52 | ], 53 | ], 54 | }, 55 | }, 56 | againOptions: { 57 | reply_markup: { 58 | inline_keyboard: [ 59 | [ 60 | { 61 | text: 'Qaytadan boshlash', 62 | callback_data: '/again', 63 | }, 64 | ], 65 | ], 66 | }, 67 | }, 68 | }; 69 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const TelegramBot = require('node-telegram-bot-api'); 2 | const { gameOptions, againOptions } = require('./options'); 3 | 4 | const token = '6390768164:AAFIhkyOne7AXw8iRT-rNEpK3t102dWnoxo'; 5 | 6 | const bot = new TelegramBot(token, { polling: true }); 7 | 8 | const obj = {}; 9 | 10 | const startGame = async chatId => { 11 | await bot.sendMessage( 12 | chatId, 13 | "Kompyuter 0 dan 9 gacham son o'yladi, siz usha sonni toposhga xarakat qiling." 14 | ); 15 | const randomNumber = Math.floor(Math.random() * 10); 16 | obj[chatId] = randomNumber; 17 | await bot.sendMessage(chatId, "Tog'ri sonni toping", gameOptions); 18 | }; 19 | 20 | const bootstrap = () => { 21 | bot.setMyCommands([ 22 | { 23 | command: '/start', 24 | description: "Bot haqida ma'lumot", 25 | }, 26 | { 27 | command: '/info', 28 | description: "O'zingiz haqingizda ma'lumot", 29 | }, 30 | { 31 | command: '/game', 32 | description: "O'yin o'ynash", 33 | }, 34 | ]); 35 | 36 | bot.on('message', async msg => { 37 | const text = msg.text; 38 | const chatId = msg.chat.id; 39 | 40 | if (text === '/start') { 41 | return bot.sendMessage( 42 | chatId, 43 | `Assalamu Alaykum xurmatli ${msg.from?.first_name} sizni o'quv botimizda ko'rib turganimizdan juda xursandmiz.` 44 | ); 45 | } 46 | 47 | if (text === '/info') { 48 | await bot.sendSticker( 49 | chatId, 50 | 'https://tlgrm.eu/_/stickers/4dd/300/4dd300fd-0a89-3f3d-ac53-8ec93976495e/2.webp' 51 | ); 52 | await bot.sendPhoto( 53 | chatId, 54 | 'https://sammi.ac/_next/image?url=https%3A%2F%2Fmedia.graphassets.com%2FEobVnxlQXmqvp3id2meQ&w=3840&q=75' 55 | ); 56 | return bot.sendMessage( 57 | chatId, 58 | `Sizning telegram username bu ${msg.from?.username}, sizning ismingiz esa ${msg.from?.first_name} ${msg.from?.last_name}` 59 | ); 60 | } 61 | 62 | if (text === '/game') { 63 | return startGame(chatId); 64 | } 65 | 66 | bot.sendMessage( 67 | chatId, 68 | 'Uzur men sizning gapingizga tushunmayapman!! ):' 69 | ); 70 | }); 71 | 72 | bot.on('callback_query', msg => { 73 | const data = msg.data; 74 | const chatId = msg.message.chat.id; 75 | 76 | if (data === '/again') { 77 | return startGame(chatId); 78 | } 79 | 80 | if (data == obj[chatId]) { 81 | return bot.sendMessage( 82 | chatId, 83 | `Tabriklaymiz siz tog'ri javob berdingiz, kompyuter ${obj[chatId]} sonni tanlagan edi` 84 | ); 85 | } else { 86 | return bot.sendMessage( 87 | chatId, 88 | `Siz notog'ri son tanladingiz tanlagan soningniz ${data}, kompyuter ${obj[chatId]} sonni tanlagan edi`, 89 | againOptions 90 | ); 91 | } 92 | }); 93 | }; 94 | 95 | bootstrap(); 96 | --------------------------------------------------------------------------------