Nail nail as the most popular office software , I spend most of my time with him at work . Today I would like to share with you how to use Python Send a message to nail , Finally, the daily sales daily report will be automatically sent to the designated group every day .
First, open the group settings , Click smart group assistant .
Select add custom robot
Then follow the prompts to add the following information , It is recommended to select the first two items of security settings , The key added here needs to be saved , We'll use that later .
Click Finish to generate a Webhook
Address , This address and key should not be published casually , There will be security risks if it is placed on an external website .
We have obtained the key and Webhook
, First, parse the key to get the timestamp (timestamp) And signature value (sign), The code is as follows .
import time
import hmac
import hashlib
import base64
import urllib.parse
timestamp = str(round(time.time() * 1000))
secret = ' Fill in your key '
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_plus(base64.b64encode(hmac_code))
print(timestamp)
print(sign)
timestamp,sign As two key parameters , Match them with Webhook
Splicing .
https://oapi.dingtalk.com/robot/send?access_token=XXXXXX×tamp=XXX&sign=XXX
access_token The value of the parameter has been included in... When creating the robot Webhook
in , Just pass in timestamp And sign You can get the complete Webhook
.
There are many types of nailing messages , You can select the type of message you want to send .
Official documents :https://open.dingtalk.com/document/robots/custom-robot-access
I'm using Markdown Format , Currently, only basic Markdown grammar , At first I thought it was modesty , After the test, it is found that it does not support ,HTML The syntax also supports only a small part .
title
# First level title
## Secondary title
### Three level title
#### The fourth level title
##### Five level titles
###### Six level title
quote
> A man who stands for nothing will fall for anything.
The words are bold 、 Italics
**bold**
*italic*
link
[this is a link](http://name.com)
picture
![](http://name.com/pic.jpg)
Unordered list
- item1
- item2
Ordered list
1. item1
2. item2
The overall code is not complicated , The code is as follows .
import time
import hmac
import hashlib
import base64
import urllib.parse
import datetime
import json
timestamp = str(round(time.time() * 1000))
secret = ' Fill in your key '
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_plus(base64.b64encode(hmac_code))
print(timestamp)
print(sign)
url = f'https://oapi.dingtalk.com/robot/send?access_token=xxxx×tamp={
timestamp}&sign={
sign}'
def send_request(url, datas):
header = {
"Content-Type": "application/json",
"Charset": "UTF-8"
}
sendData = json.dumps(datas)
sendDatas = sendData.encode("utf-8")
request = urllib.request.Request(url=url, data=sendDatas, headers=header)
opener = urllib.request.urlopen(request)
# Output response results
print(opener.read())
def get_string():
''' What you want to send , Notice the message format , If you choose markdown, Expected to contain... In string Markdown Content of format example : "<font color=#00ffff> Yesterday's sales :XXX</font> \n <font color=#00ffff> Yesterday's sales :XXX</font>" '''
return "- test 1 - test 2"
def main():
# isAtAll: whether @ All the people , It is recommended that you do not choose..., if not necessary , Otherwise, the test will be awkward
dict = {
"msgtype": "markdown",
"markdown": {
"title": " Sales daily ",
"text": ""
},
"at": {
"isAtAll": False
}
}
# Write the contents of the document into the request format
dict["markdown"]["text"] = get_string()
send_request(url, dict)
main()
If there is anything unclear , You can directly send a private letter or refer to official documents .
https://open.dingtalk.com/document/robots/robot-overview
Scrapy Is applicable to Python
解決報錯UnicodeDecodeError: ‘utf-8