import requests
import json
import sys
import os
corp_id='wwe0xxxxxxxxc2d7xxxa28xxf790c7'
corp_secret='GNx-afsJ29xxQxxxQQhJLx_-x'
agent_id='1234000xxx043223656704'
file_path='/tmp/access_token.log'
file_path_windows='C:/Windows/Temp/access_token.log'
import platform
def TestPlatform():
print ("----------Operation System--------------------------")
print(platform.architecture())
print(platform.platform())
print(platform.system())
print ("--------------Python Version-------------------------")
print(platform.python_version())
sysstr = platform.system()
if(sysstr =="Windows"):
file_path=file_path_windows
if not os.path.exists(file_path):
fp=open(file_path, 'a+')
fp.close()
def get_access_token_from_file():
try:
f=open(file_path,'r+')
this_access_token=f.read()
f.close()
return this_access_token
except Exception as e:
print(e)
def get_access_token():
get_token_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s'%(corp_id,corp_secret)
r=requests.get(get_token_url)
request_json=r.json()
this_access_token=request_json['access_token']
r.close()
try:
f=open(file_path,'w+')
f.write(this_access_token)
f.close()
except Exception as e:
return this_access_token
flag=True
while(flag):
access_token=get_access_token_from_file()
if access_token=='':
access_token = get_access_token()
try:
to_user='@all'
message=sys.argv[3]
send_message_url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s'%access_token
message_params={
"touser":to_user,
"msgtype":"text",
"agentid":agent_id,
"text":{"content":message},
"safe":0
}
r=requests.post(send_message_url,data=json.dumps(message_params))
request_json=r.json()
errmsg=request_json['errmsg']
if errmsg != 'ok':
raise
flag=False
except Exception as e:
print(e)
access_token = get_access_token()