import
sys
import
urllib2
import
time
import
json
import
requests
reload
(sys)
sys.setdefaultencoding(
'utf-8'
)
print
sys.argv
title
=
sys.argv[
2
]
content
=
sys.argv[
3
]
user
=
sys.argv[
1
]
class
Token(
object
):
def
__init__(
self
, corpid, corpsecret):
corpid, corpsecret)
self
.expire_time
=
sys.maxint
def
get_token(
self
):
if
self
.expire_time > time.time():
request
=
urllib2.Request(
self
.baseurl)
response
=
urllib2.urlopen(request)
ret
=
response.read().strip()
ret
=
json.loads(ret)
if
'errcode'
in
ret.keys():
print
>> ret[
'errmsg'
], sys.stderr
sys.exit(
1
)
self
.expire_time
=
time.time()
+
ret[
'expires_in'
]
self
.access_token
=
ret[
'access_token'
]
return
self
.access_token
def
send_msg(content):
corpid
=
"wx1bf897b86ad3abe0"
corpsecret
=
'aquTZMoaLJ8M0hovSCDZeTPfqQuxLT12PnPQ8KwRcifikz7qxd1pwtZxPRqUXPJC'
qs_token
=
Token(corpid
=
corpid, corpsecret
=
corpsecret).get_token()
qs_token)
payload
=
{
"touser"
: user,
"msgtype"
:
"text"
,
"agentid"
:
"1"
,
"text"
: {
"content"
:
"{0}"
.
format
(content)
},
"safe"
:
"0"
}
ret
=
requests.post(url, data
=
json.dumps(payload, ensure_ascii
=
False
))
print
ret.json()
if
__name__
=
=
'__main__'
:
send_msg(content)