Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. const TelegramBot = require('node-telegram-bot-api');
  2. const user = require('./models/user');
  3. const event = require('./models/event');
  4.  
  5. // replace the value below with the Telegram token you receive from @BotFather
  6. const token = '885391252:AAGvMLDuOXELn7kKjSb1RI48QW4idPgGw4A';
  7.  
  8. // Create a bot that uses 'polling' to fetch new updates
  9. const bot = new TelegramBot(token, { polling: true });
  10.  
  11. bot.onText(/\/RazmerChlena/, async (msg, match) => {
  12. const chatId = msg.chat.id;
  13. try {
  14. let fetchedUser = await user.findUserByTlegramLogin(msg.from.username);
  15. if (fetchedUser) {
  16. const resp = Math.ceil(Math.random() * 32);
  17. if (fetchedUser.telegramChatId)
  18. bot.sendMessage(tee, resp);
  19. else {
  20. fetchedUser.telegramChatId = chatId;
  21. let updUser = await user.update(fetchedUser);
  22. }
  23. bot.sendMessage(chatId, resp);
  24. } else {
  25. bot.sendMessage(chatId, "Enemy spy detected!!! Ruslan ne proidet!!!");
  26. }
  27. } catch (err) {
  28. console.log(err);
  29. }
  30. });
  31.  
  32. bot.onText(/\/hello/, (msg, match) => {
  33. const chatId = msg.chat.id;
  34. const resp = "Hello, is anybody else there?";
  35. bot.sendMessage(chatId, resp);
  36. });
  37.  
  38. bot.onText(/\/events/, async (msg, match) => {
  39. const chatId = msg.chat.id;
  40. try {
  41. let fetchedUser = await user.findUserByTlegramLogin(msg.from.username);
  42. if (fetchedUser) {
  43. if (fetchedUser.events.length == 0)
  44. bot.sendMessage(chatId, "You don`t have any events, please add some on our website!");
  45. else {
  46.  
  47. let events = "\n";
  48. for (let i = 0; i < fetchedUser.events.length; i++) {
  49. events += (`${i + 1} : ` + fetchedUser.events[i].name + "\n");
  50. }
  51. const htmlEvents = `
  52. <strong>Your events : </strong>
  53. <pre>
  54. ${events}
  55. </pre>
  56. `
  57. bot.sendMessage(chatId, htmlEvents, {
  58. parse_mode: 'HTML'
  59. });
  60. }
  61. } else {
  62. bot.sendMessage(chatId, "Enemy spy detected!!! Ruslan ne proidet!!!");
  63. }
  64. } catch (err) {
  65. console.log(err);
  66. }
  67. });
  68.  
  69. bot.onText(/\/help/, async (msg, match) => {
  70. const chatId = msg.chat.id;
  71. try {
  72. let fetchedUser = await user.findUserByTlegramLogin(msg.from.username);
  73. if (fetchedUser) {
  74. bot.sendMessage(chatId, "To start using bot just edit your telegram username in your profile");
  75. } else {
  76. bot.sendMessage(chatId, "Enemy spy detected!!! Ruslan ne proidet!!!");
  77. }
  78. } catch (err) {
  79. console.log(err);
  80. }
  81. });
  82.  
  83. bot.onText(/\/start/, async (msg, match) => {
  84. const chatId = msg.chat.id;
  85. bot.sendMessage(chatId, "Hello my young user, to start enjoying your life just edit your profile, kk w8!");
  86. });
  87.  
  88. module.exports = bot;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement