计算任务完成后发送微信通知

发布于 2022-01-25  441 次阅读


第一步 申请Server酱账号,并配置企业微信通道

UID前不用加@

第二步 自行搭建Wecomchan酱

项目地址:https://github.com/easychen/wecomchan

  • 安装依赖:pip3 install requests -i https://pypi.tuna.tsinghua.edu.cn/simple
  • 编写如下.py文件,并 chmod +x
#!/usr/bin/python3
import json,requests,base64
from sys import argv as sys_argv

def getsend(wecom_cid, wecom_aid, wecom_secret):
    get_token_url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={wecom_cid}&corpsecret={wecom_secret}"
    response = requests.get(get_token_url).content
    access_token = json.loads(response).get('access_token')
    if access_token and len(access_token) > 0:
        send_msg_url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}'
        def send(text, wecom_touid='@all'):
            data = {
                "touser":wecom_touid,
                "agentid":wecom_aid,
                "msgtype":"text",
                "text":{
                    "content":text
                },
                "duplicate_check_interval":600
            }
            response = requests.post(send_msg_url,data=json.dumps(data)).content
            return response
    else:
        def send(text, wecom_touid='@all'):
            print('get_token Failed')
    return send

### 以下请按实际情况自行配置,以上勿动 ###
'''
WECOM_CID		企业微信公司ID
WECOM_AID		企业微信应用ID
WECOM_SECRET	企业微信应用Secret
wecom_touid		消息发送对象的UID
'''
info = getsend( # 与Server酱中的参数一致
    wecom_cid = '***',
    wecom_aid = '100000*',
    wecom_secret = '***'
    )
wecom_touid = 'limour'
### 以上请按实际情况自行配置,以下勿动 ###

if __name__ == '__main__':
    if len(sys_argv) == 2:
        env_arg = sys_argv[1]
    else:
        env_arg = '参数传递错误!'

    info(
        text = env_arg,
        wecom_touid = wecom_touid
        )

 

第三步 shell调用

python3 ./wecomchan.py 'task successfully completed'

 

第四步 R调用

system(paste("python3 /home/jovyan/gene/zl_liu/wecomchan.py", "'task successfully completed'"), intern = T)

 

第五步 python调用

from os import system
system(f"python3 /home/jovyan/gene/zl_liu/wecomchan.py 'task successfully completed'")

 


医学生