bugly异常消息推送钉钉——内网穿透技术

2020-11-27  本文已影响0人  无谓着

bugly的异常信息要推送到钉钉,不能直接推送到钉钉机器人,因为bugly推送到json格式跟钉钉机器人能接收的格式不一样,需要自己搭个服务,在bugly上填自己服务的接口接收bugly推送的json消息,然后对消息进行提取,再通过钉钉机器人进行推送到钉钉群。

对于本地搭的服务,怎么才能让bugly把消息推过来呢,这里就用到里钉钉的内网穿透技术
钉钉文档

https://ding-doc.dingtalk.com/doc#/kn6zg7/hb7000

1、下载工具,git地址

git clone https://github.com/open-dingtalk/pierced.git

2、进到工具目录中,修改./ding文件权限,命令启动代理域名

chmod 777 ./ding
./ding -config=./ding.cfg -subdomain=bugly 8080

启动后如图


image.png

这样访问http://bugly.vaiwan.com ,就能映射到本地127.0.0.1:8000上,本地起的服务,外网也能访问到了

完整内容如下:

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
import json
import requests

import time
def index(request):
    body = request.body.decode()
    print(body)
    data1 = json.loads(body)
    print(data1)
    t = data1["timestamp"]/1000
    m = ''
    timestamp = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(t))
    if len(data1["eventContent"]["datas"])>0:
        msg = data1["eventContent"]["datas"]
        for i in range(len(msg)):

            crashUser = msg[i]["crashUser"]
            version = msg[i]["version"]
            url = msg[i]["url"]
            m += "### [%s bugly崩溃统计问题%s](%s) \n> 版本%s\n> 影响人数%s\n"%(timestamp,i,url,version,crashUser)

        headers = {'Content-Type': 'application/json;charset=utf-8'}
        data = {
            "msgtype": "markdown",
            "at":
                {
                    "atMobiles": [
                        "13247705056"
                    ],
                    "isAtAll": False,
                },
            "markdown":
                {
                "text":m,
                "title":"bugly昨日崩溃问题",
                }

        }
        url = 'https://oapi.dingtalk.com/robot/send?access_token=6fb6f1ae86267d7bbe32410612f3a5e7d091e02ee6780be8792f5d45ed0d2c46'
        r = requests.post(url, data=json.dumps(data), headers=headers)


    return HttpResponse("测试通过、200")
上一篇 下一篇

猜你喜欢

热点阅读