In this article, the example tells about the WeChat public number group sending picture and text message function realized by Python. Shared for your reference, as follows:
In WeChat public development, the use of api are to attach access_token content. Therefore, first you need to get access_token. as follows:
#get microsoft access_token def get_token(): payload_access_token={ 'grant_type':'client_credential', 'appid':'xxxxxxxxxxxxx', 'secret':'xxxxxxxxxxxxx' } token_url='/cgi-bin/token' r=(token_url,params=payload_access_token) dict_result= (()) return dict_result['access_token']
The media_id of the image that has been uploaded is required when group posting images.Note that the interface must be used when group posting images:/cgi-bin/material/add_material。
# Get the media_ID of the uploaded file # Group images must use the media_ID provided by the api def get_media_ID(path): img_url='/cgi-bin/material/add_material' payload_img={ 'access_token':get_token(), 'type':'image' } data ={'media':open(path,'rb')} r=(url=img_url,params=payload_img,files=data) dict =() return dict['media_id']
Subscription number for group sending, must be through the grouping id, first need to get all the users grouping situation.
# Query all user group information def get_group_id(): url="/cgi-bin/groups/get" payload_id={ 'access_token':get_token() } r=(url=url,params=payload_id) result=() return result['groups']
You need to select a group for mass mailing, here I select the first valid group for mass mailing (i.e., the first group where the number of users is not 0).
#Returns the first valid group group id. def get_first_group_id(): groups =get_group_id() group_id =0 for group in groups: if(group['count']!=0): group_id=group['id'] break; return group_id
The following code is used to send a group text message to the first valid group:
def send_txt_to_first_group(str='Hello World!'): group_id =get_first_group_id() pay_send_all={ "filter":{ "is_to_all":False, "group_id":group_id }, "text":{ "content":str }, "msgtype":"text" } url="/cgi-bin/message/mass/sendall?access_token="+get_token() # Need to specify the json encoding will not be transcoded to unicode, otherwise the message will be displayed as unicode code, can not be displayed correctly. r=(url=url,data=(pay_send_all,ensure_ascii=False,indent=2))# This parameter must be specified here result=() # Judge success based on whether the content of the return code is 0 or not return result['errcode']==0
The following code is used to group images to the first valid grouping.
def send_img_to_first_group(path='/home/fit/Desktop/'): group_id =get_first_group_id() pay_send_all={ "filter":{ "is_to_all":False, "group_id":group_id }, "image":{ "media_id":get_media_ID(path) }, "msgtype":"image" } url="/cgi-bin/message/mass/sendall?access_token="+get_token() r=(url=url,data=(pay_send_all)) result=() # Judge success based on whether the content of the return code is 0 or not return result['errcode']==0
Here are all the codes:
# -*- coding: utf-8 -*- import requests # First get the access_token import json #get microsoft access_token def get_token(): payload_access_token={ 'grant_type':'client_credential', 'appid':'xxxxxxxxxx', 'secret':'xxxxxxxxx' } token_url='/cgi-bin/token' r=(token_url,params=payload_access_token) dict_result= (()) return dict_result['access_token'] # Get the media_ID of the uploaded file # Group images must use the media_ID provided by the api def get_media_ID(path): img_url='/cgi-bin/material/add_material' payload_img={ 'access_token':get_token(), 'type':'image' } data ={'media':open(path,'rb')} r=(url=img_url,params=payload_img,files=data) dict =() return dict['media_id'] # Query all user group information def get_group_id(): url="/cgi-bin/groups/get" payload_id={ 'access_token':get_token() } r=(url=url,params=payload_id) result=() return result['groups'] #Returns the first valid group group id. def get_first_group_id(): groups =get_group_id() group_id =0 for group in groups: if(group['count']!=0): group_id=group['id'] break; return group_id def send_img_to_first_group(path='/home/fit/Desktop/'): group_id =get_first_group_id() pay_send_all={ "filter":{ "is_to_all":False, "group_id":group_id }, "image":{ "media_id":get_media_ID(path) }, "msgtype":"image" } url="/cgi-bin/message/mass/sendall?access_token="+get_token() r=(url=url,data=(pay_send_all)) result=() print result # Judge success based on whether the content of the return code is 0 or not return result['errcode']==0 def send_txt_to_first_group(str='Hello World!'): group_id =get_first_group_id() pay_send_all={ "filter":{ "is_to_all":False, "group_id":group_id }, "text":{ "content":str }, "msgtype":"text" } url="/cgi-bin/message/mass/sendall?access_token="+get_token() # Need to specify the json encoding will not be transcoded to unicode, otherwise the message will be displayed as unicode code, can not be displayed correctly. r=(url=url,data=(pay_send_all,ensure_ascii=False,indent=2))# This parameter must be specified here result=() # Judge success based on whether the content of the return code is 0 or not return result['errcode']==0 if(send_txt_to_first_group("I wish you joy and happiness for your family!")): print 'success!' else: print 'fail!'
Appendix: When using the WeChat Test Subscription to test the mass image interface, the return code is as follows:
{u'errcode': 45028, u'errmsg': u'has no masssend quota hint: [OKvFdA0813ge12]'}
This is because the test subscription doesn't have permission to send group graphic messages, not because of an error in the interface call.
PS:
Author's github./zhoudayang
Readers interested in more Python related content can check out this site's topic: theSummary of Python string manipulation techniques》、《Summary of Python coding manipulation techniques》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tipsand thePython introductory and advanced classic tutorials》。
I hope that what I have said in this article will help you in Python programming.