python微信api_python调用企业微信API

#!/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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值