Python SDK for Milky Protocol
Project description
Milky Python SDK
Milky SDK 是用于与 Milky 协议端通信的 Python SDK,支持同步/异步两种模式和装饰器风格的事件系统。
安装依赖
pip install milky-python-sdk
快速开始
方式一:MilkyBot 框架(推荐)
from milky import MilkyBot
bot = MilkyBot("http://localhost:3010", "token")
@bot.on_mention()
async def handle(event):
await bot.reply(event, "你好!")
@bot.on_command("help")
async def help_cmd(event, args):
await bot.reply(event, "帮助信息")
bot.run()
方式二:异步客户端
from milky import AsyncMilkyClient
import asyncio
async def main():
async with AsyncMilkyClient("http://localhost:3010", "token") as client:
info = await client.get_login_info()
print(info.nickname)
asyncio.run(main())
方式三:同步客户端
from milky import MilkyClient
client = MilkyClient("http://localhost:3010", "token")
info = client.get_login_info()
装饰器事件系统
| 装饰器 | 说明 |
|---|---|
@bot.on(EventType.*) |
监听指定事件类型 |
@bot.on_message() |
监听所有消息 |
@bot.on_message("group") |
只监听群消息 |
@bot.on_mention() |
只在 bot 被 @ 时触发 |
@bot.on_command("cmd") |
监听 /cmd 命令 |
消息段
发送文本
from milky.models import OutgoingTextSegment, TextSegmentData
message = [OutgoingTextSegment(data=TextSegmentData(text="Hello!"))]
await client.send_group_message(group_id, message)
发送 @ + 文本
from milky.models import OutgoingMentionSegment, MentionSegmentData, OutgoingTextSegment, TextSegmentData
message = [
OutgoingMentionSegment(data=MentionSegmentData(user_id=123)),
OutgoingTextSegment(data=TextSegmentData(text=" 你好"))
]
Bot.reply 快捷方法
@bot.on_mention()
async def handle(event):
await bot.reply(event, "你好!") # 自动 @ 发送者
await bot.reply(event, "消息", at_sender=False) # 不 @
事件类型
from milky import EventType
EventType.MESSAGE_RECEIVE # 收到消息
EventType.MESSAGE_RECALL # 消息撤回
EventType.FRIEND_REQUEST # 好友请求
EventType.GROUP_MEMBER_INCREASE # 群成员增加
EventType.GROUP_NUDGE # 群戳一戳
常用 API
# 系统
await client.get_login_info()
await client.get_friend_list()
await client.get_group_list()
# 消息
await client.send_group_message(group_id, message)
await client.send_private_message(user_id, message)
await client.recall_group_message(group_id, message_seq)
# 群管理
await client.set_group_member_mute(group_id, user_id, 60)
await client.kick_group_member(group_id, user_id)
错误处理
from milky.async_client import MilkyError, MilkyHttpError
try:
await client.send_group_message(group_id, message)
except MilkyHttpError as e:
print(f"HTTP {e.status_code}: {e.message}")
except MilkyError as e:
print(f"API [{e.retcode}]: {e.message}")
完整示例
from milky import MilkyBot
bot = MilkyBot("http://localhost:3010", "token")
@bot.on_mention()
async def reply_mention(event):
await bot.reply(event, "你好!")
@bot.on_command("echo")
async def echo(event, args):
if args:
await bot.reply(event, args, at_sender=False)
@bot.on_message("group")
async def log_group(event):
data = event["data"]
print(f"群 {data['peer_id']}: {data['segments']}")
bot.run()
Project details
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 milky_python_sdk-0.2.0.tar.gz.
File metadata
- Download URL: milky_python_sdk-0.2.0.tar.gz
- Upload date:
- Size: 198.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
76630776e0c3fb4b9eb2a1b9f466e2988212d45ee0664e99408bcd6d36ac08c7
|
|
| MD5 |
fa402ccd3ab0de214409da629b8902bc
|
|
| BLAKE2b-256 |
63685c69be45ec4fbc3938168343e5b06c0cb0414f105afac6514a95a7eaf00b
|
Provenance
The following attestation bundles were made for milky_python_sdk-0.2.0.tar.gz:
Publisher:
publish.yml on notnotype/milky-python-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
milky_python_sdk-0.2.0.tar.gz -
Subject digest:
76630776e0c3fb4b9eb2a1b9f466e2988212d45ee0664e99408bcd6d36ac08c7 - Sigstore transparency entry: 798576878
- Sigstore integration time:
-
Permalink:
notnotype/milky-python-sdk@9cf848d01a24e3a9886ad18ea00b71b39e1c5189 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/notnotype
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9cf848d01a24e3a9886ad18ea00b71b39e1c5189 -
Trigger Event:
release
-
Statement type:
File details
Details for the file milky_python_sdk-0.2.0-py3-none-any.whl.
File metadata
- Download URL: milky_python_sdk-0.2.0-py3-none-any.whl
- Upload date:
- Size: 19.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ccf9e2c96e2db4f1a89529f20b73a0008a4dd3ca1df6750a3508584c88407ff8
|
|
| MD5 |
081c2e66d42ea798beecdc4f3de3ad1d
|
|
| BLAKE2b-256 |
d7362de61797e70ac539743753c6b8e6555ca7f64e5fe9c983a1199d637dedca
|
Provenance
The following attestation bundles were made for milky_python_sdk-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on notnotype/milky-python-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
milky_python_sdk-0.2.0-py3-none-any.whl -
Subject digest:
ccf9e2c96e2db4f1a89529f20b73a0008a4dd3ca1df6750a3508584c88407ff8 - Sigstore transparency entry: 798576883
- Sigstore integration time:
-
Permalink:
notnotype/milky-python-sdk@9cf848d01a24e3a9886ad18ea00b71b39e1c5189 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/notnotype
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9cf848d01a24e3a9886ad18ea00b71b39e1c5189 -
Trigger Event:
release
-
Statement type: