Home > Article > WeChat Applet > Python3 develops WeChat enterprise account method for sending pictures and texts
This article introduces the method of developing WeChat enterprise account to send pictures and texts using python3
本例 只适用于企业号,公众号没试,可自行测试,方法差不多。有事找官网开发文档。 #!/usr/bin/env python # -*- coding: utf-8 -*- import requests import json import urllib.request ID="xxxxxxxxxxxx" Secret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" UserID = "hequan2011"##成员ID列表(消息接收者,多个接收者用'|'分隔,最多支持1000个)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送 PartyID=17 ##部门ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数 AppID = 0 ##应用ID,默认是 企业小助手 企业应用的id,整型。可在应用的设置页面查看 def get_token(): ##获取TOKEN gurl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={}&corpsecret={}".format(ID, Secret) r=requests.get(gurl) dict_result= (r.json()) return dict_result['access_token'] def get_media_ID(path): ##上传到临时素材 图片ID Gtoken = get_token() img_url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={}&type=image".format(Gtoken) files = {'image': open(path, 'rb')} r = requests.post(img_url, files=files) re = json.loads(r.text) return re['media_id'] ## def send_text(text): ##发送文字 post_data = {} msg_content = {} msg_content['content'] = text ## 消息内容,最长不超过2048个字节 post_data['touser'] = UserID post_data['toparty'] = PartyID post_data['msgtype'] = 'text' post_data['agentid'] = AppID post_data['text'] = msg_content post_data['safe'] = '0' #表示是否是保密消息,0表示否,1表示是,默认0 Gtoken = get_token() purl1="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}".format(Gtoken) json_post_data = json.dumps(post_data,False,False) request_post = urllib.request.urlopen(purl,json_post_data.encode(encoding='UTF8')) return request_post def send_tu(path): ##发送图片 img_id = get_media_ID(path) post_data1 = {} msg_content1 = {} msg_content1['media_id'] = img_id post_data1['touser'] = UserID post_data1['toparty'] = PartyID post_data1['msgtype'] = 'image' post_data1['agentid'] = AppID post_data1['image'] = msg_content1 post_data1['safe'] = '0' Gtoken = get_token() purl2="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}".format(Gtoken) json_post_data1 = json.dumps(post_data1,False,False) request_post = urllib.request.urlopen(purl2,json_post_data1.encode(encoding='UTF8')) return request_post send_tu("1.png") ##图片目录 send_text("123-何全") ##文字内容
The above is the detailed content of Python3 develops WeChat enterprise account method for sending pictures and texts. For more information, please follow other related articles on the PHP Chinese website!