Python implementation of pin subscription message

Catalog

Nail setting robot

Push pin message in Python script

Message type

text type

link type

markdown type

Overall jump ActionCard type

Independent jump ActionCard type

FeedCard type

Nail setting robot

First of all, set the nail robot in the nail

Group settings - > intelligent group assistant - > Add robot - > Custom

Add it and get a Webhook API address

Push pin message in Python script

Nail official gives the document of robot interface: https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq

But this document only implements JAVA and PHP examples. The following is an example of a python script.

# -*- coding: utf-8 -*-
"""
Created on Mon Jan 13 11:18:59 2020
python3
@author: Xie Gong Zi
"""
import json
import requests

def send_msg(url,data):
    headers = {'Content-Type': 'application/json;charset=utf-8'}
    r = requests.post(url,data = json.dumps(data),headers=headers)
    return r.text
if __name__ == '__main__':
   data = {
            "msgtype": "text", 
            "text": {
                "content": "hello,word!test"
            }, 
        }
   url = 'https://OAPI. Dingtalk. COM / robot / send? Access? Token = XX 'ා here is the webhook address of the pin robot
   print(send_msg(url,data))

If signature authentication is to be implemented, it is as follows

# -*- coding: utf-8 -*-
"""
Created on Mon Jan 13 11:18:59 2020
python3
@author: mi
"""
import json
import requests
import time
import hmac
import hashlib
import base64
import urllib
from urllib import parse

def send_msg(url,data):
    headers = {'Content-Type': 'application/json;charset=utf-8'}
    r = requests.post(url,data = json.dumps(data),headers=headers)
    return r.text
def auth(secret):
    timestamp = round(time.time() * 1000)
    secret = secret        #Secret key
    secret_enc = bytes(secret.encode('utf-8'))
    string_to_sign = '{}\n{}'.format(timestamp, secret)     #Take the timestamp+"\n" + key as the signature string string_to_sign
    string_to_sign_enc = bytes(string_to_sign.encode('utf-8'))
    hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()  #Using HmacSHA256 algorithm to calculate the signature, we get HMAC? Code
    hmac_code_base64=base64.b64encode(hmac_code)  #Base the HMAC code with Base64 encode
    sign = urllib.parse.quote(hmac_code_base64)   #Carry out urlEncode to get the final signature sign
    authlist=[timestamp,sign]
    return authlist
if __name__ == '__main__':
    data = {
    "msgtype": "link", 
    "link": {
        "text": "", 
        "title": "The train of the times is moving forward", 
        "messageUrl": "https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng"
        }
    }
    authlist = auth("Your signature")
    url = "https://oapi.dingtalk.com/robot/send?access_token=xxx"+"&timestamp="+str(authlist[0])+"&sign="+authlist[1]
    print(send_msg(url,data))

Message type

text type

{
    "msgtype": "text", 
    "text": {
        "content": "I am me, It's different fireworks@156xxxx8827"
    }, 
    "at": {
        "atMobiles": [
            "156xxxx8827", 
            "189xxxx8325"
        ], 
        "isAtAll": false
    }
}

The renderings are as follows

link type

{
    "msgtype": "link", 
    "link": {
        "text": "this new version to be released is called" mangrove "by founder xx.
Before that, when faced with major upgrades, product managers would choose a code for the occasion. This time, why "mangrove"? " 
        "title": "the train of the times moves forward", 
        "picUrl": "", 
        "messageUrl": "https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng
    }
}

The renderings are as follows

markdown type

{
     "msgtype": "markdown",
     "markdown": {
         "title":"Hangzhou weather",
         "text": "#### Hangzhou weather @ 156xxx8827 \ n "+
                 "> 9 Degree, northwest wind level 1, good air 89, relative temperature 73%\n\n" +
                 "> ![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)\n"  +
                 "> ###### Released at 10:20 [weather] (http://www.thinkpage.cn/) \n“
     },
    "at": {
        "atMobiles": [
            "156xxxx8827", 
            "189xxxx8325"
        ], 
        "isAtAll": false
    }
 }

The renderings are as follows

Overall jump ActionCard type

{
    "actionCard": {
        "title": "jobs wanted to build an apple coffee shop 20 years ago, which is the predecessor of the Apple Store", 
        "text": "![screenshot](@lADOpwk3K80C0M0FoA) 
 ###Apple cafe that jobs wanted to build 20 years ago 
 The design of Apple Store is moving from full sense of science and technology to life style, which can be traced back to Apple's plan to build a coffee shop 20 years ago, 
        "hideAvatar": "0", 
        "btnOrientation": "0", 
        "singleTitle": "reading the full text",
        "singleURL" : "https://www.dingtalk.com/"
    }, 
    "msgtype": "actionCard"
}

The renderings are as follows

Independent jump ActionCard type

{
    "actionCard": {
        "title": "jobs wanted to build an apple coffee shop 20 years ago, which is the predecessor of the Apple Store", 
        "text": "![screenshot](@lADOpwk3K80C0M0FoA) 
 ###Apple cafe that jobs wanted to build 20 years ago 
 The design of Apple Store is moving from full sense of science and technology to life style, which can be traced back to Apple's plan to build a coffee shop 20 years ago, 
        "hideAvatar": "0", 
        "btnOrientation": "0", 
        "btns": [
            {
                "title": "good content", 
                "actionURL": "https://www.dingtalk.com/"
            }, 
            {
                "title": "not interested", 
                "actionURL": "https://www.dingtalk.com/"
            }
        ]
    }, 
    "msgtype": "actionCard"
}

The renderings are as follows

FeedCard type

{
    "feedCard": {
        "links": [
            {
                "title": "The train of the times is moving forward", 
                "messageURL": "https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI", 
                "picURL": "https://www.dingtalk.com/"
            },
            {
                "title": "The train of the times goes forward 2", 
                "messageURL": "https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI", 
                "picURL": "https://www.dingtalk.com/"
            }
        ]
    }, 
    "msgtype": "feedCard"
}

The renderings are as follows

Published 367 original articles, praised 1225, visited 1.12 million+
His message board follow

Tags: JSON Python Android Java

Posted on Mon, 13 Jan 2020 00:58:49 -0800 by Comdemned