利用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 ...
随机推荐
- java工程开发之图形化界面之(第一课)
下面我们先上代码: package 一个事例图形小应用程序; import javax.swing.JApplet; import java.awt.Graphics; public class 绘制 ...
- 一个有意思的 hta 程序 (html application)
哈哈,刚才同事给我讲了一个hta 程序,他自己说最近在学html5 开发坦克大战,不错,这种好奇心, 好学的精神值得我这个程序员学习,感觉他的视野面比我这个程序员还广,有点小惭愧. 什么是hta 呢? ...
- PAT (Advanced Level) 1053. Path of Equal Weight (30)
简单DFS #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- POJ Sudoku 数独填数 DFS
题目链接:Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18105 Accepted: 8772 Sp ...
- php使用curl设置超时的重要性
原文:http://phpquan.com/lamp/php/php-curl-timeout/ 网站登录不了,原因是没有可用的 PHP 子进程来响应新的请求了.这可能是是由于PHP-curl 没有 ...
- jQuery常用的查找Dom元素方法
废话不多说,先来个总结,然后下面是demo 一. 同级节点之间的检索(检索深度N=0) next()是在兄弟节点中,往后匹配; prev()是在兄弟节点中,往前匹配. 二. 父级/子级节点的检索(检索 ...
- 基于css3的环形动态进度条(原创)
基于css3实现的环形动态加载条,也用到了jquery.当时的想法是通过两个半圆的转动,来实现相应的效果,其实用css3的animation也可以实现这种效果.之所以用jquery是因为通过jquer ...
- Keil C动态内存管理机制分析及改进(转)
源:Keil C动态内存管理机制分析及改进 Keil C是常用的嵌入式系统编程工具,它通过init_mempool.mallloe.free等函数,提供了动态存储管理等功能.本文通过对init_mem ...
- (一)phoneGap之环境搭建教程及其example分析
phoneGap之环境搭建教程及其example分析 一.环境搭建 与普通的开发android应用一样,phoneGap也同于原生android应用一样,环境相同,只是有部分不同,下面就我做理解,进行 ...
- 路过Haxe
刚才在看Nape的时候,看到Haxe的代码,意外的感觉到亲切. 因为之前写过as2代码,最近学习了python,所以对haxe看起来很亲切,于是路过一下写了个HelloWorld. 另外,估计很长时间 ...