#coding:utf-8
import urllib2
import json
import sys
def getMsg():
#为了避免发送中文消息报错,使用utf8方式编码
reload(sys)
sys.setdefaultencoding('utf8')
#这个方法生成想要发送的消息
msg = '''
要发送的消息1
要发送的消息2
要发送的消息3
...
'''
return msg
if __name__ == '__main__':
#微信公众号上应用的CropID和Secret
CropID='xxxxxxxxxxxxxxxxxx'
Secret='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
#获取access_token
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s" % (CropID,Secret)
result=urllib2.urlopen(urllib2.Request(GURL)).read()
dict_result = json.loads(result)
Gtoken=dict_result['access_token']
#生成通过post请求发送消息的url
PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % Gtoken
#企业号中的应用id
AppID=1
#部门成员id,微信接收者
UserID=1
#部门id,定义可接收消息的成员范围
PartyID=1
#生成post请求信息
post_data = {}
msg_content = {}
msg_content['content'] = getMsg()
post_data['touser'] = UserID
post_data['toparty'] = PartyID
post_data['msgtype'] = 'text'
post_data['agentid'] = AppID
post_data['text'] = msg_content
post_data['safe'] = '0'
#由于字典格式不能被识别,需要转换成json然后在作post请求
#注:如果要发送的消息内容有中文的话,第三个参数一定要设为False
json_post_data = json.dumps(post_data,False,False)
#通过urllib2.urlopen()方法发送post请求
request_post = urllib2.urlopen(PURL, json_post_data)
#read()方法查看请求的返回结果
print request_post.read()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有