利用python 实现微信公众号群发图片与文本消息功能
在微信公众号开发中,使用api都要附加access_token内容。因此,首先需要获取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']
在群发图片时,需要提供已经上传图片的media_id。注意,群发图片的时候,必须使用接口:https://api.weixin.qq.com/cgi-bin/material/add_material 。
#获取上传文件的media_ID
#群发图片的时候,必须使用该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']
订阅号进行群发,必须通过分组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']
需要选择一个分组进行群发,在这里我选择第一个有效的分组进行群发(即第一个分组用户数不为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
下面的代码用于群发文本消息,群发给第一个有效的分组:
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编码的时候不会对中文转码为unicode,否则群发的消息会显示为unicode码,不能正确显示
r=requests.post(url=url,data=json.dumps(pay_send_all,ensure_ascii=False,indent=2))#此处的必须指定此参数
result=r.json()
#根据返回码的内容是否为0判断是否成功
return result['errcode']==0
下面的代码用于群发图片,群发给第一个有效的分组。
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()
#根据返回码的内容是否为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
#群发图片的时候,必须使用该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
#根据返回码的内容是否为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编码的时候不会对中文转码为unicode,否则群发的消息会显示为unicode码,不能正确显示
r=requests.post(url=url,data=json.dumps(pay_send_all,ensure_ascii=False,indent=2))#此处的必须指定此参数
result=r.json()
#根据返回码的内容是否为0判断是否成功
return result['errcode']==0
if(send_txt_to_first_group("祝你合家欢乐,幸福美满!")):
print 'success!'
else:
print 'fail!'
附录:在使用微信测试订阅号测试群发图片接口的时候,返回码如下:
{u'errcode': 45028, u'errmsg': u'has no masssend quota hint: [OKvFdA0813ge12]'}
这是因为测试订阅号没有群发图文消息的权限,并不是因为接口调用有误。
利用python 实现微信公众号群发图片与文本消息功能的更多相关文章
- Python webpy微信公众号开发之 回复图文消息
新建图文回复模板reply_pictext.xml: $def with (toUser,fromUser,createTime,title1,description1,picurl1,url1)&l ...
- python编写微信公众号首图思路详解
前言 之前一直在美图秀秀调整自己的微信公众号首图,效果也不尽如人意,老是调来调去,最后发出来的图片被裁剪了一大部分,丢失部分关键信息,十分恼火,于是想着用python写一个程序,把微信公众号首图的模式 ...
- C#实现微信公众号群发消息(解决一天只能发一次的限制)
经过几天研究网上的代码和谢灿大神的帮忙,今天终于用C#实现了微信公众号群发消息,现在整理一下. 总体思路:1.首先必须要在微信公众平台上申请一个公众号. 2.然后进行模拟登陆.(由于我对http传输原 ...
- 微信公众号通过图片选取接口上传到阿里oss
前言 之前写过一篇微信JS-SDK的使用方法,可进行参考 https://www.cnblogs.com/fozero/p/10256862.html 配置并调用公众号接口权限 1.配置权限微信公众号 ...
- python利用wxpy监控微信公众号
此次利用wxpy可以进行微信公众号的消息推送监测(代码超级简单),这样能进行实时获取链接.但是不光会抓到公众号的消息,好友的消息也会抓到(以后会完善的,毕竟现在能用了,而且做项目的微信号肯定是没有好友 ...
- python之微信公众号开发(基本配置和校验)
前言 最近有微信公众号开发的业务,以前没有用python做过微信公众号开发,记录一下自己的学习和开发历程,共勉! 公众号类型 订阅号 普通订阅号 认证订阅号 服务号 普通服务号 认证服务号 服务方式 ...
- python爬微信公众号前10篇历史文章(1)-思路概览
作为程序员,要时刻保持一颗好奇心和想要学习的姿态. 练习怎样利用搜狗微信爬取某指定微信公众号的历史文章.爬取微信公众号本身难度非常大,感谢搜狗提供了一个可以爬取数据的平台. 代码部分参考于: http ...
- 细数Python Flask微信公众号开发中遇到的那些坑
最近两三个月的时间,断断续续边学边做完成了一个微信公众号页面的开发工作.这是一个快递系统,主要功能有用户管理.寄收件地址管理.用户下单,订单管理,订单查询及一些宣传页面等.本文主要细数下开发过程中遇到 ...
- [python]通过微信公众号“Python程序员”,编写python代码
今天发现微信公众号中,居然可以编写python代码,很是惊喜,觉得蛮有趣的. 步骤如下: 1.关注微信公众号“Python程序员” 2.关注成功后,点击右下角的“潘多拉”->"Pyth ...
随机推荐
- meta--------link
<meta http-equiv="refresh" content="20">//每二十秒刷新一次页面: <meta http-equiv= ...
- 一个web项目在myeclipse中add deployment时无法被识别出来的原因
当我们一个web项目,在myeclipse中,add deployment时,可能发现,根本无法被识别成web项目,可能的原因有: 1. 项目的properties ->Myeclipse ...
- alt+shift+j,添加日期、作者等
在preference->Java->codestyle->codetemplates->commnets->type 可以编辑如: /** * @author ${us ...
- Struts2, jquery, select二级联动
1. 下载jquery.js文件放在webroot下js文件夹里 2. 配置struts.xml: <package name="default" namespace=&qu ...
- 网络层 IP 协议首部格式与其配套使用的四个协议(ARP,RARP,ICMP,IGMP)
目录 IP协议首部格式地址解析协议 ARP逆向地址解析协议 RARP网际控制报文协议 ICMP网际组管理协议IGMP IP 数据报首部 IP数据报首部格式: 最高位在左边,记为0 bit:最低位在右边 ...
- URL 传+号到后台变空格问题解决方案
今天巧合遇到这个问题,下面是网上找的解决方案. 原文:http://blog.sina.com.cn/s/blog_a0949eec01010xta.html 今天在调试客户端向服务器传递参数时,参数 ...
- android脚步---数字时钟和模拟时钟
时钟UI组件是两个非常简单的组件,分为Digitalclock 和Analogclock, main.xml文件,书中程序有问题,加了两个组件,一个Button和一个<Chronometer ...
- [转] Linux下移动virtualbox虚拟硬盘丢失eth0
1.遇到什么的问题(What) 在新的virtualbox虚拟机上挂上曾使用过的虚拟硬盘,在启动的时候,发现找不到网卡eth0, 在输入ifconfig –a的时候,也没有任何Ethnet的 ...
- PAT (Advanced Level) 1042. Shuffling Machine (20)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PHP文件夹操作
文件:文件+目录 判断文件类型: filetype("路径"); //返回一个字符串 is_dir("路径"); //如果是目录会返回true 判断文件是不是目 ...