公司提出一个需求需要做一个企业微信的一个消息推送,需要将消息发送到聊天群里详细信息如下。
如何创建应用请阅读我的上篇文章:https://www.cnblogs.com/wangyajunblog/p/9996308.html
# -*- coding: UTF-8 -*- import requests import urllib.request import json def get_token(url, corpid, corpsecret): token_url = '%s/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (url, corpid, corpsecret) token = json.loads(urllib.request.urlopen(token_url).read().decode())['access_token'] return token #获取成员信息 corpid = 'ww****************' # 我的企业的id corpsecret = 'sGUmNQWa*************************************' #应用的Secret url = 'https://qyapi.weixin.qq.com' token=get_token(url, corpid, corpsecret) url="https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token="+token r=requests.get(url) department=r.json()['department'][1]['id'] url='https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token='+token+'&department_id='+str(department) r=requests.get(url) #print(r.json()) <--------输出成员信息 (可以注释) userid="*************************" #我的企业微信的账号 userid_list=["********************","*******************88"] #群内的人的账号#下面是获取list #userlist=r.json()['userlist'] #for user in userlist: # userid_list.append(user['userid']) #下面是建群 '''url='https://qyapi.weixin.qq.com/cgi-bin/appchat/create?access_token='+token print(userid_list) data={"name":"测试","owner":userid,"userlist":userid_list} data=json.dumps(data) r=requests.post(url,data=data) chatid=r.json()["chatid"] print(chatid)''' url="https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token="+token data={"chatid":"*********************","msgtype":"text","text":{"content":"自定义"},"safe":0} #chatid值是群组的id data=json.dumps(data) r=requests.post(url,data=data) #print(r.json()) <-----这条的意识是输出群的id信息 (可以注释)