Browse Source

release v0.4.0

Mike L 3 years ago
parent
commit
712d0183d3

+ 1 - 1
.eslintrc.js

@@ -1,7 +1,7 @@
 /* eslint-disable id-blacklist */
 module.exports = {
   ignorePatterns: [
-    '*.js',
+    '*.js', '/bin'
   ],
   env: {
     browser: true,

+ 20 - 2
README.md

@@ -1,8 +1,8 @@
-# (WIP) GoCQHTTP Twitter Bot
+# GoCQHTTP Twitter Bot
 
 修改自:[rikakomoe/cqhttp-twitter-bot](https://github.com/rikakomoe/cqhttp-twitter-bot)
 
-使用 API:[koishijs/koishi](https://github.com/koishijs/koishi)
+使用 API:[koishijs/koishi](https://github.com/koishijs/koishi)(后台使用:[Mrs4s/go-cqhttp](https://github.com/Mrs4s/go-cqhttp))
 
 ## 主要区别
 
@@ -40,6 +40,24 @@
 
 示例文件在 [`config.example.json`](./config.example.json)
 
+## 系统服务
+
+可以使用 [`systemd`](./systemd) 目录里的服务文件设置成自动启动服务
+```
+$ cd
+$ git clone https://mikeslab.dix.asia/gogs/Pirami/gocqhttp-twitter-bot
+$ cd gocqhttp-twitter-bot
+$ ./bootstrap_linux_amd64.sh
+QQ:
+Pass:
+Done!
+$ rsync -a systemd ~/.config/
+$ systemctl --user daemon-reload
+$ systemctl --user enable twitterbot.service
+$ loginctl enable-linger
+```
+注:如果想在本地文件夹保存日志,请取消注释两个服务定义中相应的行,阅读时可以使用 `tail -f`
+
 ## Bug
 
 - 好友消息的图片有可能会失效或直接无法接收(由于更换后台框架,暂时无法进行进一步处理)

+ 33 - 0
bin/playwright-server

@@ -0,0 +1,33 @@
+#!/usr/bin/env node
+const type = (arg =>
+  arg == 'firefox' ? 'firefox' : arg == 'webkit' ? 'webkit' : 'chromium'
+)(process.argv[1]);
+const wsUrl = require('../config.json').playwright_ws_spec_endpoint ||
+  require('../config.default.json').playwright_ws_spec_endpoint;
+const axios = require('axios').default;
+const playwright = require('playwright');
+axios.get('http://localhost:9222').then(res => {
+  if (res.status !== 426) {
+    console.error(Error('port 9222 is in use but not a websocket'));
+    return setTimeout(() => process.exit(1), 30000);
+  }
+  return axios.get(wsUrl).then(jsonres => {
+    const endpoint = jsonres.data[type];
+    if (!endpoint) {
+      console.error(Error('url does not return the required browser type'));
+      return setTimeout(() => process.exit(1), 30000);
+    }
+    if (typeof endpoint !== 'string') throw Error('returned endpoint url is malformed');
+    return playwright[type].connect(endpoint).catch(() => { throw Error('connection error'); });
+  });
+}).catch(err => {
+  if (!axios.isAxiosError(err)) console.log(err);
+  playwright[type].launchServer({args: process.argv.slice(2), port: 9222}).then(svr =>
+    require('http').createServer((req, res) => {
+      const json = req.url === '/playwright-ws.json' &&
+        JSON.stringify(Object.fromEntries([[type, svr.wsEndpoint()]]));
+      if (json) { res.write(json); console.log(json); res.end(); }
+      else res.destroy();
+    }).listen(8080)
+  );
+});

+ 16 - 0
bootstrap_linux_amd64.sh

@@ -0,0 +1,16 @@
+#!/bin/bash
+echo Downloading latest release from GitHub...
+wget -qO- https://api.github.com/repos/Mrs4s/go-cqhttp/releases | grep "browser" | grep v1 | \
+    grep linux_amd64.tar.gz | cut -d'"' -f4 | sort -r | head -n1 | wget -i- -qO- | tar -xz
+echo Installing dependencies from NPM...
+LIBVIPS_REPO="https://mikeslab.dix.asia/gogs/Pirami/sharp-libvips-mozjpeg/raw"
+npm_config_sharp_libvips_binary_host="$LIBVIPS_REPO" npm install
+echo $'\cc' | ./go-cqhttp > /dev/null 2>&1
+gawk -i inplace \
+    '/^[^ ]+:/{s=match($0,/servers:/)?1:0} /- http:/{if(s==1) h=1} /- ws:/{if(s==1) ws=1} \
+    /disabled:/{if(h==1){gsub("false","true"); h=0} if(ws==1){gsub("true","false"); ws=0}} \
+    {print}' ./config.yml
+read -p 'QQ: ' QQ && read -sp 'Pass: ' PSW && sed -i'' \
+    's/\(uin: \)[0-9]*\(.*\)/\1'"$QQ"'\2/; s/\(password: \)'"''"'\(.*\)/\1'"'$PSW'"'\2/' \
+    config.yml && unset QQ PSW && chmod 600 config.yml; echo
+echo Done!

+ 21 - 24
package.json

@@ -1,22 +1,21 @@
 {
-  "name": "@CL-Jeremy/mirai-twitter-bot",
+  "name": "gocqhttp-twitter-bot",
   "version": "0.4.0",
-  "description": "Mirai Twitter Bot",
+  "description": "GoCQHTTP Twitter Bot",
   "main": "./dist/main.js",
   "bin": {
-    "mirai-twitter-bot": "./dist/main.js"
+    "twitter-bot": "./dist/main.js",
+    "twitter-bot-webshot-server": "./bin/playwright-server"
   },
   "repository": {
     "type": "git",
-    "url": "git+https://github.com/CL-Jeremy/mirai-twitter-bot.git"
-  },
-  "publishConfig": {
-    "registry": "https://npm.pkg.github.com/"
+    "url": "git+https://mikeslab.dix.asia/gogs/Pirami/gocqhttp-twitter-bot.git"
   },
   "keywords": [
     "qq",
     "qqbot",
     "mirai",
+    "onebot",
     "twitter"
   ],
   "author": "CL-Jeremy",
@@ -33,35 +32,33 @@
     "callable-instance": "^2.0.0",
     "command-line-usage": "^5.0.5",
     "html-entities": "^1.3.1",
-    "koishi": "^3.10.0",
-    "koishi-adapter-onebot": "^3.0.8",
+    "koishi": "^3.11.0",
+    "koishi-adapter-onebot": "^3.0.9",
     "log4js": "^6.3.0",
-    "playwright": "^1.9.1",
-    "pngjs": "^5.0.0",
+    "playwright": "^1.11.0",
+    "pngjs": "^6.0.0",
     "read-all-stream": "^3.1.0",
     "sha1": "^1.1.1",
-    "sharp": "^0.25.4",
-    "temp": "^0.9.1",
+    "sharp": "^0.26.3",
+    "temp": "^0.9.4",
     "twitter": "^1.7.1",
-    "typescript": "^4.2.3"
+    "typescript": "^4.2.4"
   },
   "devDependencies": {
     "@types/command-line-usage": "^5.0.1",
     "@types/node": "^14.14.22",
-    "@types/pngjs": "^3.4.2",
-    "@types/puppeteer": "^1.5.0",
+    "@types/pngjs": "^6.0.0",
     "@types/redis": "^2.8.6",
-    "@types/sharp": "^0.25.0",
-    "@types/temp": "^0.8.34",
+    "@types/sharp": "^0.26.1",
+    "@types/temp": "^0.9.0",
     "@types/twitter": "^1.7.0",
-    "@typescript-eslint/eslint-plugin": "^4.22.0",
-    "@typescript-eslint/parser": "^4.22.0",
-    "eslint": "^7.25.0",
-    "eslint-plugin-import": "^2.22.1",
-    "eslint-plugin-jsdoc": "^32.3.1",
+    "@typescript-eslint/eslint-plugin": "^4.23.0",
+    "@typescript-eslint/parser": "^4.23.0",
+    "eslint": "^7.26.0",
+    "eslint-plugin-import": "^2.23.2",
+    "eslint-plugin-jsdoc": "^34.6.1",
     "eslint-plugin-prefer-arrow": "^1.2.3",
     "eslint-plugin-react": "^7.23.2",
-    "tslint-config-prettier": "^1.13.0",
     "twitter-d": "^0.4.0"
   }
 }

+ 13 - 0
systemd/user/gocqhttp.service

@@ -0,0 +1,13 @@
+[Unit]
+Description=GoCQHTTP QQ bot kickstarter
+
+[Service]
+WorkingDirectory=%h/gocqhttp-twitter-bot
+ExecStart=%h/gocqhttp-twitter-bot/go-cqhttp
+ExecStartPost=/bin/sleep 15
+#StandardError=append:%h/gocqhttp-twitter-bot/go-cqhttp.log
+Restart=always
+RestartSec=2s
+
+[Install]
+WantedBy=multi-user.target

+ 11 - 0
systemd/user/playwright.service

@@ -0,0 +1,11 @@
+[Unit]
+Description=GoCQHTTP QQ Playwright on-demand server
+
+[Service]
+WorkingDirectory=%h/gocqhttp-twitter-bot
+ExecStart=%h/gocqhttp-twitter-bot/bin/playwright-server
+Restart=always
+RestartSec=2s
+
+[Install]
+WantedBy=multi-user.target

+ 15 - 0
systemd/user/twitterbot.service

@@ -0,0 +1,15 @@
+[Unit]
+Description=GoCQHTTP Twitter bot
+Requires=gocqhttp.service
+Requires=playwright.service
+
+[Service]
+WorkingDirectory=%h/gocqhttp-twitter-bot
+ExecStart=bash -l "%h/gocqhttp-twitter-bot/bin/twitterbot" config.json
+#StandardOutput=append:%h/gocqhttp-twitter-bot/twitterbot.log
+#StandardError=append:%h/gocqhttp-twitter-bot/twitterbot.log
+Restart=always
+RestartSec=2s
+
+[Install]
+WantedBy=default.target