安装与配置 - RF-Tar-Railt/Cesloi GitHub Wiki

前提

我们假设在你使用Cesloi前已经阅读了mirai-console-loadermirai-api-http 的 README, 并且通过类似 mirai-console-wrapper / miraiOK / MiraiAndroid 的方式启动了你的 mirai-console , 同时也安装了最新版本的 mirai-api-http 插件.

安装

pip install --upgrade cesloi

你的第一个程序,Helloworld

现在我们需要提前写好 mirai-api-http 的配置, 以便于接下来的说明.

找到你的mcl所在的文件夹,在.config\net.mamoe.mirai-api-http\ 下找到 setting.yml 文件,并按照下面的代码修改:

adapters:
  - http
  - ws

## 是否开启认证流程, 若为 true 则建立连接时需要验证 verifyKey, 建议公网连接时开启
enableVerify: true
verifyKey: cesloi-mirai-api-http-verifyKey # 你可以自己设定, 这里作为示范

## 是否开启单 session 模式, 若为 true,则自动创建 session 绑定 console 中登录的 bot, 
## 开启后,接口中任何 sessionKey 不需要传递参数
## 若 console 中有多个 bot 登录,则行为未定义. 确保 console 中只有一个 bot 登陆时启用
singleMode: false

# 可选,缓存大小,默认4096.缓存过小会导致引用回复与撤回消息失败
cacheSize: 4096

## adapter 的单独配置,键名与 adapters 项配置相同
adapterSettings:
  http:  ## 详情看 http adapter 使用说明 配置
    host: localhost # http 服务监听的地址, 错误的设置会造成 Cesloi 无法与其交互
    port: 8080 # http 服务监听的端口, 错误的设置会造成 Cesloi 无法与其交互
    cors: [*]
  ws:  ## 详情看 websocket adapter 使用说明 配置
    host: localhost # ws 服务监听的地址, 错误的设置会造成 Cesloi 无法与其交互
    port: 8080 # ws 服务监听的端口, 错误的设置会造成 Cesloi 无法与其交互
    reservedSyncId: -1

保存后,将以下代码保存到文件 bot.py 内, 确保该文件位于你的工作区内:

from arclet.cesloi.bot_client import Cesloi
from arclet.cesloi.model.relation import Friend
from arclet.cesloi.communicate_with_mah import BotSession
from arclet.cesloi.alconna import Alconna, Arpamar, AlconnaParser

bot = Cesloi(
    bot_session=BotSession(
        host="http://localhost:8080",  # 填入 http与ws 服务运行的地址
        account=1113332220,  # 你的机器人的 qq 号
        verify_key="cesloi-mirai-api-http-verifyKey"  # 填入 verifyKey
    )
)

@bot.register("FriendMessage",decorators=[AlconnaParser(alconna=Alconna(command="Hello"))])
async def test(app: Cesloi, friend: Friend, result: Arpamar):
    if result.matched:
        await app.send_with(friend, "Hello, World!")
    
bot.start()

然后运行代码

终端:

2021-11-07 23:35:41.851 | INFO     | cesloi.logger:__init__:12 - ------------------------------------------
2021-11-07 23:35:41.852 | INFO     | cesloi.bot_client:start:128 - Cesloi Application Starting...
2021-11-07 23:35:41.853 | DEBUG    | cesloi.bot_client:running_task:90 - Cesloi Network Started.

当出现Cesloi Application Started with X.Xs后, 请尝试与机器人账号发起好友对话, 如果你向机器人发送 Hello , 并且机器人向你发出 Hello, World! 的话, 恭喜你, 你就已经实现了一个Cesloi应用了.

接下来的wiki中会逐一介绍Cesloi的各项功能.