Python 微信公众号发送消息
2017-12-28 16:18
211 查看
1. 公众号测试地址
https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
2. 代码
# pip3 install requests import requests import json def get_access_token(): """ 获取微信全局接口的凭证(默认有效期俩个小时) 如果不每天请求次数过多, 通过设置缓存即可 """ result = requests.get( url="https://api.weixin.qq.com/cgi-bin/token", params={ "grant_type": "client_credential", "appid": "wx2499da7621f818e8", "secret": "6239e3dfc5af686777ea40b9f3df5f48", } ).json() if result.get("access_token"): access_token = result.get('access_token') else: access_token = None return access_token def sendmsg(openid,msg): access_token = get_access_token() body = { "touser": openid, "msgtype": "text", "text": { "content": msg } } response = requests.post( url="https://api.weixin.qq.com/cgi-bin/message/custom/send", params={ 'access_token': access_token }, data=bytes(json.dumps(body, ensure_ascii=False), encoding='utf-8') ) # 这里可根据回执code进行判定是否发送成功(也可以根据code根据错误信息) result = response.json() print(result) if __name__ == '__main__': sendmsg('关注者的ID','发送消息内容')
相关文章推荐
- python实现给微信公众号发送消息的方法
- 使用C# .net开发微信公众号之给用户发送模板消息
- python实战系列之RTX发送告警消息(四)
- 利用python 实现微信公众号群发图片与文本消息功能
- 微信公众号客服接口给指定用户openid发送消息
- C# 调用微信公众号接口发送客服消息示例
- 【微信开发系列】1. python开发微信公众号消息回复开发者模式
- 使用python发送qq消息
- python:使用udp协议发送飞秋消息
- python实现jms客户端(发送接收消息)
- python 发送 iOS APNS push 消息
- Python UDP协议,冒充飞Q发送消息
- RabbitMQ(python实现)学习之二:Producer发送消息至多个消息队列queue(广播消息)
- python、rabbitmq、pika、消息发送和接收实例
- 向微信公众号发送消息
- Django开发微信公众号(5) -- 实现消息的接收与发送
- 微信开发 python django 多客服接口发送到客户端的消息乱码
- [Python]实现XMPP协议即时通讯发送消息功能
- 微信公众号发送中奖模板消息
- 向微信公众号发送么个特地消息给么个人