标签:python import 微信企业号发送图文 news
网上的都是发送text文本,个人嫌弃格式太丑,改发图文,勿喷,喜欢拿去!
注意一下以下几个变量参数:
corpid
corpsecret
3. appid
#!/usr/bin/python # -*- coding:utf-8 -*- ‘‘‘ +-----------------------------------------------------------------------+ |Author: Cheng Wenfeng <277546922@qq.com> | +-----------------------------------------------------------------------+ ‘‘‘ import urllib,urllib2 import json import sys import base64 if int(sys.version_info[0]) < 3 : reload(sys) sys.setdefaultencoding(‘utf-8‘) def gettoken(corpid,corpsecret): gettoken_url = ‘https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=‘ + corpid + ‘&corpsecret=‘ + corpsecret try: token_file = urllib2.urlopen(gettoken_url) except urllib2.HTTPError as e: print e.code print e.read().decode("utf8") sys.exit() token_data = token_file.read().decode(‘utf-8‘) token_json = json.loads(token_data) token_json.keys() token = token_json[‘access_token‘] return token
def senddata(access_token,user,title,content): newtitle=base64.b64decode(title) newcontent=base64.b64decode(content) send_url = ‘https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=‘ + access_token send_values = { "touser":user, #企业号中的用户帐号 "toparty":"1", #企业号中的部门id "msgtype":"news", #企业号中的应用id,消息类型。 "agentid":"xxxxx", "news": { "articles":[ { "title":str(newtitle), "description":str(newcontent), #"url": "URL", #"picurl": "PIC_URL" } #{ #"title":newcontent, #"description": "Description", #"url": "URL", #"picurl": "PIC_URL" #} ] } } send_data = json.dumps(send_values, ensure_ascii=False) send_request = urllib2.Request(send_url, send_data) response = json.loads(urllib2.urlopen(send_request).read()) print str(response)
if __name__ == ‘__main__‘: user = str(sys.argv[1]) #传过来的第一个参数 #title = str(sys.argv[2]) #传过来的第二个参数 #content = str(sys.argv[3]) #传过来的第三个参数 title = base64.b64encode(str(sys.argv[2])) # 传入的字符做base64编码 content = base64.b64encode(str(sys.argv[3])) corpid = ‘xxxxxxxxxxxx‘ #CorpID是企业号的标识 corpsecret = ‘xxxxxxxxxxxxxxxxxxxxxxxxxx‘ #corpsecretSecret是管理组凭证密钥 accesstoken = gettoken(corpid,corpsecret) senddata(accesstoken,user,title,content) |
本文出自 “技术成就一切!” 博客,请务必保留此出处http://803540.blog.51cto.com/793540/1853278
标签:python import 微信企业号发送图文 news
原文地址:http://803540.blog.51cto.com/793540/1853278