Bläddra i källkod

:sparkles: try qq websocket

LI JIAHAO 6 år sedan
förälder
incheckning
9fbc32260b
5 ändrade filer med 216 tillägg och 146 borttagningar
  1. 3 1
      config.examle.json
  2. 32 1
      dist/main.js
  3. 72 70
      dist/qq.js
  4. 36 1
      src/main.ts
  5. 73 73
      src/qq.ts

+ 3 - 1
config.examle.json

@@ -1,3 +1,5 @@
 {
-
+  "cq_ws_host": "127.0.0.1",
+  "cq_ws_port": "6700",
+  "cq_access_token": ""
 }

+ 32 - 1
dist/main.js

@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
 const commandLineUsage = require("command-line-usage");
 const log4js = require("log4js");
 const path = require("path");
+const qq_1 = require("./qq");
 const logger = log4js.getLogger();
 logger.level = 'info';
 const sections = [
@@ -42,4 +43,34 @@ catch (e) {
     console.log(usage);
     process.exit(1);
 }
-console.log(config);
+if (config.cq_ws_host === undefined) {
+    config.cq_ws_host = '127.0.0.1';
+    logger.warn('cq_ws_host is undefined, use 127.0.0.1 as default');
+}
+if (config.cq_ws_port === undefined) {
+    config.cq_ws_port = 6700;
+    logger.warn('cq_ws_port is undefined, use 6700 as default');
+}
+if (config.cq_access_token === undefined) {
+    config.cq_access_token = '';
+    logger.warn('cq_access_token is undefined, use empty string as default');
+}
+function handler(chat, args, bot) {
+    const config = {
+        message_type: chat.chatType,
+        user_id: chat.chatID,
+        group_id: chat.chatID,
+        discuss_id: chat.chatID,
+        message: JSON.stringify(args),
+    };
+    bot('send_msg', config);
+}
+const qq = new qq_1.default({
+    access_token: config.cq_access_token,
+    host: config.cq_ws_host,
+    port: config.cq_ws_port,
+    list: handler,
+    sub: handler,
+    unsub: handler,
+});
+qq.bot.connect();

+ 72 - 70
dist/qq.js

@@ -5,80 +5,82 @@ const log4js = require("log4js");
 const command_1 = require("./command");
 const logger = log4js.getLogger('cq-websocket');
 logger.level = 'info';
-function default_1(opt) {
-    this.bot = new CQWebsocket({
-        access_token: opt.access_token,
-        enableAPI: true,
-        enableEvent: true,
-        host: opt.host,
-        port: opt.port,
-    });
-    let retryInterval = 1000;
-    this.bot.on('socket.connect', () => {
-        logger.info('websocket connected');
-        retryInterval = 1000;
-    });
-    const connect = () => {
-        logger.warn('connecting to websocket...');
-        this.bot.connect();
-    };
-    const reconnect = () => {
-        retryInterval *= 2;
-        if (retryInterval > 300000)
-            retryInterval = 300000;
-        logger.info(`retrying in ${retryInterval / 1000}s...`);
-        setTimeout(() => {
-            logger.warn('reconnecting to websocket...');
-            connect();
-        }, retryInterval);
-    };
-    this.bot.on('socket.close', () => {
-        logger.error('websocket closed');
-        reconnect();
-    });
-    this.bot.on('socket.error', () => {
-        logger.error('websocket connect error');
-        reconnect();
-    });
-    this.bot.on('message', (e, context) => {
-        e.cancel();
-        const chat = {
-            chatType: context.message_type,
-            chatID: 0,
+class default_1 {
+    constructor(opt) {
+        this.retryInterval = 1000;
+        this.connect = () => {
+            logger.warn('connecting to websocket...');
+            this.bot.connect();
         };
-        switch (context.message_type) {
-            case ChatType.Private:
-                chat.chatID = context.user_id;
-                break;
-            case ChatType.Group:
-                chat.chatID = context.group_id;
-                break;
-            case ChatType.Discuss:
-                chat.chatID = context.discuss_id;
-                break;
-        }
-        let cmdObj = command_1.default(context.raw_message);
-        switch (cmdObj.cmd) {
-            case 'twitter_sub':
-            case 'twitter_subscribe':
-                opt.sub(chat, cmdObj.args);
-                return;
-            case 'twitter_unsub':
-            case 'twitter_unsubscribe':
-                opt.unsub(chat, cmdObj.args);
-                return;
-            case 'ping':
-            case 'twitter':
-                opt.list(chat, cmdObj.args);
-                return;
-            case 'help':
-                return `推特搬运机器人:
+        this.reconnect = () => {
+            this.retryInterval *= 2;
+            if (this.retryInterval > 300000)
+                this.retryInterval = 300000;
+            logger.info(`retrying in ${this.retryInterval / 1000}s...`);
+            setTimeout(() => {
+                logger.warn('reconnecting to websocket...');
+                this.connect();
+            }, this.retryInterval);
+        };
+        logger.info(`init cqwebsocket for ${opt.host}:${opt.port}, with access_token ${opt.access_token}`);
+        this.bot = new CQWebsocket({
+            access_token: opt.access_token,
+            enableAPI: true,
+            enableEvent: true,
+            host: opt.host,
+            port: opt.port,
+        });
+        this.bot.on('socket.connect', () => {
+            logger.info('websocket connected');
+            this.retryInterval = 1000;
+        });
+        this.bot.on('socket.close', () => {
+            logger.error('websocket closed');
+            this.reconnect();
+        });
+        this.bot.on('socket.error', () => {
+            logger.error('websocket connect error');
+            this.reconnect();
+        });
+        this.bot.on('message', (e, context) => {
+            e.cancel();
+            const chat = {
+                chatType: context.message_type,
+                chatID: 0,
+            };
+            switch (context.message_type) {
+                case ChatType.Private:
+                    chat.chatID = context.user_id;
+                    break;
+                case ChatType.Group:
+                    chat.chatID = context.group_id;
+                    break;
+                case ChatType.Discuss:
+                    chat.chatID = context.discuss_id;
+                    break;
+            }
+            let cmdObj = command_1.default(context.raw_message);
+            switch (cmdObj.cmd) {
+                case 'twitter_sub':
+                case 'twitter_subscribe':
+                    opt.sub(chat, cmdObj.args, this.bot);
+                    return;
+                case 'twitter_unsub':
+                case 'twitter_unsubscribe':
+                    opt.unsub(chat, cmdObj.args, this.bot);
+                    return;
+                case 'ping':
+                case 'twitter':
+                    opt.list(chat, cmdObj.args, this.bot);
+                    return;
+                case 'help':
+                    return `推特搬运机器人:
 /twitter - 查询当前聊天中的订阅
 /twitter_subscribe [链接] - 订阅 Twitter 搬运
 /twitter_unsubscribe [链接] - 退订 Twitter 搬运`;
-        }
-    });
-    return this;
+            }
+        });
+    }
 }
 exports.default = default_1;
 ;

+ 36 - 1
src/main.ts

@@ -3,6 +3,8 @@
 import * as commandLineUsage from 'command-line-usage';
 import * as log4js from 'log4js';
 import * as path from 'path';
+import QQBot from './qq';
+import * as CQWebsocket from 'cq-websocket';
 
 const logger = log4js.getLogger();
 logger.level = 'info';
@@ -48,4 +50,37 @@ try {
   process.exit(1);
 }
 
-console.log(config);
+if (config.cq_ws_host === undefined) {
+  config.cq_ws_host = '127.0.0.1';
+  logger.warn('cq_ws_host is undefined, use 127.0.0.1 as default');
+}
+if (config.cq_ws_port === undefined) {
+  config.cq_ws_port = 6700;
+  logger.warn('cq_ws_port is undefined, use 6700 as default');
+}
+if (config.cq_access_token === undefined) {
+  config.cq_access_token = '';
+  logger.warn('cq_access_token is undefined, use empty string as default');
+}
+
+function handler(chat: IChat, args: string[], bot: CQWebsocket) {
+  const config = {
+    message_type: chat.chatType,
+    user_id: chat.chatID,
+    group_id: chat.chatID,
+    discuss_id: chat.chatID,
+    message: JSON.stringify(args),
+  };
+  bot('send_msg', config)
+}
+
+const qq = new QQBot({
+  access_token: config.cq_access_token,
+  host: config.cq_ws_host,
+  port: config.cq_ws_port,
+  list: handler,
+  sub: handler,
+  unsub: handler,
+});
+
+qq.bot.connect();

+ 73 - 73
src/qq.ts

@@ -9,94 +9,94 @@ interface IQQProps {
   access_token: string;
   host: string;
   port: number;
-  list: (chat: IChat, args: string[]) => void;
-  sub: (chat: IChat, args: string[]) => void;
-  unsub: (chat: IChat, args: string[]) => void;
+  list: (chat: IChat, args: string[], bot: CQWebsocket) => void;
+  sub: (chat: IChat, args: string[], bot: CQWebsocket) => void;
+  unsub: (chat: IChat, args: string[], bot: CQWebsocket) => void;
 }
 
-interface IQQ {
-  bot: CQWebsocket;
-}
-
-export default function (opt: IQQProps): IQQ {
-  this.bot = new CQWebsocket({
-    access_token: opt.access_token,
-    enableAPI: true,
-    enableEvent: true,
-    host: opt.host,
-    port: opt.port,
-  });
-
-  let retryInterval = 1000;
-
-  this.bot.on('socket.connect', () => {
-    logger.info('websocket connected');
-    retryInterval = 1000;
-  });
+export default class {
 
-  const connect = () => {
+  public bot: CQWebsocket;
+  private retryInterval = 1000;
+  private connect = () => {
     logger.warn('connecting to websocket...');
     this.bot.connect();
   };
 
-  const reconnect = () => {
-    retryInterval *= 2;
-    if (retryInterval > 300000) retryInterval = 300000;
-    logger.info(`retrying in ${retryInterval / 1000}s...`);
+  private reconnect = () => {
+    this.retryInterval *= 2;
+    if (this.retryInterval > 300000) this.retryInterval = 300000;
+    logger.info(`retrying in ${this.retryInterval / 1000}s...`);
     setTimeout(() => {
       logger.warn('reconnecting to websocket...');
-      connect();
-    }, retryInterval);
+      this.connect();
+    }, this.retryInterval);
   };
 
-  this.bot.on('socket.close', () => {
-    logger.error('websocket closed');
-    reconnect();
-  });
+  constructor(opt: IQQProps) {
+    logger.info(`init cqwebsocket for ${opt.host}:${opt.port}, with access_token ${opt.access_token}`);
+
+    this.bot = new CQWebsocket({
+      access_token: opt.access_token,
+      enableAPI: true,
+      enableEvent: true,
+      host: opt.host,
+      port: opt.port,
+    });
+
+    this.bot.on('socket.connect', () => {
+      logger.info('websocket connected');
+      this.retryInterval = 1000;
+    });
+
+    this.bot.on('socket.close', () => {
+      logger.error('websocket closed');
+      this.reconnect();
+    });
 
-  this.bot.on('socket.error', () => {
-    logger.error('websocket connect error');
-    reconnect();
-  });
+    this.bot.on('socket.error', () => {
+      logger.error('websocket connect error');
+      this.reconnect();
+    });
 
-  this.bot.on('message', (e, context) => {
-    e.cancel();
-    const chat: IChat = {
-      chatType: context.message_type,
-      chatID: 0,
-    };
-    switch (context.message_type) {
-      case ChatType.Private:
-        chat.chatID = context.user_id;
-        break;
-      case ChatType.Group:
-        chat.chatID = context.group_id;
-        break;
-      case ChatType.Discuss:
-        chat.chatID = context.discuss_id;
-        break;
-    }
-    let cmdObj = command(context.raw_message);
-    switch (cmdObj.cmd) {
-      case 'twitter_sub':
-      case 'twitter_subscribe':
-        opt.sub(chat, cmdObj.args);
-        return;
-      case 'twitter_unsub':
-      case 'twitter_unsubscribe':
-        opt.unsub(chat, cmdObj.args);
-        return;
-      case 'ping':
-      case 'twitter':
-        opt.list(chat, cmdObj.args);
-        return;
-      case 'help':
-        return `推特搬运机器人:
+    this.bot.on('message', (e, context) => {
+      e.cancel();
+      const chat: IChat = {
+        chatType: context.message_type,
+        chatID: 0,
+      };
+      switch (context.message_type) {
+        case ChatType.Private:
+          chat.chatID = context.user_id;
+          break;
+        case ChatType.Group:
+          chat.chatID = context.group_id;
+          break;
+        case ChatType.Discuss:
+          chat.chatID = context.discuss_id;
+          break;
+      }
+      let cmdObj = command(context.raw_message);
+      switch (cmdObj.cmd) {
+        case 'twitter_sub':
+        case 'twitter_subscribe':
+          opt.sub(chat, cmdObj.args, this.bot);
+          return;
+        case 'twitter_unsub':
+        case 'twitter_unsubscribe':
+          opt.unsub(chat, cmdObj.args, this.bot);
+          return;
+        case 'ping':
+        case 'twitter':
+          opt.list(chat, cmdObj.args, this.bot);
+          return;
+        case 'help':
+          return `推特搬运机器人:
 /twitter - 查询当前聊天中的订阅
 /twitter_subscribe [链接] - 订阅 Twitter 搬运
 /twitter_unsubscribe [链接] - 退订 Twitter 搬运`;
-    }
-  });
+      }
+    });
 
-  return this;
+  }
 };