├── package.json ├── README.md ├── .gitignore └── api └── webhook.js /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "test-bot", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "devDependencies": { 13 | "ngrok": "^5.0.0-beta.2" 14 | }, 15 | "dependencies": { 16 | "node-telegram-bot-api": "^0.65.1" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Telegram bot 2 | 3 | A test telegram bot which is deployed to Vercel. See my blog post on it: 4 | 5 | [https://marclittlemore.com/serverless-telegram-chatbot-vercel](https://marclittlemore.com/serverless-telegram-chatbot-vercel/) 6 | 7 | ## Installing 8 | 9 | Clone this repository into a new directory and install the dependencies: 10 | 11 | ```bash 12 | git clone git@github.com:MarcL/telegram-test-bot.git 13 | cd telegram-test-bot 14 | npm install 15 | ``` 16 | 17 | ## Deploying to Vercel 18 | 19 | Install the Vercel command line tools using `npm`: 20 | 21 | ```bash 22 | npm install -g vercel 23 | ``` 24 | 25 | Deploy the project to your Vercel account: 26 | 27 | ```bash 28 | vercel 29 | ``` 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | 39 | lib 40 | .env 41 | .env.build 42 | 43 | .vercel 44 | -------------------------------------------------------------------------------- /api/webhook.js: -------------------------------------------------------------------------------- 1 | // Require our Telegram helper package 2 | const TelegramBot = require('node-telegram-bot-api'); 3 | 4 | // Export as an asynchronous function 5 | // We'll wait until we've responded to the user 6 | module.exports = async (request, response) => { 7 | try { 8 | // Create our new bot handler with the token 9 | // that the Botfather gave us 10 | // Use an environment variable so we don't expose it in our code 11 | const bot = new TelegramBot(process.env.TELEGRAM_TOKEN); 12 | 13 | // Retrieve the POST request body that gets sent from Telegram 14 | const { body } = request; 15 | 16 | // Ensure that this is a message being sent 17 | if (body.message) { 18 | // Retrieve the ID for this chat 19 | // and the text that the user sent 20 | const { chat: { id }, text } = body.message; 21 | 22 | // Create a message to send back 23 | // We can use Markdown inside this 24 | const message = `✅ Thanks for your message: *"${text}"*\nHave a great day! 👋🏻`; 25 | 26 | // Send our new message back in Markdown 27 | await bot.sendMessage(id, message, {parse_mode: 'Markdown'}); 28 | } 29 | } 30 | catch(error) { 31 | // If there was an error sending our message then we 32 | // can log it into the Vercel console 33 | console.error('Error sending message'); 34 | console.log(error.toString()); 35 | } 36 | 37 | // Acknowledge the message with Telegram 38 | // by sending a 200 HTTP status code 39 | response.send('OK'); 40 | }; 41 | --------------------------------------------------------------------------------