Teach you to build a nail early warning robot hand in hand

Posted by jemrys on Thu, 06 Jan 2022 02:09:50 +0100

abstract

With the increasing production and on-line business and the increasing demand for unattended, the emergence of nail early warning robot has greatly solved our problem. From 0 to 1, this paper introduces in detail the construction and customization of nail early warning robot, which is divided into three steps: robot construction, early warning content development and online deployment

Robot construction

All of the following configuration development comes from Nailing official website document , if you like to read the official website or have further personalized needs, you can go to check it, or you can build it step by step from the perspective of junior developers

First, you must have a group and you have the permission to add robots to the group, and then

  • Add robot

Group settings - intelligent group assistant - add robot - customize, select add, and then make relevant settings

  • Set up Robot

First, give a nice name. There are three security settings, and there must be at least one

  1. User defined Keywords: user defined keywords for sending content. Only when the sent content contains the set keywords can it be sent
  2. Signature: that is, encryption. Generally, this one is used more,
  3. IP address: This is well understood. A specific IP can send content

Here we choose the second tag, and then click Add. The group will prompt you that you have successfully added a robot

We open the robot and save two data, namely Webhook token and signed ciphertext

https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxWebhook: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxx
Countersigned by: yyyyyyyyyyyyyyyyyy

These two xxxxxx and yyyyy y are reserved for standby

Early warning content development

The above robots have been added, which is equivalent to an API interface for receiving data. Now we start to develop early warning content

The simplest python is adopted, and the overall framework code is as follows

from datetime import datetime, timedelta
import requests
import hmac
import hashlib
import base64
import urllib.parse
import urllib.request
import json
import time
import os
import sys
import re
def getUrl(secret,access_token):
    timestamp = str(round(time.time() * 1000))
    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))
    url = "https://oapi.dingtalk.com/robot/send?access_token=%s&sign=%s&timestamp=%s" % (access_token, sign, timestamp)
    return url
def sendToGroup(url,header,data):
    sendData = json.dumps(data)#Convert dictionary type data to json format
    sendData = sendData.encode("utf-8") # Request of python3 requires data of byte type
    request = urllib.request.Request(url=url, data=sendData, headers=header)
    opener = urllib.request.urlopen(request)
    return opener


secret= ''
access_token=''
data={
 "msgtype": "text",
 "text": {
     "content": "Analysis task completed"
 },
  "at": {
      "atMobiles": ["13216816870"],
      "isAtAll": False
  }
}

header = {
"Content-Type": "application/json",
"Charset": "UTF-8"
}
url=getUrl(secret,access_token)
opener=sendToGroup(url,header,data)

Put the above xxxx and yyyy into the secret and access in the code_ Token, run and the nail robot will pop up

The whole content needs to be developed is the data part of the code

Our common query is through the database, so we also connect the database query here and add an sql function code

def exeSqlRes(dbip,dbport,dbacco,dbpwd,dbname,sql):
    conn = connect(host=dbip, port=dbport, user=dbacco, password=dbpwd, database=dbname) 
    cursor = conn.cursor() 
    try:
        cursor.execute(sql)    
        list1 = cursor.fetchall()
        global dataStr1,dataStr2
        dataStr1 = "In line operation date:%s" % (list1[0][0])
        dataStr2 = "Public offering operation date:%s" % (list1[1][0])
    except:
        print("Database connection failed\n")
    conn.close()
sql = '''
    SELECT SZ_TO_CHAR_D(max(d_date),'YYYYMMDD') as SYSTEMDATE from datacollect_log 
    union all 
    SELECT SZ_TO_CHAR_D(max(d_date),'YYYYMMDD') as SYSTEMDATE from datacollect_log 
    )
'''
exeSqlRes('xx.xx.xx.xx',3306,'xxxxx','xxxxx','xxxxx',sql)

The official website of nailing introduces a lot in the form of data, including txt, json, xml, etc. limited to space, here we only list the most commonly used MARKDOWN form

data = {
     "msgtype": "markdown",
     "markdown": {
         "title":"Data monitoring",
         "text": "#### Current data run date @17665011132 \n > - **%s** \n > - **%s** \n > ![screenshot](https://img-blog. csdnimg. cn/3c6a5aa682df459ab04d2fe5dc32475c. png? x-oss-process=image/watermark,type_ d3F5LXplbmhlaQ,shadow_ 50,text_ Q1NETiBA56We6Iq36L-m6JOd5a-6,size_ 20,color_ FFFFFF,t_ 70,g_ se,x_ 16) \ n > ########% s publish [View Details]( https://www.dingtalk.com) \n" % (dataStr1,dataStr2,timeStr)
     },
      "at": {
          "atMobiles": [
              "176xxxx1132"
          ],
          "atUserIds": [
              ""
          ],
          "isAtAll": "false"
      }
 }

You can develop data according to your actual needs. After the development, run the code to pop up

All configuration is complete

Online deployment

Put the python code into crontab and run it on demand

*/10 * * * * /bisz/python/anaconda3/bin/python /bisz/python/DingDingSend.py

Topics: Python