python 微信企业号

来源:互联网 发布:颓废 知乎 编辑:程序博客网 时间:2024/05/17 05:55

准备,如果没有微信企业号,可以先申请体验号
记下CorpID和Secret(获取Token用)
这里写图片描述

发送消息
首先可以在微信的开发者中心,查看接口文档

下面就是python代码:
1、根据CorpID和Secret得到token

def get_token_in_time(corp_id, secret):    res = urllib2.urlopen('https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (corp_id, secret))    res_dict = simplejson.loads(res.read())    token = res_dict.get('access_token', False)    return token

2、send

def send_txt_msg(token, content, to_user="@all", to_party="", to_tag="", application_id=0, safe=0):    try:        data = {            "touser": to_user,            "toparty": to_party,            "totag": to_tag,            "msgtype": "text",            "agentid": application_id,            "text": {"content": content},            "safe": safe        }        data = simplejson.dumps(data, ensure_ascii=False)        print data, type(data)        req = urllib2.Request('https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s' % (token,))        resp = urllib2.urlopen(req, data)        msg = u'返回值:' + resp.read()    except Exception, ex:        msg = u'异常:' + str(ex)    finally:        print msg
0 0