diff --git a/wechat.py b/wechat.py new file mode 100644 index 0000000..c3dc32e --- /dev/null +++ b/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