Python實(shí)現(xiàn)的微信公眾號(hào)群發(fā)圖片與文本消息功能實(shí)例詳解
本文實(shí)例講述了Python實(shí)現(xiàn)的微信公眾號(hào)群發(fā)圖片與文本消息功能。分享給大家供大家參考,具體如下:
在微信公眾號(hào)開發(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ā)圖片時(shí),需要提供已經(jīng)上傳圖片的media_id。注意,群發(fā)圖片的時(shí)候,必須使用接口:https://api.weixin.qq.com/cgi-bin/material/add_material。
#獲取上傳文件的media_ID #群發(fā)圖片的時(shí)候,必須使用該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']
訂閱號(hào)進(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']
需要選擇一個(gè)分組進(jìn)行群發(fā),在這里我選擇第一個(gè)有效的分組進(jìn)行群發(fā)(即第一個(gè)分組用戶數(shù)不為0的分組)。
#返回第一個(gè)有效的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ā)給第一個(gè)有效的分組:
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編碼的時(shí)候不會(huì)對(duì)中文轉(zhuǎn)碼為unicode,否則群發(fā)的消息會(huì)顯示為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ā)給第一個(gè)有效的分組。
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ā)圖片的時(shí)候,必須使用該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'] #返回第一個(gè)有效的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編碼的時(shí)候不會(huì)對(duì)中文轉(zhuǎn)碼為unicode,否則群發(fā)的消息會(huì)顯示為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!'
附錄:在使用微信測(cè)試訂閱號(hào)測(cè)試群發(fā)圖片接口的時(shí)候,返回碼如下:
{u'errcode': 45028, u'errmsg': u'has no masssend quota hint: [OKvFdA0813ge12]'}
這是因?yàn)闇y(cè)試訂閱號(hào)沒有群發(fā)圖文消息的權(quán)限,并不是因?yà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)典教程》。
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python查看微信撤回消息代碼
- python如何查看微信消息撤回
- 使用Python微信庫itchat獲得好友和群組已撤回的消息
- 78行Python代碼實(shí)現(xiàn)現(xiàn)微信撤回消息功能
- python使用wxpy實(shí)現(xiàn)微信消息防撤回腳本
- Python 實(shí)現(xiàn)還原已撤回的微信消息
- python實(shí)現(xiàn)文件助手中查看微信撤回消息
- Python實(shí)現(xiàn)微信消息防撤回功能的實(shí)例代碼
- 使用Python制作自動(dòng)推送微信消息提醒的備忘錄功能
- python實(shí)現(xiàn)微信定時(shí)每天和女友發(fā)送消息
- Python實(shí)現(xiàn)微信中找回好友、群聊用戶撤回的消息功能示例
相關(guān)文章
keras 實(shí)現(xiàn)輕量級(jí)網(wǎng)絡(luò)ShuffleNet教程
這篇文章主要介紹了keras 實(shí)現(xiàn)輕量級(jí)網(wǎng)絡(luò)ShuffleNet教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06python通過opencv調(diào)用攝像頭操作實(shí)例分析
在本篇文章里小編給大家整理的是一篇關(guān)于python通過opencv調(diào)用攝像頭操作實(shí)例分析內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-06-06使用jupyter notebook將文件保存為Markdown,HTML等文件格式
這篇文章主要介紹了使用jupyter notebook將文件保存為Markdown,HTML等文件格式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04django如何根據(jù)現(xiàn)有數(shù)據(jù)庫表生成model詳解
這篇文章主要給大家介紹了關(guān)于django如何根據(jù)現(xiàn)有數(shù)據(jù)庫表生成model的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Django具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-08-08Python超簡(jiǎn)單分析評(píng)論提取關(guān)鍵詞制作精美詞云流程
這篇文章主要介紹了使用Python來分析評(píng)論并且提取其中的關(guān)鍵詞,用于制作精美詞云的方法,感興趣的朋友來看看吧2022-03-03Python中l(wèi)ambda表達(dá)式的用法示例小結(jié)
本文主要展示了一些lambda表達(dá)式的使用示例,通過這些示例,我們可以了解到lambda表達(dá)式的常用語法以及使用的場(chǎng)景,感興趣的朋友跟隨小編一起看看吧2024-04-04詳解Numpy中的數(shù)組拼接、合并操作(concatenate, append, stack, hstack, vstac
這篇文章主要介紹了詳解Numpy中的數(shù)組拼接、合并操作(concatenate, append, stack, hstack, vstack, r_, c_等),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05