mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-16 19:11:00 +00:00
📝 Add document for message template (#789)
* 📝 Add document for message template * 📝 🎨 Optimize example reading experience for message template * 🚨 auto fix by pre-commit hooks Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@ -120,24 +120,8 @@ class Message(List[TMS], abc.ABC):
|
||||
|
||||
并且提供了拓展的格式化控制符, 可以用适用于该消息类型的 `MessageSegment` 的工厂方法创建消息
|
||||
|
||||
用法:
|
||||
```python
|
||||
>>> Message.template("{} {}").format("hello", "world") # 基础演示
|
||||
Message(MessageSegment(type='text', data={'text': 'hello world'}))
|
||||
>>> Message.template("{} {}").format(MessageSegment.image("file///..."), "world") # 支持消息段等对象
|
||||
Message(MessageSegment(type='image', data={'file': 'file///...'}), MessageSegment(type='text', data={'text': 'world'}))
|
||||
>>> Message.template( # 支持以Message对象作为消息模板
|
||||
... MessageSegment.text('test {event.user_id}') + MessageSegment.face(233) +
|
||||
... MessageSegment.text('test {event.message}')).format(event={'user_id':123456, 'message':'hello world'})
|
||||
Message(MessageSegment(type='text', data={'text': 'test 123456'}),
|
||||
MessageSegment(type='face', data={'face': 233}),
|
||||
MessageSegment(type='text', data={'text': 'test hello world'}))
|
||||
>>> Message.template("{link:image}").format(link='https://...') # 支持拓展格式化控制符
|
||||
Message(MessageSegment(type='image', data={'file': 'https://...'}))
|
||||
```
|
||||
|
||||
参数:
|
||||
format_string: 格式化字符串
|
||||
format_string: 格式化模板
|
||||
|
||||
返回:
|
||||
消息格式化器
|
||||
|
Reference in New Issue
Block a user