lapis

lapis

理解以真实为本,但真实本身不会自动呈现

Second Generation KiviBot: Lightweight, Efficient, Cross-platform QQbot

KiviBot: Lightweight, Efficient, Cross-platform QQ Bot#

date: January 27, 2023
slug: 18
status: Published
tags: Technology
type: Post

Bot Introduction#

KiviBot is a lightweight, elegant, cross-platform, developer-friendly, and "just works" QQ bot framework written in TypeScript.

The framework provides comprehensive state monitoring, plugin management (supports hot updates), main and sub-administrator mechanisms, message notifications, request handling capabilities, and a friendly scaffolding, ready to use. The framework is completely open source, highly extensible, with simple plugin development, and uses the oicq v2 as the core underlying protocol, with numerous APIs and powerful functionality. Additionally, the framework is driven by Node.js, benefiting from Node.js and its efficient v8 engine, KiviBot has impressive performance.

Installation#

1. Using Linux#

i. Install Node.js#

Install using the corresponding package manager or from the official website (I chose Baota). It is recommended to use version 18, with a minimum requirement of version 14. Make sure to configure the environment variables (including NODE_HOME) to ensure that the command line can access the node and npm commands.

If you still can't start the bot after configuring the environment variables, running the following code may be effective:
echo -e "export PATH=(npmprefix−g)/bin:(npm prefix -g)/bin:(npmprefix−g)/bin:PATH" >> ~/.bashrc && source ~/.bashrc
(I'm probably just bad at it)

ii. Install KiviBot CLI globally#

CLI (Command Line Interface) is an abbreviation for command-line interface, which refers to the way of operating a computer system by entering text commands in the terminal or command line. In the CLI, users enter commands via the keyboard, and the computer performs corresponding operations based on the commands. Compared to the graphical user interface (GUI), CLI is more flexible and powerful, and is the preferred method for programmers and system administrators.

npmi -g kivibot@latest

Of course, you can also choose not to install it globally and use npm kivi to execute CLI commands.

iii. Generate the configuration file using CLI#

# Create an empty directory as the framework directory and switch to this directory
mkdir ~/bot && cd ~/bot
# Generate the configuration file in the current directory using CLI
kivi init

iv. Start KiviBot#

kivistart

v. Deploy in the background (optional)#

kivi deploy
# CLI will use pm2 to deploy the framework process in the background

2. Using Windows#

Deployment method is the same as above.

3. Using KiviBot on Android#

i. Download and install Termux#

This step should be self-explanatory, right?

ii. Configure Termux to use mirror sources in China#

sed -i 's@^\(deb.*stable main\)$@#\1\ndeb https://mirrors.aliyun.com/termux/termux-packages-24 stable main@' $PREFIX/etc/apt/sources.list
sed -i 's@^\(deb.*games stable\)$@#\1\ndeb https://mirrors.aliyun.com/termux/game-packages-24 games stable@' $PREFIX/etc/apt/sources.list.d/game.list
sed -i 's@^\(deb.*science stable\)$@#\1\ndeb https://mirrors.aliyun.com/termux/science-packages-24 science stable@' $PREFIX/etc/apt/sources.list.d/science.list
pkgupdate

iii. Install Node.js#

pkg install nodejs

The remaining steps are the same as the installation on Linux#

In fact, all installation methods simulate/flash a Linux environment to run Node.js and then run the framework. Therefore, theoretically, you can even run KiviBot on your electronic watch, and its performance might be sufficient.

Configuration#

After completing the installation steps, you should receive a notification that the bot is online. Now, you can send the command /help to the bot to view the list of supported commands.

Message Commands#

KiviBot does not have a UI interface, and operations such as plugin management, administrative changes, and status monitoring are all performed through QQ message commands. Send the command /help to the bot to view all framework commands. These commands are only available to administrators and can be used in private chats or group chats. The list of all message commands in the framework:

https://blog-1302893975.cos.ap-shanghai.myqcloud.com/pic/202301272325564.png

KiviBot CLI Commands#

KiviBot CLI is the scaffolding or command-line tool of the framework, which is used to start the framework with the command kivi. You can use it to assist in generating configuration files, installing plugins and dependencies, and starting the framework. Enter the command kivi to view the detailed usage of the CLI. The following table lists all the commands of the CLI and their descriptions.

https://blog-1302893975.cos.ap-shanghai.myqcloud.com/pic/20230127232727.png

Configuration File#

The kivi.json file in the framework directory is the configuration file for KiviBot, which contains a series of framework configurations. You can manually modify it and it will take effect after restarting.

kivi start

kivi.json
app.js
{
// Bot QQ account, generated by CLI "account": 114514,
// Login method, generated by CLI, options: password (password login) and qrcode (QR code login) "login_mode": "password",
// Device lock verification method, when selecting password login, generated by CLI, if selecting QR code, it defaults to verification code login// Options: sms (SMS verification code verification) and qrcode (QR code verification) "device_mode": "sms",
// Framework log message display mode, default is short (concise mode, ignores CQ codes, consumes fewer resources)// Can also be set to detail (detailed mode, displays the specific content of CQ codes, helpful for debugging plugins) "message_mode": "short",
// Bot account password, when selecting password login, generated by CLI, if selecting QR code, it is empty by default"password": "",
// Framework log output level, default is info, set to off to disable log output, can be set to debug for debugging plugins"log_level": "info",
// Administrator list, generated by CLI, the first one is the main administrator (required), the rest are sub-administrators (optional) "admins": [1141284758],
// Enabled plugin list, including npm plugins (starting with kivibot-plugin-) and plugins in the plugins directory// npm plugins (starting with kivibot-plugin-) ignore the kivibot-plugin- prefix"plugins": ["demo"],
// Bot event notifications"notice": {
// Whether to enable, can be switched in real-time using the message command /config notice on and /config notice off"enable":true,
// Friend list related configuration"friend": {
// Friend requests"request": {
// Whether to enable friend request notifications, default is enabled (true), set to false to disable"enable":true,

// Handling friend requests, options: ignore (ignore, default), accept (accept), refuse (refuse)"action": "ignore"
  },

// New friend notifications, default is enabled (true), set to false to disable"increase":true,

// Friend removal notifications, default is enabled (true), set to false to disable"decrease":true,

// Friend private chat message notifications, default is disabled (false), set to true to enable"message":false},

// Group chat related configuration"group": {
// Inviting the bot to a group"request": {
// Whether to enable notifications, default is enabled (true), set to false to disable"enable":true,

// Handling group invitations, options: ignore (ignore, default), accept (accept), refuse (refuse)"action": "ignore"
  },

