diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c86f4ab --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +node_modules +package-lock.json +config.js +botimg.png diff --git a/README.md b/README.md new file mode 100644 index 0000000..d735c86 --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +# commonroutesBot +Telegram bot to notify when new travels are published. + + +### Common Routes repositories +- server code: https://github.com/arnaucube/commonroutesServer +- frontend app code: https://github.com/arnaucube/commonroutesApp +- frontend webapp code: https://github.com/arnaucube/commonroutesWebApp +- images server: https://github.com/arnaucube/goImgServer +- admin web: https://github.com/arnaucube/commonroutesAdminWeb +- landing page: https://github.com/arnaucube/commonroutesLandingPage +- telegram bot: https://github.com/arnaucube/commonroutesBot diff --git a/configEXAMPLE.js b/configEXAMPLE.js new file mode 100644 index 0000000..918b5e6 --- /dev/null +++ b/configEXAMPLE.js @@ -0,0 +1,4 @@ +module.exports = { + 'telegramToken': 'tokenhere', + 'port': 3003 +}; diff --git a/main.js b/main.js new file mode 100644 index 0000000..a2cf0ac --- /dev/null +++ b/main.js @@ -0,0 +1,79 @@ +const TelegramBot = require('node-telegram-bot-api'); + +var express = require("express"), + app = express(), + bodyParser = require("body-parser"), + methodOverride = require("method-override"); + +const config = require('./config'); + + + +let chatId = ''; // only one chatid + +const bot = new TelegramBot(config.telegramToken, {polling: true}); +bot.onText(/\/start/, (msg, match) => { + console.log('chatId', chatId); + if(chatId) { + // only allow one chatId + } else { + chatId = msg.chat.id; + console.log('chatId', chatId); + var msg = ` + Wellcome to commonroutesTelegramBot. Available commands: + /start + /pong + New messages will be send when new travels are published + `; + bot.sendMessage(chatId, msg); + } +}); + +bot.onText(/\/ping/, (msg, match) => { + if(chatId) { + let msg = `pong + Bot alive.`; + bot.sendMessage(chatId, msg); + } +}); + +var newtravelHandler = function(req, res) { + console.log(req.body); + let msg = ` + New ` + req.body.type + ` travel published: + - 📣 title: ` + req.body.title + ` + - 📄 description: ` + req.body.description + ` + - 🌏 from: ` + req.body.from.name + ` + - 🌍 to: ` + req.body.to.name + ` + - 📆 date: ` + req.body.date + ` + - 💺 seats: ` + req.body.seats; + if(req.body.package) { + msg += ` + - 📦 can carry package`; + } + + msg += ` + + 📱 Check all the info in the app, or in the web visualizer https://routes.fair.coop/app/#!/travel/` + req.body._id + ` + `; + bot.sendMessage(chatId, msg); + + res.status(200).jsonp({}); +} + +// API +var apiRoutes = express.Router(); + +app.use(bodyParser.urlencoded({ + extended: false +})); +app.use(bodyParser.json()); +app.use(methodOverride()); + +apiRoutes.route('/travel') + .post(newtravelHandler); + +app.use('/api', apiRoutes); +app.listen(config.port, 'localhost', function() { + console.log("Node server running on http://localhost:" + config.port); +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..a64046c --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "commonroutesBot", + "version": "0.0.1", + "description": "", + "main": "main.js", + "scripts": { + "prestart": "npm install", + "start": "node main.js" + }, + "author": "", + "license": "GPLv3", + "dependencies": { + "node-telegram-bot-api": "^0.30.0", + "express": "^4.7.1", + "method-override": "^2.1.2", + "body-parser": "latest" + } +}