Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dingTalk Robot message support #431

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions notifiers/providers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from . import dingtalk
from . import email
from . import gitter
from . import gmail
Expand All @@ -21,6 +22,7 @@
"simplepush": simplepush.SimplePush,
"slack": slack.Slack,
"email": email.SMTP,
"dingtalk": dingtalk.DingTalk,
"gmail": gmail.Gmail,
"icloud": icloud.iCloud,
"telegram": telegram.Telegram,
Expand Down
93 changes: 93 additions & 0 deletions notifiers/providers/dingtalk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from ..core import Provider
from ..core import Response
from ..utils import requests


class DingTalk(Provider):
"""Send DingTalk notifications"""

base_url = "https://oapi.dingtalk.com/robot/send?access_token={}"
blackstrip marked this conversation as resolved.
Show resolved Hide resolved
site_url = "https://oapi.dingtalk.com/"
path_to_errors = ("message",)
name = "dingtalk"

_required = {"required": ["access_token", "msg_data"]}
_schema = {
"type": "object",
"properties": {
"access_token": {
"type": "string",
"title": "access token to pair a channel to receive notification",
},
"msg_data": {
blackstrip marked this conversation as resolved.
Show resolved Hide resolved
"type": "object",
"title": "dingtalk message body definition",
blackstrip marked this conversation as resolved.
Show resolved Hide resolved
"properties": {
"msgtype": {
blackstrip marked this conversation as resolved.
Show resolved Hide resolved
"type": "string",
"title": "choose a message type, these type supported: text, markdown, link, actionCard",
blackstrip marked this conversation as resolved.
Show resolved Hide resolved
},
"text": {
"type": "object",
"title": "text message",
"properties": {
"content": {
"type": "string",
"title": "text message content",
}
},
"required": ["content"],
},
"markdown": {
"type": "object",
"title": "markdown message",
"properties": {
"title": {"type": "string", "title": "message title"},
"text": {
"type": "string",
"title": "markdown message content",
},
},
"required": ["title", "text"],
},
"link": {
"type": "object",
"title": "link message",
"properties": {
"title": {"type": "string", "title": "message title"},
"text": {"type": "string", "title": "message content"},
"messageUrl": {"type": "string", "title": "link url"},
},
"required": ["title", "text", "messageUrl"],
},
"actionCard": {
"type": "object",
"title": "card message",
"properties": {
"title": {"type": "string", "title": "message title"},
"text": {"type": "string", "title": "message content"},
"singleTitle": {
"type": "string",
"title": "title for card footage button, like 'Read more.' button",
},
"singleURL": {
"type": "string",
"title": "link url when user click card button",
},
},
"required": ["title", "text", "singleTitle", "singleURL"],
},
},
"required": ["msgtype"],
},
},
"additionalProperties": False,
}

def _send_notification(self, data: dict) -> Response:
access_token = data.pop("access_token")
url = self.base_url.format(access_token)
response, errors = requests.post(
blackstrip marked this conversation as resolved.
Show resolved Hide resolved
url, json=data.pop("msg_data"), path_to_errors=self.path_to_errors
)
return self.create_response(data, response, errors)
29 changes: 29 additions & 0 deletions source/providers/dingtalk.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
DingTalk
----------
Send `DingTalk Robot <https://dingtalk.com/>`_ notifications

Minimal example:

.. code-block:: python

>>> from notifiers import get_notifier
>>> dingtalk = get_notifier('dingtalk')
>>> dingtalk.notify(access_token='token', msg_data={'msgtype': 'text', 'text':{'content': 'Hi there!'}})

Full schema:

.. code-block:: yaml

additionalProperties: false
properties:
access_token:
title: your access token
type: string
msg_data:
title: your message definition
type: object
required:
- access_token
- msg_data
type: object

18 changes: 18 additions & 0 deletions tests/providers/test_dingtalk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest

provider = "dingtalk"


class TestDingTalk:
def test_dingtalk_metadata(self, provider):
assert provider.metadata == {
"base_url": "https://oapi.dingtalk.com/robot/send?access_token={}",
blackstrip marked this conversation as resolved.
Show resolved Hide resolved
"name": "dingtalk",
"site_url": "https://oapi.dingtalk.com/",
}

@pytest.mark.online
def test_sanity(self, provider, test_message):
msg_data = {"msgtype": "text", "text": {"content": test_message}}
data = {"access_token": "token", "msg_data": msg_data}
provider.notify(**data, raise_on_errors=True)