🐛 fix ding adapter

1. fix send `at_sender`
2. fix event parse error
3. `MessageSegment` add `atDingtalkIds`/`code`
This commit is contained in:
Artin 2021-01-29 14:31:36 +08:00
parent 06837d4a56
commit 27e06dd4e8
4 changed files with 72 additions and 23 deletions

View File

@ -216,7 +216,9 @@ class Bot(BaseBot):
params.update(kwargs) params.update(kwargs)
if at_sender and event.conversationType != ConversationType.private: if at_sender and event.conversationType != ConversationType.private:
params["message"] = f"@{event.senderNick} " + msg params[
"message"] = f"@{event.senderId} " + msg + MessageSegment.atDingtalkIds(
event.senderId)
else: else:
params["message"] = msg params["message"] = msg

View File

@ -77,7 +77,7 @@ class MessageEvent(Event):
conversationId: str conversationId: str
senderId: str senderId: str
senderNick: str senderNick: str
senderCorpId: str senderCorpId: Optional[str]
sessionWebhook: str sessionWebhook: str
sessionWebhookExpiredTime: int sessionWebhookExpiredTime: int
isAdmin: bool isAdmin: bool

View File

@ -44,6 +44,17 @@ class MessageSegment(BaseMessageSegment):
"""@指定手机号人员""" """@指定手机号人员"""
return MessageSegment("at", {"atMobiles": list(mobileNumber)}) return MessageSegment("at", {"atMobiles": list(mobileNumber)})
@staticmethod
def atDingtalkIds(*dingtalkIds: str) -> "MessageSegment":
"""@指定 id@ 默认会在消息段末尾。
所以你可以在消息中使用 @{senderId} 占位发送出去之后 @ 就会出现在占位的位置
```python
message = MessageSegment.text(f"@{event.senderId},你好")
message += MessageSegment.atDingtalkIds(event.senderId)
```
"""
return MessageSegment("at", {"atDingtalkIds": list(dingtalkIds)})
@staticmethod @staticmethod
def text(text: str) -> "MessageSegment": def text(text: str) -> "MessageSegment":
"""发送 ``text`` 类型消息""" """发送 ``text`` 类型消息"""
@ -59,6 +70,16 @@ class MessageSegment(BaseMessageSegment):
""""标记 text 文本的 extension 属性,需要与 text 消息段相加。""" """"标记 text 文本的 extension 属性,需要与 text 消息段相加。"""
return MessageSegment("extension", dict_) return MessageSegment("extension", dict_)
@staticmethod
def code(code_language: str, code: str) -> "Message":
""""发送 code 消息段"""
message = MessageSegment.text(code)
message += MessageSegment.extension({
"text_type": "code_snippet",
"code_language": code_language
})
return message
@staticmethod @staticmethod
def markdown(title: str, text: str) -> "MessageSegment": def markdown(title: str, text: str) -> "MessageSegment":
"""发送 ``markdown`` 类型消息""" """发送 ``markdown`` 类型消息"""

View File

