1.1 概述
官方文档:http://work.weixin.qq.com/api/doc#12739
1.2 发送应用消息
参考文档:http://work.weixin.qq.com/api/doc#10167
# -*- coding:UTF-8 -*- import urllib2 import urllib import json def get_access_token(): url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?' # corpid: 每个企业都拥有唯一的corpid corpsecret: 自建应用secret para = {'corpid':'ww2f9a1a85f1806981','corpsecret':'tGFtVLxmjxPf6jj2t5SKyqEUYkpCM9e2hw-OrwSQwSg'} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) return ret token_id = get_access_token().get('access_token') # 所有请求必须携带access_token值进行身份验证 data = { "touser": "@all", "toparty": "", "totag": "", "msgtype": "text", "agentid": 1000002, "text": { "content": "测试发送信息" }, "safe": 0 } def send_msg(token_id,data): headers = {'Content-Type': 'application/json'} url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s"%(token_id) request = urllib2.Request(url=url, headers=headers, data=json.dumps(data)) response = urllib2.urlopen(request) print response.read() # 返回结果:{"errcode":0,"errmsg":"ok","invaliduser":""} send_msg(token_id,data)
1.3 接收消息与事件
1、概述 & 接收消息前准备
官方文档: http://work.weixin.qq.com/api/doc#12977
1.4 发消息到群聊会话
1、概述 & 接收消息前准备
官方文档: http://work.weixin.qq.com/api/doc#13308
2、 创建群聊会话
官方文档: http://work.weixin.qq.com/api/doc#13288
# -*- coding:UTF-8 -*- import urllib2 import urllib import json def get_access_token(): '''获取认证access_token值''' url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?' # corpid: 每个企业都拥有唯一的corpid corpsecret: 自建应用secret para = {'corpid':'ww2f9a1a85f1806981','corpsecret':'tGFtVLxmjxPf6jj2t5SKyqEUYkpCM9e2hw-OrwSQwSg'} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) return ret token_id = get_access_token().get('access_token') data = { "name" : "python交流群", # 群聊名 (选填) "owner" : "XiaoNaiQiang", # 指定群主的id (选填) "userlist" : ["XiaoNaiQiang", "ZhangPengPeng", ], # 群成员id列表,至少2人(必填) } print token_id def create_group(token_id,data): '''创建群会话 :param token_id: 用于认证的access_token :param data: 提交创建群会话的数据 ''' headers = {'Content-Type': 'application/json'} url = "https://qyapi.weixin.qq.com/cgi-bin/appchat/create?access_token=%s"%(token_id) request = urllib2.Request(url=url, headers=headers, data=json.dumps(data)) response = urllib2.urlopen(request) print response.read() # 返回结果:{"errcode":0,"errmsg":"ok","chatid":"16894972527282740677"} create_group(token_id,data)
3、修改群聊会话
官方文档:http://work.weixin.qq.com/api/doc#13292
# -*- coding:UTF-8 -*- import urllib2 import urllib import json def get_access_token(): '''获取认证access_token值''' url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?' # corpid: 每个企业都拥有唯一的corpid corpsecret: 自建应用secret para = {'corpid':'ww2f9a1a85f1806981','corpsecret':'tGFtVLxmjxPf6jj2t5SKyqEUYkpCM9e2hw-OrwSQwSg'} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) return ret token_id = get_access_token().get('access_token') data = { "chatid" : "16894972527282740677", # 群聊id (必填) # "name" : "NAME", # 新的群聊名(选填) "owner" : "XiaoNaiQiang", # 新群主的id(选填) "add_user_list" : ["DiYiShuai"], # 添加成员的id列表(选填) # "del_user_list" : [] # 踢出成员的id列表 } print token_id def update_group(token_id,data): '''修改群会话 :param token_id: 用于认证的access_token :param data: 提交修改会话的数据 ''' headers = {'Content-Type': 'application/json'} url = "https://qyapi.weixin.qq.com/cgi-bin/appchat/update?access_token=%s"%(token_id) request = urllib2.Request(url=url, headers=headers, data=json.dumps(data)) response = urllib2.urlopen(request) print response.read() # 返回结果:{"errcode":0,"errmsg":"ok"} update_group(token_id,data)
4、获取群聊会话
官方文档:http://work.weixin.qq.com/api/doc#13293
# -*- coding:UTF-8 -*- import urllib2 import urllib import json def get_access_token(): '''获取认证access_token值''' url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?' # corpid: 每个企业都拥有唯一的corpid corpsecret: 自建应用secret para = {'corpid':'ww2f9a1a85f1806981','corpsecret':'tGFtVLxmjxPf6jj2t5SKyqEUYkpCM9e2hw-OrwSQwSg'} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) return ret token_id = get_access_token().get('access_token') def get_group_info(token_id,chatid): ''' 获取群会话信息 :param token_id: 用于认证的access_token :param chatid: 刚刚新建群会话生成的chatid ''' url = 'https://qyapi.weixin.qq.com/cgi-bin/appchat/get?' para = {'access_token': token_id, 'chatid': chatid} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) print ret get_group_info(token_id,"16894972527282740677") # 这里的chatid是我们刚刚创建群会话设置的或随机生成的
5、应用推送消息
官方文档:http://work.weixin.qq.com/api/doc#13293
# -*- coding:UTF-8 -*- import urllib2 import urllib import json def get_access_token(): '''获取认证access_token值''' url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?' # corpid: 每个企业都拥有唯一的corpid corpsecret: 自建应用secret para = {'corpid':'ww2f9a1a85f1806981','corpsecret':'tGFtVLxmjxPf6jj2t5SKyqEUYkpCM9e2hw-OrwSQwSg'} req = urllib2.Request(url + urllib.urlencode(para)) ret = urllib2.urlopen(req) ret = json.loads(ret.read()) return ret token_id = get_access_token().get('access_token') data = { "chatid": "16894972527282740677", # 群聊id(必填) "msgtype":"text", # 消息类型(必填) "text":{ # 消息内容(必填) "content" : "你的快递已到 请携带工卡前往邮件中心领取" }, "safe":0 # 表示是否是保密消息,0表示否,1表示是,默认0(选填) } print token_id def send_msg_group(token_id,data): '''应用推送消息 :param token_id: 用于认证的access_token :param data: 发送到群会话的数据 ''' headers = {'Content-Type': 'application/json'} url = "https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token=%s"%(token_id) request = urllib2.Request(url=url, headers=headers, data=json.dumps(data)) response = urllib2.urlopen(request) print response.read() # 返回结果:{"errcode":0,"errmsg":"ok"} send_msg_group(token_id,data)