Use python3.7 configure a custom robot development nail group (new edition 2020 Raiders)

Reprinted from the original "Liu Yue technical blog" v3u.cn/a_id_132

Recently more serious epidemic, many companies rely on Ali's Office software for remote offices nails, of course, nails this product is really hard to describe people, how to be more difficult to use and difficult to use, people feel really Ali pm are brain-dead will design a product that brain-dead, but go Tucao Tucao, the use have to use, although other nail function is very sad, but the robot still bright spots this function, the function are more geek it can be third-party services aggregated into the nail base, automate synchronization information, for example: by polymerization Github, gitlab source code management and other services to achieve the source code update synchronization; through polymerization Trello, JIRA and other project coordination services to achieve project information synchronization; colleagues, support for custom access Webhook protocol support more possibilities, such as: the operation and maintenance alarm reminder, report the results of automated testing reminders, work and life schedule (Daka go to work, from work to eat, fitness, reading, birthdays, anniversaries ...), etc. reminder, aggregated into a nail through custom robots.

However, some nails on the robot's Raiders online are relatively long, a lot of code are based on python2, for the times, we try to develop python3.7 configure a custom robot nails.

First clear, long nails do not support custom robot created in the mobile phone side, so open your pc or mac end of the nail end client, the chat interface requires the robot group, click on intelligent assistant group

Then click the Add button robot

This time to see a lot of already packaged third-party robot, this time we choose a custom robot

It is worth mentioning that the nails robot based webhook agreement, webhook it is a concept api, api service is one of the micro-use paradigm, also known as reverse api, that is, the front end does not take the initiative to send a request to completely pushed by the rear end , single door will have the opportunity to write an article describes webhook

Add in robot interface, fill out some information robot

Note that, in the security settings of a column, we choose endorsement way to verify, elaborate here, nailing robot security policy, there are three, the first is the use of keywords, that you push in the news It must contain the keywords that you define when you create the robot, if it does not contain a push message, and the second is to use cryptographic signatures, and the third is to define a few ip source, requesting not all these sources will be rejected, the whole or The second and safe and flexible.

Once created, the system will assign you a webhook address that need to save it, the address has a unique accesstoken

ok, then how to use this address to make your robot to push news? View the official document: ding-doc.dingtalk.com/doc#/server...

Surprisingly, I found the document version python2.0, well, ourselves to translate into 3.0

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   #导入依赖库
headers={'Content-Type': 'application/json'}   #定义数据类型
webhook = 'https://oapi.dingtalk.com/robot/send?access_token=f0ca7636f5812fe4815c97a72de9a7cc780c414c258b6c9a631036b1d0f49e3b&timestamp='+timestamp+"&sign="+sign
#定义要发送的数据
#"at": {"atMobiles": "['"+ mobile + "']"
data = {
    "msgtype": "text",
    "text": {"content": '都谁没加到群里来?小心升不了班'},
    "isAtAll": True}
res = requests.post(webhook, data=json.dumps(data), headers=headers)   #发送post请求

print(res.text)
复制代码

Push this effect is the following:

Reprinted from the original "Liu Yue technical blog" v3u.cn/a_id_132

Guess you like

Origin juejin.im/post/5e83e16e51882573bb78e84f