天天看點

調用釘釘接口向群裡推送告警

先在群裡申請“群機器人”,得到access_token

然後調用接口:

#!/usr/bin/python

import urllib, urllib2, time, datetime, httplib, json

def sendMsgToDingDing(alarm):

    url_alarm1 = "https://oapi.dingtalk.com/robot/send?access_token=e82c1....."

    alarm_log = '%s [crash] ' % datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

    msg_data = {}

    msg_data["msgtype"] = "text"

    msg_data["text"] = {}

    msg_data["text"]["content"] = alarm

    msg_data["at"] = {}

    msg_data["at"]["isAtAll"] = True

    with open('alarm.log', 'a') as f:

        f.write('%s\n' % alarm_log)

    headers = {'Content-Type': 'application/json'}

    request = urllib2.Request(url=url_alarm1, headers=headers, data=json.dumps(msg_data))

    response = urllib2.urlopen(request)