<Python heuristic automation > Nail push

If you want to receive notifications via a pin (note that it is not a pin email, all the email operations have been covered in the previous section)

Set up a swarm of robots on the nail, nail individual authority caused by.

If the permission please reference developers.dingtalk.com/document in detail for the enterprise

  • Add aThe customRobot and specifygroup
  • To get aWebhookaddress
  • For security reasons, addThe signature, i.e.,secret

According to the developers.dingtalk.com/document/ap… Implement a robot push function in Python

The textpush

# -*- coding: utf-8 -*-
import requests
import json
import time
import hmac
import hashlib
import base64
import urllib.parse


def dingMessage() :
    timestamp = str(round(time.time() * 1000))
    secret = 'SEC21022... cf06a65a761c86eb4027b'
    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)

    # Request URL, WebHook address
    webhook = f"https://oapi.dingtalk.com/robot/send?access_token=33576dfcb4... 7bc0ec08354cc181a&timestamp={timestamp}&sign={sign}"
    # Build the request header
    header = {
        "Content-Type": "application/json"."Charset": "UTF-8"
    }
    # Build request data
    tex = "Call 0. Call 0."
    message = {
        "msgtype": "text"."text": {
            "content": f" @137.... 97091{tex}"
        },

        # todo is @all
        "at": {
            "atMobiles": [
                "137... 7091"]."isAtAll": False}}Json wrap the requested data
    message_json = json.dumps(message)
    # Send request
    info = requests.post(url=webhook, data=message_json, headers=header)
    Print the returned result
    print(info.text)


if __name__ == "__main__":
    dingMessage()
Copy the code

What did

  • secretRobot signature
  • webhookThe robotAPIAddress, parameter composition:timestamp(Time stamp)sign(sha256encryptedbase64Signature generated by encoding)
  • texText information
  • messageSubject object, selection@Related person (here according to mobile phone number)

markdownpush

# -*- coding: utf-8 -*-
import json

import requests


def dingmessage() :
    import time
    import hmac
    import hashlib
    import base64
    import urllib.parse

    timestamp = str(round(time.time() * 1000))
    secret = 'SEC21022b79.... 9cf06a65a761c86eb4027b'
    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)

    # Request URL, WebHook address
    webhook = f"https://oapi.dingtalk.com/robot/send?access_token=33576dfc.... 4800d577bc0ec08354cc181a&timestamp={timestamp}&sign={sign}"
    # Build the request header
    header = {
        "Content-Type": "application/json"."Charset": "UTF-8"
    }
    message = {
        "msgtype": "markdown".# "text": {
        # "content": f" @137... 97091 {tex}"
        #}.

        "markdown": {
            "title": "Weather in Hangzhou"."text": #### Hangzhou weather @155... 773 \n> 900 degrees, northwest wind 1, air good 8009, Relative temperature 673%\n> [![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)](https://developers.dingtalk.com/do Cument /app/ song-normal-messages)\n> ###### 10:20 分 版 [weather](https://www.dingtalk.com) \n"
        },

        # todo is @all
        "at": {
            "atMobiles": [
                "15....773"]."isAtAll": False}}Json wrap the requested data
    message_json = json.dumps(message)
    # Send request
    info = requests.post(url=webhook, data=message_json, headers=header)
    Print the returned result
    print(info.text)


if __name__ == "__main__":
    dingmessage()
Copy the code

Web pagelink

# -*- coding: utf-8 -*-
import json

import requests


