Tsugu Python Frontend
Project description
Tsugu-Lite-Py
Python编写的 Tsugu 前端模块
安装与更新
安装 tsugu 模块
pip install tsugu
使用官方源安装,可以保证你安装的是最新版本。
pip install tsugu --index-url https://pypi.org/simple/
后端需求
- 出图:需要支持
v2 API(2024.2.28日以后的后端版本) - utils:需要支持
v2 API - 用户数据:需要一个启用了数据库的后端,需要支持
v2 API
三个后端可以设置为不同,默认全部设置为公共后端。
更新
pip install tsugu --upgrade
使用官方源更新,可以保证你更新的是最新版本。
pip install tsugu --upgrade --index-url https://pypi.org/simple/
测试与接入
调用 tsugu
tsugu 是一个同步函数,需要 message user_id platform channel_id 四个参数,分别意味着 消息内容 用户id 平台 频道id。
在常用的qqbot中,群号就是 channel_id。
当你使用QQ号作为 user_id 时,platform 可以填写 red。
from tsugu import tsugu_config, tsugu
data = tsugu('查卡 红 ksm 5x', '114514', 'red', '666808414')
返回
[{"type": "string", "string": string},
{"type": "base64", "string": string}, ... ]
当 type 为 string 时,string 为消息内容,可以直接发送到客户端。
当 type 为 base64 时,string 为图片的 base64 编码,需要解码后发送到客户端。
可能会返回多个结果。
如果返回值为 None 代表bot不发送任何消息。
测试例
import base64
from PIL import Image
import io
from tsugu import tsugu_config, tsugu
data = tsugu('查卡 红 ksm 5x', '114514', 'red', '666808414')
if not data:
print("[无反馈数据]")
for item in data:
if item["type"] == "string":
print(f"[文字信息]\n{item['string']}")
elif item["type"] == "base64":
image_data = base64.b64decode(item["string"])
print(f"[图像大小: {len(image_data) / 1024:.2f}KB]")
Image.open(io.BytesIO(image_data)).show()
else:
print(item)
上面的代码使用 pillow 库,将返回值处理成字符串或者图片输出。
更改 tsugu_config 配置
查看文档
from tsugu import tsugu_config
tsugu_config.config_docs()
将会在控制台输出文档。
Config 属性文档:
backend (str)
默认值: "http://tsugubot.com:8080"
描述: 应用程序的后端服务地址,需要 v2 API 。
...
例如,你可以更改的后端地址。
from tsugu import tsugu_config
tsugu_config.backend = "http://127.0.0.0.1:3000"
你可以设置代理。
from tsugu import tsugu_config
tsugu_config.use_proxies = True
tsugu_config.proxies = {"http": "http://127.0.0.1:1145", "https": "http://127.0.0.1:1919"}
你可以添加关闭抽卡模拟的群号。
from tsugu import tsugu_config
tsugu_config.ban_gacha_simulate_group_data = ["114514", "1919810"]
你也可以使用 add_command_name 和 remove_command_name 方法添加或删除命令名以添加别名或关闭命令。
from tsugu import tsugu_config
tsugu_config.add_command_name(api="gacha", command_name="抽卡")
机器人接入例
from core import on
from tsugu import tsugu_config, tsugu
# 监听每一条文字消息
@on.message_created
def tsugulp(event):
# 设置代理
tsugu_config.use_proxies = True
# 添加关闭抽卡模拟的群号
tsugu_config.add_ban_gacha_simulate_group_data('114514')
tsugu_config.add_ban_gacha_simulate_group_data('1919810')
# 调用 tsugu
rpl = tsugu(event.message.content, event.user.id, 'red', event.message.id)
# 如果返回值为None,不发送任何消息
if not rpl:
return
def handle_string(item):
return item['string']
def handle_base64(item):
base64_data = item['string']
return f'<img src="data:image/png;base64,{base64_data}"/>'
handlers = {
'string': handle_string,
'base64': handle_base64,
}
# 使用列表推导式,将返回值处理成字符串
modified_results = [handlers[item['type']](item) for item in rpl if item['type'] in handlers]
result_string = ''.join(modified_results)
# 发送消息
event.message_create(result_string)
这是随机挑选的一个 satori 协议机器人框架的 tsugu 接入实现,对于绝大部分 Python 机器人框架,接入方法大同小异。唯一可能需要处理的可能就是异步函数的调用问题。
为什么不再提供登录端?
黑暗时代...
因为这不是一个好的时代,当下一个好的时代到来时,我们会再次提供登录端。
但部署方式仍然是有不少的,如果有部署期望,可以看下面的 ▶️客服ano酱指导 进群聊聊天,我们会尽力帮助你。
另外, C# + Lagrange 组合的登录端正由 棱镜 开发中,敬请期待。
基于本项目的 NoneBot2 插件也由 zhaomoaniu 开发中,敬请期待。
客服ano酱指导
注意,如果你不知道什么是BanGDream,请不要随意加群
本群还是欢迎加群的(
BanGDreamBot开发聊天群
温馨的聊天环境~
未来还会继续完善以及同步更新的,感谢大家的支持!
也感谢山本和大家的付出
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tsugu-0.0.2.tar.gz.
File metadata
- Download URL: tsugu-0.0.2.tar.gz
- Upload date:
- Size: 13.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
952d33b4946ce49a56f61e83b288559757dbfc8e7d7f0eedbac13d39921241f0
|
|
| MD5 |
29225732e77695bfbb086ab4d85500c2
|
|
| BLAKE2b-256 |
0949b1c3ef9e57aaf57999cc7862b17e1269f36a99725ff67714d7ebca882de1
|
File details
Details for the file tsugu-0.0.2-py3-none-any.whl.
File metadata
- Download URL: tsugu-0.0.2-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8712b0ff7b60a6b4f412933a5bbb15fc10206c9888e9730b97ca3aae9bec084f
|
|
| MD5 |
30eb7eb3c564672c7f72232ae0bf9050
|
|
| BLAKE2b-256 |
ca43617e80100b42b606f775ea919e5fcfdac67c0bfd3c9529f9fe994c25d890
|