├── .gitignore ├── Procfile ├── README.md ├── bot.js ├── index.js ├── package.json └── web.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | _temp 6 | 7 | # Runtime data 8 | pids 9 | *.pid 10 | *.seed 11 | 12 | # Directory for instrumented libs generated by jscoverage/JSCover 13 | lib-cov 14 | 15 | # Coverage directory used by tools like istanbul 16 | coverage 17 | 18 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 19 | .grunt 20 | 21 | # node-waf configuration 22 | .lock-wscript 23 | 24 | # Compiled binary addons (http://nodejs.org/api/addons.html) 25 | build/Release 26 | 27 | # Dependency directory 28 | # https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git 29 | node_modules/* 30 | 31 | # Heroku evnironment configuration 32 | .env 33 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: node index.js 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # telegram-bot-webhook 2 | A simple telegram bot server in node.js using webhook. 3 | 4 | see: [Configure a telegram bot webhook into an existing express app](http://mvalipour.github.io/node.js/2015/12/06/telegram-bot-webhook-existing-express) 5 | 6 | # Getting Started 7 | To test in a local environment you need to create a `.env` file in the root directory of the project and run heroku local 8 | 9 | To run online you need to set a couple of config vars with `heroku config:set TOKEN=youmustputherethebottoken` and `heroku config:set HEROKU_URL=$(heroku info -s | grep web-url | cut -d= -f2)` 10 | -------------------------------------------------------------------------------- /bot.js: -------------------------------------------------------------------------------- 1 | var token = process.env.TOKEN; 2 | 3 | var Bot = require('node-telegram-bot-api'); 4 | var bot; 5 | 6 | if(process.env.NODE_ENV === 'production') { 7 | bot = new Bot(token); 8 | bot.setWebHook(process.env.HEROKU_URL + bot.token); 9 | } 10 | else { 11 | bot = new Bot(token, { polling: true }); 12 | } 13 | 14 | console.log('bot server started...'); 15 | 16 | // hello command 17 | bot.onText(/^\/say_hello (.+)$/, function (msg, match) { 18 | var name = match[1]; 19 | bot.sendMessage(msg.chat.id, 'Hello ' + name + '!').then(function () { 20 | // reply sent! 21 | }); 22 | }); 23 | 24 | // sum command 25 | bot.onText(/^\/sum((\s+\d+)+)$/, function (msg, match) { 26 | var result = 0; 27 | match[1].trim().split(/\s+/).forEach(function (i) { 28 | result += (+i || 0); 29 | }) 30 | bot.sendMessage(msg.chat.id, result).then(function () { 31 | // reply sent! 32 | }); 33 | }); 34 | 35 | module.exports = bot; 36 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var bot = require('./bot'); 2 | require('./web')(bot); 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "telegram-demo-bot", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "dependencies": { 7 | "body-parser": "^1.14.1", 8 | "express": "^4.13.3", 9 | "node-telegram-bot-api": "^0.20.1" 10 | }, 11 | "devDependencies": {}, 12 | "scripts": { 13 | "test": "echo \"Error: no test specified\" && exit 1", 14 | "start": "node index.js" 15 | }, 16 | "author": "", 17 | "license": "ISC" 18 | } 19 | -------------------------------------------------------------------------------- /web.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var packageInfo = require('./package.json'); 3 | var bodyParser = require('body-parser'); 4 | 5 | var app = express(); 6 | app.use(bodyParser.json()); 7 | 8 | app.get('/', function (req, res) { 9 | res.json({ version: packageInfo.version }); 10 | }); 11 | 12 | var server = app.listen(process.env.PORT, function () { 13 | var host = server.address().address; 14 | var port = server.address().port; 15 | 16 | console.log('Web server started at http://%s:%s', host, port); 17 | }); 18 | 19 | module.exports = function (bot) { 20 | app.post('/' + bot.token, function (req, res) { 21 | bot.processUpdate(req.body); 22 | res.sendStatus(200); 23 | }); 24 | }; 25 | --------------------------------------------------------------------------------