commonroutesBot/main.js
main.jsRaw
1const TelegramBot = require('node-telegram-bot-api');
2
3var express = require("express"),
4 app = express(),
5 bodyParser = require("body-parser"),
6 methodOverride = require("method-override");
7
8const config = require('./config');
9
10
11
12let chatId = ''; // only one chatid
13
14const bot = new TelegramBot(config.telegramToken, {polling: true});
15bot.onText(/\/start/, (msg, match) => {
16 console.log('chatId', chatId);
17 if(chatId) {
18 // only allow one chatId
19 } else {
20 chatId = msg.chat.id;
21 console.log('chatId', chatId);
22 var msg = `
23 Wellcome to commonroutesTelegramBot. Available commands:
24 /start
25 /pong
26 New messages will be send when new travels are published
27 `;
28 bot.sendMessage(chatId, msg);
29 }
30});
31
32bot.onText(/\/ping/, (msg, match) => {
33 if(chatId) {
34 let msg = `pong
35 Bot alive.`;
36 bot.sendMessage(chatId, msg);
37 }
38});
39
40var newtravelHandler = function(req, res) {
41 console.log(req.body);
42 let msg = `
43 New ` + req.body.type + ` travel published:
44 - ๐ฃ title: ` + req.body.title + `
45 - ๐ description: ` + req.body.description + `
46 - ๐ from: ` + req.body.from.name + `
47 - ๐ to: ` + req.body.to.name + `
48 - ๐ date: ` + req.body.date + `
49 - ๐บ seats: ` + req.body.seats;
50 if(req.body.package) {
51 msg += `
52 - ๐ฆ can carry package`;
53 }
54
55 msg += `
56
57 ๐ฑ Check all the info in the app, or in the web visualizer https://routes.fair.coop/app/#!/travel/` + req.body._id + `
58 `;
59 bot.sendMessage(chatId, msg);
60
61 res.status(200).jsonp({});
62}
63
64// API
65var apiRoutes = express.Router();
66
67app.use(bodyParser.urlencoded({
68 extended: false
69}));
70app.use(bodyParser.json());
71app.use(methodOverride());
72
73apiRoutes.route('/travel')
74 .post(newtravelHandler);
75
76app.use('/api', apiRoutes);
77app.listen(config.port, 'localhost', function() {
78 console.log("Node server running on http://localhost:" + config.port);
79});