grafana_alert_to_dingding.py 913 Bytes
# -*- coding: utf8 -*-
import json
import requests 

print('receive grafana alert info')

DING =  "https://oapi.dingtalk.com/robot/send?access_token=31cf1a526c98862b7945e561c5a0d857a069350dbee0e807074b83b39fa1e1af"

def main_handler(event, context):
    
    print("Received event: " + json.dumps(event, indent = 2)) 
    grafana_body = json.loads(event['body'])

    # send text dingding message. see:https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.karFPe&treeId=257&articleId=105735&docType=1
    ding_req = {}
    ding_req["msgtype"]="markdown"
    ding_req["markdown"] = {}
    ding_req["markdown"]['title']="Grafana 告警"
    ding_req["markdown"]["text"]= "### %s ![alert_img](%s)" % (grafana_body['title'], grafana_body['imageUrl'])
    
    r = requests.post(url = DING, json = ding_req) 
    print("Send DingDing message result:%s, request: %s "  %(r, ding_req)) 
    return("SUCCESS")