Python實現(xiàn)的微信公眾號群發(fā)圖片與文本消息功能實例詳解
本文實例講述了Python實現(xiàn)的微信公眾號群發(fā)圖片與文本消息功能。分享給大家供大家參考,具體如下:
在微信公眾號開發(fā)中,使用api都要附加access_token內(nèi)容。因此,首先需要獲取access_token。如下:
#獲取微信access_token def get_token(): payload_access_token={ 'grant_type':'client_credential', 'appid':'xxxxxxxxxxxxx', 'secret':'xxxxxxxxxxxxx' } token_url='https://api.weixin.qq.com/cgi-bin/token' r=requests.get(token_url,params=payload_access_token) dict_result= (r.json()) return dict_result['access_token']
在群發(fā)圖片時,需要提供已經(jīng)上傳圖片的media_id。注意,群發(fā)圖片的時候,必須使用接口:https://api.weixin.qq.com/cgi-bin/material/add_material。
#獲取上傳文件的media_ID #群發(fā)圖片的時候,必須使用該api提供的media_ID def get_media_ID(path): img_url='https://api.weixin.qq.com/cgi-bin/material/add_material' payload_img={ 'access_token':get_token(), 'type':'image' } data ={'media':open(path,'rb')} r=requests.post(url=img_url,params=payload_img,files=data) dict =r.json() return dict['media_id']
訂閱號進(jìn)行群發(fā),必須通過分組id,首先需要獲取所有的用戶分組情況。
#查詢所有用戶分組信息 def get_group_id(): url="https://api.weixin.qq.com/cgi-bin/groups/get" payload_id={ 'access_token':get_token() } r=requests.get(url=url,params=payload_id) result=r.json() return result['groups']
需要選擇一個分組進(jìn)行群發(fā),在這里我選擇第一個有效的分組進(jìn)行群發(fā)(即第一個分組用戶數(shù)不為0的分組)。
#返回第一個有效的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
下面的代碼用于群發(fā)文本消息,群發(fā)給第一個有效的分組:
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="https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token="+get_token() #需要指定json編碼的時候不會對中文轉(zhuǎn)碼為unicode,否則群發(fā)的消息會顯示為unicode碼,不能正確顯示 r=requests.post(url=url,data=json.dumps(pay_send_all,ensure_ascii=False,indent=2))#此處的必須指定此參數(shù) result=r.json() #根據(jù)返回碼的內(nèi)容是否為0判斷是否成功 return result['errcode']==0
下面的代碼用于群發(fā)圖片,群發(fā)給第一個有效的分組。
def send_img_to_first_group(path='/home/fit/Desktop/test.jpg'): 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="https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token="+get_token() r=requests.post(url=url,data=json.dumps(pay_send_all)) result=r.json() #根據(jù)返回碼的內(nèi)容是否為0判斷是否成功 return result['errcode']==0
以下是所有代碼:
# -*- coding: utf-8 -*- import requests #首先獲取access_token import json #獲取微信access_token def get_token(): payload_access_token={ 'grant_type':'client_credential', 'appid':'xxxxxxxxxx', 'secret':'xxxxxxxxx' } token_url='https://api.weixin.qq.com/cgi-bin/token' r=requests.get(token_url,params=payload_access_token) dict_result= (r.json()) return dict_result['access_token'] #獲取上傳文件的media_ID #群發(fā)圖片的時候,必須使用該api提供的media_ID def get_media_ID(path): img_url='https://api.weixin.qq.com/cgi-bin/material/add_material' payload_img={ 'access_token':get_token(), 'type':'image' } data ={'media':open(path,'rb')} r=requests.post(url=img_url,params=payload_img,files=data) dict =r.json() return dict['media_id'] #查詢所有用戶分組信息 def get_group_id(): url="https://api.weixin.qq.com/cgi-bin/groups/get" payload_id={ 'access_token':get_token() } r=requests.get(url=url,params=payload_id) result=r.json() return result['groups'] #返回第一個有效的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/test.jpg'): 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="https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token="+get_token() r=requests.post(url=url,data=json.dumps(pay_send_all)) result=r.json() print result #根據(jù)返回碼的內(nèi)容是否為0判斷是否成功 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="https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token="+get_token() #需要指定json編碼的時候不會對中文轉(zhuǎn)碼為unicode,否則群發(fā)的消息會顯示為unicode碼,不能正確顯示 r=requests.post(url=url,data=json.dumps(pay_send_all,ensure_ascii=False,indent=2))#此處的必須指定此參數(shù) result=r.json() #根據(jù)返回碼的內(nèi)容是否為0判斷是否成功 return result['errcode']==0 if(send_txt_to_first_group("祝你合家歡樂,幸福美滿!")): print 'success!' else: print 'fail!'
附錄:在使用微信測試訂閱號測試群發(fā)圖片接口的時候,返回碼如下:
{u'errcode': 45028, u'errmsg': u'has no masssend quota hint: [OKvFdA0813ge12]'}
這是因為測試訂閱號沒有群發(fā)圖文消息的權(quán)限,并不是因為接口調(diào)用有誤。
PS:
作者的github: https://github.com/zhoudayang
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》及《Python入門與進(jìn)階經(jīng)典教程》。
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
keras 實現(xiàn)輕量級網(wǎng)絡(luò)ShuffleNet教程
這篇文章主要介紹了keras 實現(xiàn)輕量級網(wǎng)絡(luò)ShuffleNet教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06python通過opencv調(diào)用攝像頭操作實例分析
在本篇文章里小編給大家整理的是一篇關(guān)于python通過opencv調(diào)用攝像頭操作實例分析內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-06-06使用jupyter notebook將文件保存為Markdown,HTML等文件格式
這篇文章主要介紹了使用jupyter notebook將文件保存為Markdown,HTML等文件格式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04django如何根據(jù)現(xiàn)有數(shù)據(jù)庫表生成model詳解
這篇文章主要給大家介紹了關(guān)于django如何根據(jù)現(xiàn)有數(shù)據(jù)庫表生成model的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Django具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-08-08Python超簡單分析評論提取關(guān)鍵詞制作精美詞云流程
這篇文章主要介紹了使用Python來分析評論并且提取其中的關(guān)鍵詞,用于制作精美詞云的方法,感興趣的朋友來看看吧2022-03-03Python中l(wèi)ambda表達(dá)式的用法示例小結(jié)
本文主要展示了一些lambda表達(dá)式的使用示例,通過這些示例,我們可以了解到lambda表達(dá)式的常用語法以及使用的場景,感興趣的朋友跟隨小編一起看看吧2024-04-04詳解Numpy中的數(shù)組拼接、合并操作(concatenate, append, stack, hstack, vstac
這篇文章主要介紹了詳解Numpy中的數(shù)組拼接、合并操作(concatenate, append, stack, hstack, vstack, r_, c_等),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05