-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdingding.py
69 lines (55 loc) · 2.18 KB
/
dingding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
#Author: guomaoqiu
#Date: 2020-2-28
#Desc: 钉钉通知
import os
import sys
import json
import datetime
import requests,time
# 依赖包: pip install flask gitpython
from flask import Flask, request, jsonify,abort
def dingding(dingding_send_info):
# dingding_send_info = {
# "address": request.headers.get('X-Forwarded-For',request.remote_addr) ,
# "agent": str(request.user_agent),
# "title": u'😝点点滴滴😝'
# }
##
## 告警测试
print("*** 发送钉钉 *** ")
webhook = 'https://oapi.dingtalk.com/robot/send?access_token=3fbfe5dff3c6e5002d908b96c20f654256a4ff916aec169fc2ea79e683335b14'
## 定义接受信息的人和信息内容
user = '15928461018'
## 组装内容
data = {
"msgtype": "markdown",
"markdown": {
# "title":"MSG"
"title":u"" + dingding_send_info['title'],
"text": u"" + dingding_send_info['title'] + "\n\n" +
u"> AccessIP: " + dingding_send_info['address'] + "\n\n" +
u"> Time: " + time.strftime('%Y-%m-%d %H:%M:%S') + "\n\n" +
u"> Target: " + dingding_send_info['title'] + "\n\n"
u"> Browser: " + dingding_send_info['agent'] + "\n\n" +
# "> \n\n" +
"> [Add Blacklist?](http://x.sctux.com/blackip/" + dingding_send_info['address'] + ") \n"
"> [CheckIP](http://x.sctux.com/checkip/" + dingding_send_info['address'] + ") \n"
},
"at": {
"atMobiles": [user],
"isAtAll": False
}
}
## 调用request.post发送json格式的参数
headers = {'Content-Type': 'application/json'}
if "Ubuntu" not in dingding_send_info['agent'] and "Firefox" not in dingding_send_info['agent']:
result = requests.post(url=webhook, data=json.dumps(data), headers=headers,verify=False)
print('--'*30)
print(result)
print(result.json())
print('--'*30)
if result.json()["errcode"] == 0:
print("send ok")
else:
print("send failed!")