// New group chat notifications, default is enabled (true), set to false to disable"increase":true,

// Group chat removal notifications, default is enabled (true), set to false to disable"decrease":true,

// Bot being banned notifications, default is enabled (true), set to false to disable"ban":true,

// Group administrator change notifications, default is enabled (true), set to false to disable"admin":true,

// Group transfer notifications, default is enabled (true), set to false to disable"transfer":true}

},
// oicq protocol configuration, refer to the oicq documentation, the data directory is limited to data/oicq and cannot be manually modified"oicq_config": {
// Login protocol: 1 for Android phone, 2 for Android tablet, 3 for Android watch, 4 for MacOS, 5 for iPad"platform": 5,
// oicq log level, oicq logs will be output to the logs folder in the framework directory"log_level": "info",

// In group chats, whether to ignore messages from the bot itself, default is to ignore, set to false to not ignore"ignore_self":true,

// Whether to automatically select the optimal server, default is enabled, set to false to disable// After disabling, it will always use msfwifi.3g.qq.com:8080 for connection"auto_server":true,

// Whether to cache the group member list, default is to cache for improved efficiency, set to false to significantly reduce memory usage"cache_group_member":true,

// Reconnection interval when disconnected due to network issues, default is 5 (seconds), set to 0 to disable automatic reconnection"reconn_interval": 5,

// Whether to attempt to send in fragments when being restricted by the anti-spam system, default is true, set to false to not send in fragments"resend":true,

// Specify the ffmpeg path"ffmpeg_path": "ffmpeg",

// Specify the ffprobe path"ffprobe_path": "ffprobe"

}
}

Frequently Asked Questions#

There may be many frequently asked questions, but I'm too lazy to look at them. Just make do with it.

Plugins#

Plugins in KiviBot can be divided into the following two types:

  • npm plugins
  • local plugins Whether it is an npm plugin or a local plugin, they should all be node modules that comply with the CommonJS specification. The plugin configuration file is uniformly placed in the corresponding directory under the framework directory/data/plugins/<name> (usually the config.json file). Unless otherwise specified, please modify the configuration in this location, modifying the source code directly will be overwritten during updates and the configuration will be invalid.

Node modules, that is: the module directory contains

index.js
package.json
package.json
main

npm Plugins#

Npm plugins refer to plugins that can be downloaded and installed directly from npm using the /plugin add message command (or installed via npm or kivi commands in the terminal). Typically, these plugins follow the naming convention of starting with kivibot-plugin-. Official plugins are published in this format on npm to improve user experience. Npm plugins need to be installed before they can be used. After installation, they will be stored in the node_modules directory under the framework directory, where you can find the source code of the plugins.

When installing or enabling plugins using the message command, there is no need to include the kivibot-plugin- prefix.

Local Plugins#

As the name suggests, local plugins refer to plugins that have not been published to npm (such as plugins developed by users themselves or some private plugins) and do not need to be downloaded. They are stored in the plugins directory under the framework directory. Like npm plugins, each plugin should be a node module that complies with the CommonJS specification.

User Experience#

This bot is easy to configure, has many plugins, and is developer-friendly, but unfortunately, the author has disappeared. Although its conversation feature has more bugs than the first bot and does not support personality training settings, it has many fancy features (which are not very useful to me).

Let's wait for the ChatGPT API to be released. The current version 3.0 is too rigid.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.