企业微信通知脚本不能用了(非wecom_robot)

Viewed 80

v5可以用的一个通知脚本notify.py,V6不能用了,

脚本如下:需要改什么,或要改什么配置
cat /opt/n9e/etc/script/notify.py
#!/usr/bin/env python

-- coding: UTF-8 --

import sys,getopt
import json
import requests

class Sender(object):
@classmethod
def send_email(cls, payload):
# already done in go code
pass

@classmethod
def send_wecom(cls, payload):
    acc_id=1000008
    acc_serc='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    corpid='xxxxxxxxxxxxxxxxxxxxxx'
    corpsecret=acc_serc
    url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?'
    requests.adapters.DEFAULT_RETRIES = 5 # 增加重连次数
    s = requests.session()
    s.keep_alive = False # 关闭多余连接
    rep = s.get(url+'corpid='+corpid+'&corpsecret='+corpsecret)
    token=rep.json().get('access_token')
    users = payload.get('event').get("notify_users_obj")
    tokens={}

    for u in users:
        contacts = u.get("contacts")
        if contacts.get("wecom_user_id", ""):
            tokens[contacts.get("wecom_user_id", "")] = 1


    for t in tokens:
        url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}".format(token)
        body = {
            "msgtype": "text",
            "touser" :"{}".format(t),
            "agentid" : acc_id,
            "text": {
                "content": payload.get('tpls').get("wecom.tpl", "wecom.tpl not found")
            }
        }
        headers = {'Content-Type':'application/json'}
        req = requests.post(url=url, json=body, headers=headers)
        try:
            
            print(req) 
        except (error):
            print(error)

@classmethod
def send_dingtalk(cls, payload):
    # already done in go code
    pass

@classmethod
def send_feishu(cls, payload):
    # already done in go code
    pass

@classmethod
def send_sms(cls, payload):
    users = payload.get('event').get("notify_users_obj")
    phones = {}
    for u in users:
        if u.get("phone"):
            phones[u.get("phone")] = 1
    if phones:
        print("send_sms not implemented, phones: {}".format(phones.keys()))

@classmethod
def send_voice(cls, payload):
    users = payload.get('event').get("notify_users_obj")
    phones = {}
    for u in users:
        if u.get("phone"):
            phones[u.get("phone")] = 1
    if phones:
        print("send_voice not implemented, phones: {}".format(phones.keys()))

def main():
payload = json.load(sys.stdin)
with open(".payload", 'w') as f:
f.write(json.dumps(payload, indent=4))
for ch in payload.get('event').get('notify_channels'):
send_func_name = "send_{}".format(ch.strip())
if not hasattr(Sender, send_func_name):
print("function: {} not found", send_func_name)
continue
send_func = getattr(Sender, send_func_name)
send_func(payload)

def hello():
print("hello nightingale")

if name == "main":
if len(sys.argv) == 1:
main()
elif sys.argv[1] == "hello":
hello()
else:
print("I am confused")

2 Answers

找到原因了,脚本是没有问题的。 配置问题,脚本那边web配置默认超时时间是0,改成10就好了。这个默认值看是不是要改一下。

哈哈,这是让社区小伙伴帮你debug么?我可以提供思路,你可以:
1,看日志,日志在n9e的日志里,n9e去调用脚本,成功与否都会打印日志
2,查看 .payload 隐藏文件,看看是否生成,看看里边的数据结构和代码是否匹配

n9e日志显示超时,应该是原来会有stdin,现在默认没了。
2023-03-29 10:34:43.383764 ERROR sender/plugin.go:81 event_notify: timeout and killed process /opt/n9e/etc/script/notify.py