@ -1,3 +1,4 @@
from nonebot.adapters.ding.event import GroupMessageEvent, PrivateMessageEvent
from nonebot.rule import to_me from nonebot.rule import to_me
from nonebot.plugin import on_command from nonebot.plugin import on_command
from nonebot.adapters.ding import Bot as DingBot, MessageSegment, MessageEvent from nonebot.adapters.ding import Bot as DingBot, MessageSegment, MessageEvent
@ -6,7 +7,7 @@ markdown = on_command("markdown", to_me())
@markdown.handle() @markdown.handle()
async def test_handler(bot: DingBot): async def markdown_handler(bot: DingBot):
message = MessageSegment.markdown( message = MessageSegment.markdown(
"Hello, This is NoneBot", "Hello, This is NoneBot",
"#### NoneBot \n> Nonebot 是一款高性能的 Python 机器人框架\n> ![screenshot](https://v2.nonebot.dev/logo.png)\n> [GitHub 仓库地址](https://github.com/nonebot/nonebot2) \n" "#### NoneBot \n> Nonebot 是一款高性能的 Python 机器人框架\n> ![screenshot](https://v2.nonebot.dev/logo.png)\n> [GitHub 仓库地址](https://github.com/nonebot/nonebot2) \n"
@ -18,7 +19,7 @@ actionCardSingleBtn = on_command("actionCardSingleBtn", to_me())
@actionCardSingleBtn.handle() @actionCardSingleBtn.handle()
async def test_handler(bot: DingBot): async def actionCardSingleBtn_handler(bot: DingBot):
message = MessageSegment.actionCardSingleBtn( message = MessageSegment.actionCardSingleBtn(
title="打造一间咖啡厅", title="打造一间咖啡厅",
text= text=
@ -32,7 +33,7 @@ actionCard = on_command("actionCard", to_me())
@actionCard.handle() @actionCard.handle()
async def test_handler(bot: DingBot): async def actionCard_handler(bot: DingBot):
message = MessageSegment.raw({ message = MessageSegment.raw({
"msgtype": "actionCard", "msgtype": "actionCard",
"actionCard": { "actionCard": {
@ -53,14 +54,14 @@ async def test_handler(bot: DingBot):
}] }]
} }
}) })
await actionCard.finish(message) await actionCard.finish(message, at_sender=True)
feedCard = on_command("feedCard", to_me()) feedCard = on_command("feedCard", to_me())
@feedCard.handle() @feedCard.handle()
async def test_handler(bot: DingBot): async def feedCard_handler(bot: DingBot):
message = MessageSegment.raw({ message = MessageSegment.raw({
"msgtype": "feedCard", "msgtype": "feedCard",
"feedCard": { "feedCard": {
@ -88,9 +89,11 @@ atme = on_command("atme", to_me())
@atme.handle() @atme.handle()
async def test_handler(bot: DingBot, event: MessageEvent): async def atme_handler(bot: DingBot, event: MessageEvent):
message = f"@{event.senderNick} at you" + MessageSegment.atMobiles( message = f"@{event.senderId} manually at you" + MessageSegment.atDingtalkIds(
"13800000001") event.senderId)
await atme.send("matcher send auto at you", at_sender=True)
await bot.send(event, "bot send auto at you", at_sender=True)
await atme.finish(message) await atme.finish(message)
@ -98,7 +101,7 @@ image = on_command("image", to_me())
@image.handle() @image.handle()
async def test_handler(bot: DingBot, event: MessageEvent): async def image_handler(bot: DingBot, event: MessageEvent):
message = MessageSegment.image( message = MessageSegment.image(
"https://static-aliyun-doc.oss-accelerate.aliyuncs.com/assets/img/zh-CN/0634199951/p158167.png" "https://static-aliyun-doc.oss-accelerate.aliyuncs.com/assets/img/zh-CN/0634199951/p158167.png"
) )
@ -109,7 +112,7 @@ textAdd = on_command("t", to_me())
@textAdd.handle() @textAdd.handle()
async def test_handler(bot: DingBot, event: MessageEvent): async def textAdd_handler(bot: DingBot, event: MessageEvent):
message = "第一段消息\n" + MessageSegment.text("asdawefaefa\n") message = "第一段消息\n" + MessageSegment.text("asdawefaefa\n")
await textAdd.send(message) await textAdd.send(message)
@ -129,17 +132,8 @@ code = on_command("code", to_me())
@code.handle() @code.handle()
async def test_handler(bot: DingBot, event: MessageEvent): async def code_handler(bot: DingBot, event: MessageEvent):
raw = MessageSegment.raw({ raw = MessageSegment.code("Python", 'print("hello world")')
"msgtype": "text",
"text": {
"content": 'print("hello world")'
},
"extension": {
"text_type": "code_snippet",
"code_language": "Python",
}
})
await code.send(raw) await code.send(raw)
message = MessageSegment.text("""using System; message = MessageSegment.text("""using System;
@ -158,3 +152,35 @@ namespace HelloWorld
"code_language": "C#" "code_language": "C#"
}) })
await code.finish(message) await code.finish(message)
test_message = on_command("test_message", to_me())
@test_message.handle()
async def test_message_handler1(bot: DingBot, event: PrivateMessageEvent):
await test_message.finish("PrivateMessageEvent")
@test_message.handle()
async def test_message_handler2(bot: DingBot, event: GroupMessageEvent):
await test_message.finish("GroupMessageEvent")
hello = on_command("hello", to_me())
@hello.handle()
async def hello_handler(bot: DingBot, event: MessageEvent):
message = MessageSegment.raw({
"msgtype": "text",
"text": {
"content": 'hello '
},
})
message += MessageSegment.atDingtalkIds(event.senderId)
await hello.send(message)
message = MessageSegment.text(f"@{event.senderId},你好")
message += MessageSegment.atDingtalkIds(event.senderId)
await hello.finish(message)