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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# -*- coding: utf-8 -*-
# @Time : 2023/5/13 19:15
# @Author : 南宫乘风
# @Email : 1794748404@qq.com
# @File : app.py
# @Software: PyCharm
import datetime
import json
from time import strftime
from utils.send_ding import access_token, generate_timestamp, generate_sign, secret_enc
from dingtalkchatbot.chatbot import DingtalkChatbot
import datetime
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
def generate_text_info(url, title, context, author):
t = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
text = ('''<font color=\'#008000\'><b>[巡检]</b> </font><b>每天巡检</b>'''
+ '''\n\n --- \n\n'''
+ '''<font color=\'#708090\' size=2><b>详情:</b> [点击查看详情](%s)</font> \n\n '''
+ '''<font color=\'#778899\' size=2><b>巡检标题:</b> %s</font> \n\n '''
+ '''<font color=\'#708090\' size=2><b>巡检类容:</b> %s</font> \n\n '''
+ '''<font color=\'#708090\' size=2><b>相关人员:</b> %s</font>'''
+ '''\n\n --- \n\n'''
+ '''<b>播报时间:</b> %s''') % (url, title, context, author, t)
return text
def generate_text_error(url, title, context, author):
t = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
text = ('''<font color=\'#FF0000\'><b>[巡检]</b> </font><b>每天巡检异常</b>'''
+ '''\n\n --- \n\n'''
+ '''<font color=\'#708090\' size=2><b>详情:</b> [点击查看详情](%s)</font> \n\n '''
+ '''<font color=\'#778899\' size=2><b>巡检标题:</b> %s</font> \n\n '''
+ '''<font color=\'#708090\' size=2><b>巡检类容:</b> %s</font> \n\n '''
+ '''<font color=\'#708090\' size=2><b>相关人员:</b> %s</font>'''
+ '''\n\n --- \n\n'''
+ '''<b>播报时间:</b> %s''') % (url, title, context, author, t)
return text
def send_dingtalk_msg(title, wehook, text):
ddrobot = DingtalkChatbot(wehook)
ret = ddrobot.send_markdown(title, text=text, is_at_all=False)
return ret
def dingtalk_robot(url, title, content, author, level):
timestamp = generate_timestamp()
sign = generate_sign(secret_enc, timestamp)
# 构造请求 URL
wehook = 'https://oapi.dingtalk.com/robot/send?access_token={}×tamp={}&sign={}'.format(access_token, timestamp,
sign)
# wehook = 'https://oapi.dingtalk.com/robot/send?access_token=paste_your_token_from_dingtalk'
if level == "info":
text = generate_text_info(url, title, content, author)
else:
text = generate_text_error(url, title, content, author)
result = send_dingtalk_msg(title, wehook, text)
return result
message = {
'msg': '请按照上面格式 post 请求参数(source(认证) title(标题) content(内容) 必选参数)',
'data': {'source': 'heian', 'title': 'rds日志拉去', 'content': 'rds日志拉去正常',
'level': 'info|error (默认: info)',
'author': '南宫乘风(默认: 巡检机器人)'}
}
@app.route('/ding/send', methods=['POST', "GET"])
def send():
if request.method == 'GET':
return jsonify(message)
else:
json_data = request.get_json()
source = json_data.get('source')
title = json_data.get('title')
content = json_data.get('content')
level = json_data.get('level', 'info')
author = json_data.get('author', '巡检机器人')
if not source or not title or not content:
return jsonify(message)
if source != 'heian':
return "认证失败,请联系运维人员!"
else:
# 在这里处理传入的参数
url = 'http://127.0.0.1:5000/'
result = dingtalk_robot(url, title, content, author, level)
# print(result)
if result['errcode'] == 0:
print('消息发送成功!')
return "消息发送成功!"
else:
print('消息发送失败:', result['errmsg'])
return f"消息发送失败, {result['errmsg']}"
if __name__ == '__main__':
app.run(debug=True)
|