PYTHON发送微信企业号脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PYTHON发送微信企业号脚本相关的知识,希望对你有一定的参考价值。
#!/usr/bin/env python
# coding:utf-8
import sys
import urllib2
import time
import json
import requests
reload(sys)
sys.setdefaultencoding(‘utf-8‘)
title = sys.argv[1]
content = sys.argv[2]
class Token(object):
# 获取token
def __init__(self, corpid, corpsecret):
self.baseurl = ‘https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}‘.format(corpid, corpsecre
t)
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(title, content):
# 发送消息
corpid = "在微信企业号后台获取"
corpsecret = "在微信企业号后台获取"
qs_token = Token(corpid=corpid, corpsecret=corpsecret).get_token()
url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}".format(qs_token)
payload = {
"touser": "@all",
"msgtype": "text",
"agentid": "0",
"text": {
"content": "标题:{0}\n 内容:{1}".format(title, content)
},
"safe": "0"
}
ret = requests.post(url, data=json.dumps(payload, ensure_ascii=False))
print ret.json()
if __name__ == ‘__main__‘:
# print title, content
send_msg(title, content)
以上是关于PYTHON发送微信企业号脚本的主要内容,如果未能解决你的问题,请参考以下文章