app.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //@flow
  2. const moment = require("moment");
  3. const telegraf = require("../telegraf");
  4. const Extra = require('telegraf/extra');
  5. const Markup = require('telegraf/markup');
  6. const db = require("../db");
  7. function app() {
  8. if (telegraf.bot == null) {
  9. throw new Error("Bot uninitialized");
  10. }
  11. telegraf.bot.hears(/✔️\[(\d+)\] [\w ]+/, async (ctx) => {
  12. let id = ctx.match[1];
  13. let app = await db.App.findById(id);
  14. await app.update({
  15. activated: true
  16. })
  17. ctx.reply(`${app.name} accepted`);
  18. });
  19. telegraf.bot.hears(/❌\[(\d+)\] [\w ]+/, async (ctx) => {
  20. let id = ctx.match[1];
  21. let app = await db.App.findById(id);
  22. await app.destroy();
  23. ctx.reply(`${app.name} deleted`);
  24. });
  25. telegraf.bot.command("pendingApps", async (ctx) => {
  26. let pendingApps = await db.App.findAll({
  27. where: {
  28. activated: false
  29. },
  30. limit: 5
  31. });
  32. if (pendingApps <= 0) {
  33. ctx.reply("No application is waiting for activation");
  34. return;
  35. }
  36. let apps = pendingApps.map(el => {
  37. return [
  38. `✔️[${el.id}] ${el.name}`,
  39. `❌[${el.id}] ${el.name}`
  40. ]
  41. });
  42. console.log(apps);
  43. return ctx.reply('Activate app', Markup
  44. .keyboard(apps)
  45. .oneTime()
  46. .resize()
  47. .extra());
  48. });
  49. }
  50. module.exports = app;