异步QQ机器人框架 oicq2-webapid 使用保姆级教程

简介

一个基于 Python3.8+,面向对象,异步的QQ聊天框架

和其他QQ机器人框架相比的特点:

  • 写异步事件很舒适

  • 可以配置 systemd 服务

  • 更新非常方便

  • 由协会大佬 SalimTerryLi 所编写,遇到问题可以直接戳

有关链接

准备

  • 一台联网Linux机器
  • nodejs环境
  • Python3.8+环境
  • 一个QQ小号(机器人)

环境配置

1. 安装 nodejs npm 环境

见另一篇单独的帖子
Linux下node最新稳定版环境安装
或者
Linux下node最新稳定版环境安装标准版(推荐)

2. 安装 Python3.8+

一般来说现在Linux都是自带Python3.8+环境的,安装过程略。

如果你的Linux只支持 python3 而不认 python 命令,可以 sudo apt install python-is-python3

3. 安装 oicq2-webapid

因为我的npm是通过 sudo apt install 安装的,所以我 npm i 的操作也需要sudo,如果你用别的方法安装的nodejs且没有用sudo,这里也不需要sudo

npm i oicq2-webapid -g

注意,我第一遍运行的时候报错 npm ERR! code ERR_SOCKET_TIMEOUT,搜了以下说重新跑一遍就行了,我重跑了一遍果然又行了,真是玄学

测试安装是否成功:

oicq2-webapid -h

下面进行 oicq 的配置工作。

首先创建一个目录来存储配置文件,这里以 $HOME/.config/oicq2-webapid 为例:

mkdir -p $HOME/.config/oicq2-webapid
cd $HOME/.config/oicq2-webapid

然后拉取一个配置模板:

curl -L https://github.com/SalimTerryLi/oicq_webd/raw/main/oicqweb_cfg.json.template -o oicqweb_cfg.json

修改模板中的 qqpassword 两项(换成你自己机器人的),端口号可以自行选择是否修改。

password 项可以为空,即选择扫码登录而非密码登录。

这里墙裂推荐第一次使用扫码登陆:

# -s means scan QR code
oicq2-webapid -s

然后命令行会出来一个二维码,用手机登录机器人的号扫描登录即可。如果第一次扫显示二维码已过期属于正常现象,ctrl c 结束掉重新运行重新扫应该就可以成功登录了。

4. 将 oicq2-webapid 写成 systemd 服务的形式运行

下面进行 oicq-webapid 的 systemd 的配置工作。

编写 $HOME/.config/systemd/user/oicq2-webapid.service 为:

[Unit]
Description=OICQ2 WebAPI daemon
After=network.target
[Service]
Type=simple
WorkingDirectory=$HOME/.config/oicq2-webapid
ExecStart=[whereis oicq2-webapid 的运行结果]
Restart=always
RestartSec=10
[Install]
WantedBy=default.target

启动 systemd 服务:

systemctl --user enable oicq2-webapid
systemctl --user start oicq2-webapid

更新:

npm update -g oicq2-webapid oicq
systemctl --user restart oicq2-webapid

4. 创建项目目录

cd
mkdir pyAsyncBot
cd pyAsyncBot

5. Python venv 有关配置

# 在当前目录下创建一个名为venv的venv
python -m venv venv

激活 venv

source venv/bin/activate

安装 pyasyncbot 库

pip install pyasyncbot

测试安装是否成功:

pyasyncbotd -h

插件的编写与使用

cd 
cd pyAsyncBot
pyasyncbotd -h

输出

usage: pyasyncbotd [-h] [-d PLUGINS_DIR] [-p BOT_PROTOCOL] [-u URL]

pyAsyncBot standalone daemon

optional arguments:
  -h, --help            show this help message and exit
  -d PLUGINS_DIR, --plugins-dir PLUGINS_DIR
                        directory of hot-pluggable plugins
  -p BOT_PROTOCOL, --protocol BOT_PROTOCOL
                        bot protocol
  -u URL, --url URL     bot protocol backend url

可知 -d 参数控制插件目录的选择,如果不加默认是当前目录

基本插件模板

这里有个已经写好的插件:

搜图插件

【番外】pyAsyncbotd 配置成 systemd 服务

编写 $HOME/.config/systemd/user/pyasyncbot.service 为:

[Unit]
Description=pyAsyncBot plugins
After=oicq2-webapid.service

[Service]
Type=simple
WorkingDirectory=[你的插件目录]
ExecStart=[whereis python输出结果中你的venv的目录] [whereis pyasyncbotd输出结果] -u http://127.0.0.1:[你的oicq2-webapid运行的端口(默认8888)]
Restart=always
RestartSec=10

[Install]
WantedBy=default.target

启动 systemd 服务:

systemctl --user enable pyasyncbot.service
systemctl --user start pyasyncbot.service

更新:

pip install -U pyasyncbot
systemctl --user restart pyasyncbot.service