Creating a Custom DingTalk Robot with Python 3.7 and Webhook
This guide explains how to create a custom DingTalk robot using Python 3.7, covering webhook configuration, security signing, step‑by‑step setup in the PC client, and a complete code example for sending text messages.
Remote work has become common, and many companies use Alibaba's DingTalk for management. While DingTalk has many shortcomings, its robot feature allows third‑party services to be aggregated into group chats for automated information synchronization.
Typical use cases include aggregating source‑code updates from GitHub or GitLab, project information from Trello or JIRA, and custom webhook integrations such as operation alerts, test reports, or personal schedules.
Because most existing tutorials target Python 2, this article demonstrates how to develop a custom DingTalk robot with Python 3.7.
First, note that custom robots can no longer be created on mobile; you must use the PC or Mac client. In the group chat, click "Smart Group Assistant" → "Add Robot" and choose the "Custom Robot" option.
In the robot creation interface, fill in the robot information. For security, select the "Signature" method, which is more flexible and secure than keyword or IP restrictions.
After creation, DingTalk provides a webhook URL containing a unique access token. This URL will be used to push messages.
The following Python code shows how to generate the required signature and send a simple text message via the webhook.
import time
import hmac
import hashlib
import base64
import urllib.parse
timestamp = str(round(time.time() * 1000))
secret = 'SEC90485937c351bfaed41fea8eda5f1e155bbf22842d5f9d6871999e05822fd894'
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote(base64.b64encode(hmac_code))
# print(timestamp)
# print(sign)
import requests, json # import dependencies
headers = {'Content-Type': 'application/json'} # define content type
webhook = 'https://oapi.dingtalk.com/robot/send?access_token=替换成你的accesstoken值×tamp=' + timestamp + '&sign=' + sign
# define payload
data = {
"msgtype": "text",
"text": {"content": '都谁没加到群里来?小心升不了班'},
}
res = requests.post(webhook, data=json.dumps(data), headers=headers) # send POST request
print(res.text)The result appears as a simple text message in the group. Besides plain text, DingTalk robots can send images, links, markdown, ActionCard, FeedCard, and other message types as documented.
With this basic robot in place, you can extend its functionality further to suit various automation scenarios.
Original article: v3u.cn/a_id_132
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.