Sign inSign up

xiao1121feng/python-flask-requests

By xiao1121feng

Updated over 3 years ago

Image
0

170

xiao1121feng/python-flask-requests repository overview

运行该镜像,执行命令 /data/app.py
需要挂载两个文件 /data/app.py /daa/env.py
#!/usr/bin/python3
# coding=utf-8
import requests, os
from flask import request, jsonify
from multiprocessing import Process
from flask import Flask
import json, time
from jsonsearch import JsonSearch
import env

app = Flask(__name__)
app.debug = True

@app.route('/tg', methods=['post'])
def post_http():
    if not request.data:  # 检测是否有数据
        return ('fail')
    params = request.data.decode('utf-8')
    # print(params)
    # 获取到POST过来的数据,因为我这里传过来的数据需要转换一下编码。根据晶具体情况而定
    prams = json.loads(params)
    print(prams)
    # 把区获取到的数据转为JSON格式。
    return recode_json(prams)
    # 返回JSON数据。


@app.route('/git', methods=['post'])
def post_http_git():
    if not request.data:  # 检测是否有数据
        return ('fail')
    params = request.data.decode('utf-8')
    # print(params)
    # 获取到POST过来的数据,因为我这里传过来的数据需要转换一下编码。根据晶具体情况而定
    prams = json.loads(params)
    print(prams)
    # 把区获取到的数据转为JSON格式。
    return send_my(str(params))
    # 返回JSON数据。


def recode_json(prams):
    yichang = None
    print(type(prams))
    print(str(prams))
    send_my(str(prams))
    jsondata = JsonSearch(object=prams, mode='j')
    class_ = jsondata.search_first_value(key='status')
    alertname = jsondata.search_first_value(key='alertname')
    namespace = jsondata.search_first_value(key='namespace')
    alert_class = jsondata.search_first_value(key='summary')
    container = jsondata.search_first_value(key='container')
    locale_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
    message1 = jsondata.search_first_value(key='summaryCn') if jsondata.search_first_value(key='message') else jsondata.search_first_value(key='message')
    message2 = jsondata.search_first_value(key='message')
    pod = jsondata.search_first_value(key='pod')

    try:
        pass
        # return send_im_tg(msg) if 'im' in ('' if namespace is None else namespace) else send_ops_tg(msg)
    except Exception as e:
        yichang = f'异常了: {e}'
        # return send_my(str(json))
    finally:
        msg = f'''类型:💧{class_}💧
告警名:\t{alertname}
命名空间:\t{namespace}
服务: \t{container}
pod:\t{pod}
告警内容:\t{alert_class}
告警消息:\t{message1} {message2}
告警时间:\t{locale_time}'''
        if yichang is not None:
            send_ops_tg(str(yichang))
            return send_my(str(yichang))
        else:
            return send_im_tg(msg) if 'im' in ('' if namespace is None else namespace) else send_ops_tg(msg)


def send_im_tg(prams):
    r = requests.post(f'https://api.telegram.org/bot{env.im["tg_token"]}/sendMessage',
                      json={"chat_id": env.im["channel_id"], "text": prams})
    print(r.json())
    return 'ok'


def send_ops_tg(prams):
    r = requests.post(f'https://api.telegram.org/bot{env.ops["tg_token"]}/sendMessage',
                      json={"chat_id": env.ops["channel_id"], "text": prams})
    print(r.json())
    return 'ok'


def send_my(prams):
    r = requests.post(f'https://api.telegram.org/bot{env.my["tg_token"]}/sendMessage',
                      json={"chat_id": env.my["channel_id"], "text": prams})
    print(r.json())
    return 'ok'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
    # 这里指定了地址和端口号。也可以不指定地址填0.0.0.0那么就会使用本机地址ip
发送到telegram bot的token,和群组id
im = {
    "tg_token": '5625473518:AAFcRFhLFwLhgnM8qAbgX9Cxxxxxxxxxxxx',
    "channel_id": '-879061180'
}

ops ={
    "tg_token": '5625473518:AAFcRFhLFwLhgnM8qAbgX9Cxxxxxxxxxxxx',
    "channel_id": '-852971288'
}

my = {
    "tg_token": '5625473518:AAFcRFhLFwLhgnM8qAbgX9CR6LZlxxxxxxx',
    "channel_id": '-888771261'
}

Tag summary

Content type

Image

Digest

sha256:c029ff499

Size

344 MB

Last updated

over 3 years ago

docker pull xiao1121feng/python-flask-requests:3.9-v2