def dingmessage() :
    import time
    import hmac
    import hashlib
    import base64
    import urllib.parse

    timestamp = str(round(time.time() * 1000))
    secret = 'SEC21022b792577.... 8639cf06a65a761c86eb4027b'
    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)
    # Request URL, WebHook address
    webhook = f"https://oapi.dingtalk.com/robot/send?access_token=33576dfcb4.... 800d577bc0ec08354cc181a&timestamp={timestamp}&sign={sign}"

    header = {
        "Content-Type": "application/json"."Charset": "UTF-8"
    }

    message = {
        "msgtype": "link"."link": {
            "text": "This new version, which is about to be released, is what founder XX calls Mangrove. Before that, whenever there was a major upgrade, product managers would have a code name that suited the situation. This time, why mangrove?"."title": "The train of The Times moves on."."picUrl": ""."messageUrl": "https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srci d=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nett ype=WIFI"
        },

        # todo is @all
        "at": {
            "atMobiles": [
                "15....8773"]."isAtAll": False
        }

    }
    message_json = json.dumps(message)
    info = requests.post(url=webhook, data=message_json, headers=header)
    print(info.text)


if __name__ == "__main__":
    dingmessage()
Copy the code

cardpush

The overall jumpActionCardtype

# -*- coding: utf-8 -*-
import json

import requests


def dingmessage() :
    import time
    import hmac
    import hashlib
    import base64
    import urllib.parse

    timestamp = str(round(time.time() * 1000))
    secret = 'SEC21022b7925.... 064668639cf06a65a761c86eb4027b'
    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)
    # Request URL, WebHook address
    webhook = f"https://oapi.dingtalk.com/robot/send?access_token=33576dfcb..... 4800d577bc0ec08354cc181a&timestamp={timestamp}&sign={sign}"

    header = {
        "Content-Type": "application/json"."Charset": "UTF-8"
    }

    message = {
        "msgtype": "actionCard"."actionCard": {
            "text": ! "" [screenshot] (https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png) \ n \ n # # # # 20 years ago to create jobs apple cafe \ n \ n Apple Store design is moving from the original full of technology to life, and the life trend can actually be traced back to Apple's plan to build a coffee shop 20 years ago."."title": "The train of The Times moves on."."btnOrientation": "0"."singleTitle" : "Read the full text"."singleURL" : "https://www.dingtalk.com/"
        },

        # todo is @all
        "at": {
            "atMobiles": [
                "155....8773"]."isAtAll": False
        }

    }
    message_json = json.dumps(message)
    info = requests.post(url=webhook, data=message_json, headers=header)
    print(info.text)


if __name__ == "__main__":
    dingmessage()
Copy the code

Independent jumpActionCardtype

# -*- coding: utf-8 -*-
import json

import requests


def dingmessage() :
    import time
    import hmac
    import hashlib
    import base64
    import urllib.parse

    timestamp = str(round(time.time() * 1000))
    secret = 'SEC21022b792..... 39cf06a65a761c86eb4027b'
    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)
    # Request URL, WebHook address
    webhook = f"https://oapi.dingtalk.com/robot/send?access_token=33576dfcb4..... 0ec08354cc181a&timestamp={timestamp}&sign={sign}"

    header = {
        "Content-Type": "application/json"."Charset": "UTF-8"
    }

    message = {
        "msgtype": "actionCard"."actionCard": {
            "text": ! "" [screenshot] (https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png) \ n \ n # # # # 20 years ago to create jobs apple cafe \ n \ n Apple Store design is moving from the original full of technology to life, and the life trend can actually be traced back to Apple's plan to build a coffee shop 20 years ago."."title": "The train of The Times moves on."."hideAvatar": "0"."btnOrientation": "1"."btns": [{"title": "Good content."."actionURL": "https://www.dingtalk.com/"
                },
                {
                    "title": "Not interested."."actionURL": "https://www.dingtalk.com/"}},# todo is @all
        "at": {
            "atMobiles": [
                "155....8773"]."isAtAll": False
        }

    }
    message_json = json.dumps(message)
    info = requests.post(url=webhook, data=message_json, headers=header)
    print(info.text)


if __name__ == "__main__":
    dingmessage()
Copy the code

These are several types of nail swarm robot notifications.

In a word, nail robot push is a very good choice, enough to deal with all kinds of scenarios in daily work (periodic reminder, threshold alarm, event notification, etc.)