From 404e27006cae9080c4ecd6daaa5d0246eea360e9 Mon Sep 17 00:00:00 2001 From: p39718465 <569659537@qq.com> Date: Thu, 31 Aug 2023 20:06:08 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E4=BA=8E=E7=9B=91=E6=B5=8B=E4=BC=81?= =?UTF-8?q?=E4=B8=9A=E5=BE=AE=E4=BF=A1=E5=B9=B6=E5=91=8A=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/wechat.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/wechat.py diff --git a/src/wechat.py b/src/wechat.py new file mode 100644 index 0000000..c3dc32e --- /dev/null +++ b/src/wechat.py @@ -0,0 +1,51 @@ +#!/usr/bin/python3 +#_*_coding:utf-8 _*_ +import urllib.request +import json +import sys + +def gettoken(corpid,corpsecret): + gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret + print (gettoken_url) + try: + token_file = urllib.request.urlopen(gettoken_url) + except urllib.error.HTTPError as e: + print (e.code) + print (e.read().decode("utf8")) + sys.exit() + token_data = token_file.read().decode('utf-8') + token_json = json.loads(token_data) + token_json.keys() + token = token_json['access_token'] + return token + +def senddata(access_token,user,subject,content): + + send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token + send_values = { + "touser":"$1", + "toparty":"2", + "msgtype":"text", + "agentid":"1000002", + "text":{ + "content":subject + '\n' + content + }, + "safe":"0" + } +# send_data = json.dumps(send_values, ensure_ascii=False) + send_data = json.dumps(send_values, ensure_ascii=False).encode('utf-8') + send_request = urllib.request.Request(send_url, send_data) + response = urllib.request.urlopen(send_request) + msg = response.read() + print ("returned value : " + str(msg)) + + +if __name__ == '__main__': + user = str(sys.argv[1]) + subject = str(sys.argv[2]) + content = str(sys.argv[3]) + + corpid = 'ww07d639f4db857c16' + corpsecret = 'gTBUceozR5SQLStDeEdKN2bWJYLF2jlzk4zoCn_8I34' + accesstoken = gettoken(corpid,corpsecret) + senddata(accesstoken,user,subject,content) \ No newline at end of file