This article introduces python3 development enterprise number sending text and text methods This article introduces python3 development enterprise number sending text and text methods
This example only applies to the enterprise number. if the public number is not tested, you can test it by yourself. the method is similar. Find the development documents on the official website. #! /Usr/bin/env python #-*-coding: UTF-8-*-import requestsimport jsonimport urllib. request ID = "xxxxxxxxxxxxxx" Secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX" UserID = "hequan2011" # List of member IDs (message recipients, separated by '|', up to 1000 recipients ). Special case: if it is specified as @ all, PartyID = 17 # Department ID list is sent to all members who are interested in the enterprise application. Multiple recipients are separated by '|, A maximum of 100 instances are supported. When touser is @ all, this parameter AppID = 0 # Application ID is ignored. the default value is the id of the Enterprise assistant enterprise application, an integer. You can view def get_token (): # obtain TOKEN gurl =" https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid= {} & Cortana Cret = {}". format (ID, Secret) r = requests. get (gurl) dict_result = (r. json () return dict_result ['Access _ token'] def get_media_ID (path): # upload to temporary clip Image 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 ): # Send text post_data ={} msg_content ['content'] = text # message content, the maximum length of 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' # indicates whether the message is a confidential message, 0 indicates no, 1 indicates yes, and the default value is 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): # send an image img_id = get_media_ID (path) post_data1 = {} msg_content1 = {} msg_content1 ['media _ id'] = img_id post_data1 ['touser'] = UserID post_data1 ['topart'] = 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") # text in the image directory send_text (" 123-He Quan ") # text content
The above is the detailed content of the graphic method sent by python3 development enterprise number. For more information, see other related articles on php Chinese network!