Преглед на файлове

- Add app autorisation through Telegram

Parad0x преди 7 години
родител
ревизия
7918ee1b49
променени са 4 файла, в които са добавени 62 реда и са изтрити 4 реда
  1. 56 0
      src/bot/app.js
  2. 2 0
      src/bot/index.js
  3. 3 2
      src/connectionHandler.js
  4. 1 2
      src/db/App.js

+ 56 - 0
src/bot/app.js

@@ -0,0 +1,56 @@
+//@flow
+
+const moment = require("moment");
+
+const telegraf = require("../telegraf");
+const Extra = require('telegraf/extra');
+const Markup = require('telegraf/markup');
+const db = require("../db");
+
+function app() {
+  if (telegraf.bot == null) {
+    throw new Error("Bot uninitialized");
+  }
+
+  telegraf.bot.hears(/✔️\[(\d+)\] [\w ]+/, async (ctx) => {
+    let id = ctx.match[1];
+    let app = await db.App.findById(id);
+    await app.update({
+      activated: true
+    })
+    ctx.reply(`${app.name} accepted`);
+  });
+
+  telegraf.bot.hears(/❌\[(\d+)\] [\w ]+/, async (ctx) => {
+    let id = ctx.match[1];
+    let app = await db.App.findById(id);
+    await app.destroy();
+    ctx.reply(`${app.name} deleted`);
+  });
+
+  telegraf.bot.command("pendingApps", async (ctx) => {
+    let pendingApps = await db.App.findAll({
+      where: {
+        activated: false
+      },
+      limit: 5
+    });
+    if (pendingApps <= 0) {
+      ctx.reply("No application is waiting for activation");
+      return;
+    }
+    let apps = pendingApps.map(el => {
+      return [
+        `✔️[${el.id}] ${el.name}`,
+        `❌[${el.id}] ${el.name}`
+      ]
+    });
+    console.log(apps);
+    return ctx.reply('Activate app', Markup
+      .keyboard(apps)
+      .oneTime()
+      .resize()
+      .extra());
+  });
+}
+module.exports = app;

+ 2 - 0
src/bot/index.js

@@ -1,6 +1,7 @@
 //@flow
 
 const auth = require("./auth");
+const app = require("./app");
 const telegraf = require("../telegraf");
 const commandParts = require('telegraf-command-parts');
 
@@ -10,6 +11,7 @@ function bot() {
 
   // Modules
   auth();
+  app();
 
   // Start
   telegraf.bot.startPolling();

+ 3 - 2
src/connectionHandler.js

@@ -83,11 +83,12 @@ class Connection {
     const auth = await this.queue.remove();
     const user = await db.App.findOne({
       where: {
-        auth
+        auth,
+        activated: true
       }
     });
     if (!user) {
-      this.write("error:auth unmatched;");
+      this.write("error:auth unmatched or not activated;");
       return;
     }
     this.session = {

+ 1 - 2
src/db/App.js

@@ -11,8 +11,7 @@ const App = sequelize.define("app", {
   auth: {
     type: Sequelize.STRING,
     defaultValue: "",
-    unique: true,
-    primaryKey: true
+    unique: true
   },
   activated: {
     type: Sequelize.BOOLEAN,