python调用企业微信API
时间:2017-09-26 10:41:48
收藏:0
阅读:2973
#!/usr/bin/env python # -*- coding:utf-8 -*- # 2017-07-25 编写 import json import sys import urllib, urllib2 """ CorpID 企业ID Secret 应用密钥 """ CorpID = ‘‘ Secret = ‘‘ touser = ‘@all‘ content = ‘‘ #获取access_token def getToken(CorpID, Secret): url = ‘https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s‘ % (CorpID, Secret) req = urllib2.Request(url) result = urllib2.urlopen(req) access_token = json.loads(result.read()) return access_token[‘access_token‘] #发送消息 def tonews(access_token, content): url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + access_token """ touser 成员 @all 就是所有 toparty 部门ID @all 就是所有 msgtype 文本类型 agentid 企业应用ID content 内容 safe 是否保密 0是不保密 """ values = { "touser" : touser, "toparty" : ‘2‘, "msgtype" : "text", "agentid" : 1, "text" : { "content" : content }, "safe" :"0" } send_data = json.dumps(values) send_request = urllib2.Request(url, send_data) response = json.loads(urllib2.urlopen(send_request).read()) if response[‘errcode‘] == 0: print ‘发送消息成功‘ if __name__ == ‘__main__‘: access_token = getToken(CorpID, Secret) print "获取token成功" content = ‘\n‘.join(sys.argv[1:]) if not content: content = "测试成功" tonews(access_token, content)
[root@400ec7d4b418 /]# python wechat.py 456 678 //需要传递的数据
本文出自 “小卡” 博客,请务必保留此出处http://xiaocuik.blog.51cto.com/12090846/1968648
评论(0)