码迷,mamicode.com
首页 > 微信 > 详细

PYTHON发送微信企业号脚本

时间:2017-07-13 10:33:25      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:https   port   ons   ini   sys.argv   load   return   ken   lib   

#!/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发送微信企业号脚本

标签:https   port   ons   ini   sys.argv   load   return   ken   lib   

原文地址:http://www.cnblogs.com/palmxin/p/7158657.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!