You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.9 KiB

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