archery 1.80推送工单到飞书webhook失败解决方案

今天收到开发反馈,archery工单不在飞群书内通知了,

从archery服务器cul 飞书群机器人webhook地址可以正常推送消息,估计是飞书接口发生了变化,

因为之前archery推送消息到飞书webhook也不行,最后去行webhook地址中的v2关键字才成功。

 

进入到archery 所在docker中查看日志,报错在【/opt/archery/common/utils/sendmsg.py】 第212行代码

[root@3a5c33b1a688 logs]# tail -f archery.log
  File "/opt/archery/sql/notify.py", line 64, in __send
    msg_sender.send_feishu_webhook(feishu_webhook, msg_title, msg_content)
  File "/opt/archery/common/utils/sendmsg.py", line 212, in send_feishu_webhook
    if r_json['ok']:
KeyError: 'ok'

 

查看【send_feishu_webhook】发送消息的方法,这个并不符合飞书规范,

飞书帮助文档:

https://open.feishu.cn/document/ukTMukTMukTM/uUjNz4SN2MjL1YzM

报错根本原因是缺少msg_type,这个是必填项。

def send_feishu_webhook(url, title, content):
    data = {
        xxxxxx 
    }
    r = requests.post(url=url, json=data)
    r_json = r.json()
    #if r_json['ok']:
    if r_json.get('StatusCode') == 0:
        logger.debug(f'飞书Webhook推送成功\n通知对象:{url}\n消息内容:{content}')
    else:
        logger.error(f"飞书Webhook推送失败错误码\n请求url:{url}\n请求data:{data}\n请求响应:{r_json}")

 

最后将

data = {

    xxxxx

      } 

替换为,可以正常推送消息。

    data = {
    "msg_type": "post",
    "content": {
        "post": {
            "zh_cn": {
                "title": title,
                "content": [
                    [
                        {
                            "tag": "text",
                            "text": content
                        }
                    ]
                ]
            }
        }
    }
}

 

 

我们目前正在由企业微信向飞书过渡阶段,webhook地址既配置了飞书,又配置了企业微信,

但archery推送消息是顺序推的,先推飞书,再推企业微信,如果推飞书失败,就退了代码不再继续推企业微信了。

 

最后看了github,才发现官方在1.8.1中修复这个问题了

https://github.com/hhyo/Archery/blob/master/common/utils/sendmsg.py

 

 

继续优化这段代码,如果工单审核通过后

    def send_feishu_webhook(url, title, content):
        if "审核通过" in title or "异常" in title:
            data={"msg_type": "post",
    "content": {
        "post": {
            "zh_cn": {
                "title": title,
                "content": [
                    [
                        {
                            "tag": "text",
                            "text": content
                        },
                        {
                            "tag": "at",
                            "user_id": "dba1"
                        },
                        {
                            "tag": "at",
                            "user_id": "dba2"
                        }
                    ]
                ]
            }
        }
    }
}
        else:
            data = {
    "msg_type": "post",
    "content": {
        "post": {
            "zh_cn": {
                "title": title,
                "content": [
                    [
                        {
                            "tag": "text",
                            "text": content
                        }
                    ]
                ]
            }
        }
    }
}

        r = requests.post(url=url, json=data)
        r_json = r.json()
        #if r_json['ok']:
        if r_json.get('StatusCode') == 0:
            logger.debug(f'飞书Webhook推送成功\n通知对象:{url}\n消息内容:{content}')
        else:
            logger.error(f"飞书Webhook推送失败错误码\n请求url:{url}\n请求data:{data}\n请求响应:{r_json}")

 

posted on 2021-06-24 15:55  柴米油盐酱醋  阅读(629)  评论(0编辑  收藏  举报

导航