欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)的微信公眾號(hào)群發(fā)圖片與文本消息功能實(shí)例詳解

 更新時(shí)間:2017年06月30日 09:05:18   作者:ZHOU YANG  
這篇文章主要介紹了Python實(shí)現(xiàn)的微信公眾號(hào)群發(fā)圖片與文本消息功能,結(jié)合實(shí)例形式詳細(xì)分析了Python調(diào)用微信接口實(shí)現(xiàn)微信公眾號(hào)群發(fā)圖片與文本消息的具體操作步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下

本文實(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ì)有所幫助。

相關(guān)文章

最新評(píng)論