├── README.md ├── package.json └── src └── app.js /README.md: -------------------------------------------------------------------------------- 1 | Codebox 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "telegram-bot-sitepoint", 3 | "version": "1.0.0", 4 | "description": "Telegram bot for labelling articles", 5 | "main": "src/app.js", 6 | "scripts": { 7 | "start": "node src/app.js", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "keywords": [ 11 | "Telegram", 12 | "Node.js", 13 | "Javascript" 14 | ], 15 | "author": "Michiel Mulders", 16 | "license": "ISC", 17 | "dependencies": { 18 | "dotenv": "^8.0.0", 19 | "node-telegram-bot-api": "^0.30.0" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/app.js: -------------------------------------------------------------------------------- 1 | require('dotenv').config(); 2 | const TelegramBot = require('node-telegram-bot-api'); 3 | 4 | const token = process.env.TOKEN; 5 | 6 | // Created instance of TelegramBot 7 | const bot = new TelegramBot(token, { 8 | polling: true 9 | }); 10 | 11 | // In-memory storage 12 | const URLs = []; 13 | const URLLabels = []; 14 | let tempSiteURL = ''; 15 | 16 | // Listener (handler) for telegram's /bookmark event 17 | bot.onText(/\/bookmark/, (msg, match) => { 18 | const chatId = msg.chat.id; 19 | const url = match.input.split(' ')[1]; 20 | // 'msg' is the received Message from Telegram 21 | // 'match' is the result of executing the regexp above on the text content 22 | // of the message 23 | 24 | if (url === undefined) { 25 | bot.sendMessage( 26 | chatId, 27 | 'Please provide URL of article!', 28 | ); 29 | return; 30 | } 31 | 32 | URLs.push(url); 33 | bot.sendMessage( 34 | chatId, 35 | 'URL has been successfully saved!', 36 | ); 37 | }); 38 | bot.onText(/\/articles/, (msg, match) => { 39 | const chatId = msg.chat.id; 40 | 41 | 42 | 43 | 44 | bot.sendMessage( 45 | chatId, 46 | JSON.stringify(URLs), 47 | ); 48 | }); 49 | 50 | // Listener (handler) for telegram's /label event 51 | bot.onText(/\/label/, (msg, match) => { 52 | const chatId = msg.chat.id; 53 | const url = match.input.split(' ')[1]; 54 | 55 | if (url === undefined) { 56 | bot.sendMessage( 57 | chatId, 58 | 'Please provide URL of article!', 59 | ); 60 | return; 61 | } 62 | 63 | tempSiteURL = url; 64 | bot.sendMessage( 65 | chatId, 66 | 'URL has been successfully saved!', 67 | { 68 | reply_markup: { 69 | inline_keyboard: [[ 70 | { 71 | text: 'Development', 72 | callback_data: 'development' 73 | }, { 74 | text: 'Lifestyle', 75 | callback_data: 'lifestyle' 76 | }, { 77 | text: 'Other', 78 | callback_data: 'other' 79 | } 80 | ]] 81 | } 82 | } 83 | ); 84 | }); 85 | 86 | // Listener (handler) for callback data from /label command 87 | bot.on('callback_query', (callbackQuery) => { 88 | const message = callbackQuery.message; 89 | const category = callbackQuery.data; 90 | 91 | URLLabels.push({ 92 | url: tempSiteURL, 93 | label: category, 94 | }); 95 | 96 | tempSiteURL = ''; 97 | 98 | bot.sendMessage(message.chat.id, `URL has been labeled with category "${category}"`); 99 | }); 100 | 101 | // Listener (handler) for showcasing different keyboard layout 102 | bot.onText(/\/keyboard/, (msg) => { 103 | bot.sendMessage(msg.chat.id, 'Alternative keybaord layout', { 104 | 'reply_markup': { 105 | 'keyboard': [['Sample text', 'Second sample'], ['Keyboard'], ['I\'m robot']], 106 | resize_keyboard: true, 107 | one_time_keyboard: true, 108 | force_reply: true, 109 | } 110 | }); 111 | }); 112 | 113 | // Inline keyboard options 114 | const inlineKeyboard = { 115 | reply_markup: { 116 | inline_keyboard: [ 117 | [ 118 | { 119 | text: 'YES', 120 | callback_data: JSON.stringify({ 121 | 'command': 'mycommand1', 122 | 'answer': 'YES' 123 | }) 124 | }, 125 | { 126 | text: 'NO', 127 | callback_data: JSON.stringify({ 128 | 'command': 'mycommand1', 129 | 'answer': 'NO' 130 | }) 131 | }, 132 | ] 133 | ] 134 | } 135 | }; 136 | 137 | // Listener (handler) for showcasing inline keyboard layout 138 | bot.onText(/\/inline/, (msg) => { 139 | bot.sendMessage(msg.chat.id, 'You have to agree with me, OK?', inlineKeyboard); 140 | }); 141 | 142 | // Keyboard layout for requesting phone number access 143 | const requestPhoneKeyboard = { 144 | "reply_markup": { 145 | "one_time_keyboard": true, 146 | "keyboard": [[{ 147 | text: "My phone number", 148 | request_contact: true, 149 | one_time_keyboard: true 150 | }], ["Cancel"]] 151 | } 152 | }; 153 | 154 | // Listener (handler) for retrieving phone number 155 | bot.onText(/\/phone/, (msg) => { 156 | bot.sendMessage(msg.chat.id, 'Can we get access to your phone number?', requestPhoneKeyboard); 157 | }); 158 | 159 | // Handler for phone number request when user gives permission 160 | bot.on('contact', async (msg) => { 161 | const phone = msg.contact.phone_number; 162 | bot.sendMessage(msg.chat.id, `Phone number saved: ${phone}`); 163 | console.log(phone) 164 | }) 165 | 166 | // Listener (handler) for telegram's /start event 167 | // This event happened when you start the conversation with both by the very first time 168 | // Provide the list of available commands 169 | bot.onText(/\/start/, (msg) => { 170 | const chatId = msg.chat.id; 171 | bot.sendMessage( 172 | chatId, 173 | ` 174 | Welcome at Codebox, thank you for using my service 175 | 176 | Available commands: 177 | 178 | /bookmark URL - save interesting article URL 179 | /start an initial point to use Codebox 180 | /lable to label your articles 181 | /phone to share your phone number with the Codebox 182 | /hi to say a hi to codebox 183 | ... 184 | `, { 185 | parse_mode: 'HTML', 186 | } 187 | ); 188 | }); 189 | bot.onText(/\/hi/, (msg) => { 190 | const chatId = msg.chat.id; 191 | bot.sendMessage( 192 | chatId, 193 | `hello new codeboxer welcome to codebox land it is a new bot and it is gonna grow up by your content.please feed it as much as you can! :)`, { 194 | parse_mode: 'HTML', 195 | } 196 | ); 197 | }); 198 | --------------------------------------------------------------------------------