django wechatpy建立微信简单自动回复
2017-05-23 14:16
926 查看
首先申请下微信公众平台账号,进入公众平台
4000
开发-基础配置界面
在你的项目中编写代码
记得先安装下wechatpy(pip install wechatpy)
from django.http.response import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from wechatpy import parse_message, create_reply
from wechatpy.exceptions import InvalidSignatureException
from wechatpy.utils import check_signature
WECHAT_TOKEN = 'your token'
@csrf_exempt
def wechat(request):
if request.method == 'GET':
signature = request.GET.get('signature', '')
timestamp = request.GET.get('timestamp', '')
nonce = request.GET.get('nonce', '')
echo_str = request.GET.get('echostr', '')
try:
check_signature(WECHAT_TOKEN, signature, timestamp, nonce)
except InvalidSignatureException:
echo_str = 'error'
response = HttpResponse(echo_str, content_type="text/plain")
return response
elif request.method == 'POST':
msg = parse_message(request.body)
if msg.type == 'text':
reply = create_reply('这是条文字消息', msg)
elif msg.type == 'image':
reply = create_reply('这是条图片消息', msg)
elif msg.type == 'voice':
reply = create_reply('这是条语音消息', msg)
else:
reply = create_reply('这是条其他类型消息', msg)
response = HttpResponse(reply.render(), content_type="application/xml")
return response
else:
logger.info('--------------------------------')
其中url配置如下:
然后提交服务器配置,并启用
进入微信公众号,随意发送一个信息
4000
开发-基础配置界面
在你的项目中编写代码
记得先安装下wechatpy(pip install wechatpy)
from django.http.response import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from wechatpy import parse_message, create_reply
from wechatpy.exceptions import InvalidSignatureException
from wechatpy.utils import check_signature
WECHAT_TOKEN = 'your token'
@csrf_exempt
def wechat(request):
if request.method == 'GET':
signature = request.GET.get('signature', '')
timestamp = request.GET.get('timestamp', '')
nonce = request.GET.get('nonce', '')
echo_str = request.GET.get('echostr', '')
try:
check_signature(WECHAT_TOKEN, signature, timestamp, nonce)
except InvalidSignatureException:
echo_str = 'error'
response = HttpResponse(echo_str, content_type="text/plain")
return response
elif request.method == 'POST':
msg = parse_message(request.body)
if msg.type == 'text':
reply = create_reply('这是条文字消息', msg)
elif msg.type == 'image':
reply = create_reply('这是条图片消息', msg)
elif msg.type == 'voice':
reply = create_reply('这是条语音消息', msg)
else:
reply = create_reply('这是条其他类型消息', msg)
response = HttpResponse(reply.render(), content_type="application/xml")
return response
else:
logger.info('--------------------------------')
其中url配置如下:
url(r'^wechat', wechat, name='wechat')说明http://www.zhangpengpeng.cn/wechat指向上述方法
然后提交服务器配置,并启用
进入微信公众号,随意发送一个信息
相关文章推荐
- 学习微信官方代码--简单自动回复
- 微信机器人:小蜗牛有道翻译小助手——Django + SAE + 微信公众帐号自动回复开放接口
- 微信平台ASPX高级定制开发(一):如何使用C#建立响应微信接入和自动回复的代码
- JAVA实现 SpringMVC方式的微信接入、实现简单的自动回复功能
- 微信----简单自动回复功能emoji和文本处理demo
- PHP微信开发---简单的文本自动回复
- PHP微信开发, 简单的文本自动回复
- 微信机器人:小蜗牛有道翻译小助手——Django + SAE + 微信公众帐号自动回复开放接口
- 微信公共平台开发:简单的自动回复
- 微信机器人:小蜗牛有道翻译小助手——Django + SAE + 微信公众帐号自动回复开放接口
- python脚本设置微信自动回复[简单应用]
- Python包装网页微信API并实现简单自动回复
- Python实现微信自动回复机器人(简单)
- 超简单微信公众帐号自动回复和天气播报功能应用
- 微信简单文本回复
- 微信公众平台源码,微信机器人源码,微信自动回复源码
- 微信开发之关注自动回复
- springMVC调微信接口实现关注时消息回复和自动回复功能
- wordpress 插件 之 微信自动回复机器人
- 公众平台自动回复支持插入微信卡券 多图文消息可嵌入卡券