本文整理汇总了Python中wechat_sdk.WechatBasic.get_message方法的典型用法代码示例。如果您正苦于以下问题:Python WechatBasic.get_message方法的具体用法?Python WechatBasic.get_message怎么用?Python WechatBasic.get_message使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wechat_sdk.WechatBasic
的用法示例。
在下文中一共展示了WechatBasic.get_message方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: entrance
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import get_message [as 别名]
def entrance(request):
def get_access_token():
url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+APP_ID+'&secret='+APP_SECRET
access_token = get_rsp_data(url)['access_token']
return access_token
def get_user_info():
access_token = get_access_token()
url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='+access_token+'&openid='+message.source
ueser_info_dic = get_rsp_data(url)
user_info = ""
for k, v in ueser_info_dic.items():
user_info = user_info+str(k)+":"+str(v)+"\n"
return user_info
def OAuth():
REDIRECT_URL = 'http://115.159.160.143/oauth'
url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+APP_ID+'&redirect_uri='+REDIRECT_URL+'&response_type=code&scope=snsapi_userinfo&state=2#wechat_redirect=0'
rsp = '<a href="'+url+'">点我授权</a>'
return rsp
wechat = WechatBasic(token=TOKEN)
if wechat.check_signature(signature=request.GET['signature'],
timestamp=request.GET['timestamp'],
nonce=request.GET['nonce']):
if request.method == 'GET':
rsp = request.GET.get('echostr', 'error')
else:
wechat.parse_data(request.body)
message = wechat.get_message()
rsp = wechat.response_text(OAuth())
else:
rsp = "failed"
return HttpResponse(rsp)
示例2: weixin
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import get_message [as 别名]
def weixin(request):
wechat = WechatBasic(token=token)
if wechat.check_signature(signature=request.GET['signature'],
timestamp=request.GET['timestamp'],
nonce=request.GET['nonce']):
if request.method == 'GET':
rsp = request.GET.get('echostr', 'error')
else:
wechat.parse_data(request.body)
message = wechat.get_message()
# write_log(message.type)
if message.type == u'text':
response_content = 'this is text'
# response_content = handle_text(message.content)
if message.content == u'wechat':
# response_content = 'hello wechat'
rsp = wechat.response_text(u'^_^')
elif re.findall(u'锐捷', message.content):
rsp = wechat.response_news(ruijie_response)
else:
rsp = wechat.response_news(default_response)
else:
# rsp = wechat.response_text(u'消息类型: {}'.format(message.type))
rsp = wechat.response_news(default_response)
else:
rsp = wechat.response_text('check error')
return HttpResponse(rsp)
示例3: index
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import get_message [as 别名]
def index(request):
signature = request.GET.get('signature')
timestamp = request.GET.get('timestamp')
nonce = request.GET.get('nonce')
xml = request.body
body_text = """
<xml>
<ToUserName><![CDATA[touser]]></ToUserName>
<FromUserName><![CDATA[fromuser]]></FromUserName>
<CreateTime>1405994593</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[新闻]]></Content>
<MsgId>6038700799783131222</MsgId>
</xml>
"""
token = get_weixin_accesstoken()
# 实例化 wechat
wechat = WechatBasic(token=token)
# 对签名进行校验
if wechat.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
# 对 XML 数据进行解析 (必要, 否则不可执行 response_text, response_image 等操作)
wechat.parse_data(body_text)
# 获得解析结果, message 为 WechatMessage 对象 (wechat_sdk.messages中定义)
message = wechat.get_message()
response = None
if isinstance(message, TextMessage):
response = wechat.response_text(content=u'文字信息')
elif isinstance(message, VoiceMessage):
response = wechat.response_text(content=u'语音信息')
elif isinstance(message, ImageMessage):
response = wechat.response_text(content=u'图片信息')
elif isinstance(message, VideoMessage):
response = wechat.response_text(content=u'视频信息')
elif isinstance(message, LinkMessage):
response = wechat.response_text(content=u'链接信息')
elif isinstance(message, LocationMessage):
response = wechat.response_text(content=u'地理位置信息')
elif isinstance(message, EventMessage): # 事件信息
if message.type == 'subscribe': # 关注事件(包括普通关注事件和扫描二维码造成的关注事件)
if message.key and message.ticket: # 如果 key 和 ticket 均不为空,则是扫描二维码造成的关注事件
response = wechat.response_text(content=u'用户尚未关注时的二维码扫描关注事件')
else:
response = wechat.response_text(content=u'普通关注事件')
elif message.type == 'unsubscribe':
response = wechat.response_text(content=u'取消关注事件')
elif message.type == 'scan':
response = wechat.response_text(content=u'用户已关注时的二维码扫描事件')
elif message.type == 'location':
response = wechat.response_text(content=u'上报地理位置事件')
elif message.type == 'click':
response = wechat.response_text(content=u'自定义菜单点击事件')
elif message.type == 'view':
response = wechat.response_text(content=u'自定义菜单跳转链接事件')
elif message.type == 'templatesendjobfinish':
response = wechat.response_text(content=u'模板消息事件')
# 现在直接将 response 变量内容直接作为 HTTP Response 响应微信服务器即可,此处为了演示返回内容,直接将响应进行输出
print response
return render_to_response('index.html',response)
示例4: validate
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import get_message [as 别名]
def validate():
signature = request.args.get('signature')
echostr = request.args.get('echostr')
timestamp = request.args.get('timestamp')
nonce = request.args.get('nonce')
token = "test"
body_text = request.get_data()
# 实例化 wechat
wechat = WechatBasic(token=token)
# 对签名进行校验
if wechat.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
# 对 XML 数据进行解析 (必要, 否则不可执行 response_text, response_image 等操作)
wechat.parse_data(body_text)
# 获得解析结果, message 为 WechatMessage 对象 (wechat_sdk.messages中定义)
message = wechat.get_message()
response = None
if message.type == 'subscribe':
response = wechat.response_text("谢谢关注V2EX热帖分享,输入任意内容以获取最新热帖咨询")
if message.type == 'text':
newsList = generate()
response = wechat.response_news(newsList)
# 现在直接将 response 变量内容直接作为 HTTP Response 响应微信服务器即可,此处为了演示返回内容,直接将响应进行输出
return response
示例5: post
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import get_message [as 别名]
def post(self):
# 实例化 wechat
wechat = WechatBasic(token=token)
signature = self.get_argument("signature", None)
timestamp = self.get_argument("timestamp", None)
nonce = self.get_argument("nonce", None)
# 对签名进行校验
if wechat.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
# 对 XML 数据进行解析 (必要, 否则不可执行 response_text, response_image 等操作)
# look request method
# print dir(self.request)
content = self.request.body
wechat.parse_data(content)
# 获得解析结果, message 为 WechatMessage 对象 (wechat_sdk.messages中定义)
message = wechat.get_message()
response = None
if message.type == 'text':
if message.content == 'wechat':
response = wechat.response_text(u'''
<a href="https://open.weixin.qq.com/connect/oauth2/authorize?
appid=wxafac6b4bc457eb26&redirect_uri=http://tuteng.info/login_access&
response_type=code&scope=snsapi_userinfo&state=14#wechat_redirect">testtt</a>
''')
else:
response = wechat.response_text(u'world')
elif message.type == 'image':
response = wechat.response_text(u'image')
else:
response = wechat.response_text(u'no image')
示例6: weixin_response
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import get_message [as 别名]
def weixin_response(token, signature, timestamp, nonce, body_text=''):
debug_log('check sign ...\n')
# 用户的请求内容 (Request 中的 Body)
# 请更改 body_text 的内容来测试下面代码的执行情况
# 实例化 wechat
wechat = WechatBasic(token=token)
# 对签名进行校验
if wechat.check_signature(signature, timestamp, nonce):
# 对 XML 数据进行解析 (必要, 否则不可执行 response_text, response_image 等操作)
wechat.parse_data(body_text)
# 获得解析结果, message 为 WechatMessage 对象 (wechat_sdk.messages中定义)
message = wechat.get_message()
response = None
if message.type == 'text':
if message.content == 'wechat':
response = wechat.response_text(u'^_^')
else:
response = wechat.response_text(u'文字')
elif message.type == 'image':
response = wechat.response_text(u'图片')
else:
response = wechat.response_text(u'未知')
# 现在直接将 response 变量内容直接作为 HTTP Response 响应微信服务器即可,此处为了演示返回内容,直接将响应进行输出
return response
return HttpResponse('')
示例7: api_wechat
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import get_message [as 别名]
def api_wechat(request):
# 从 request 中提取基本信息 (signature, timestamp, nonce, xml)
signature = request.GET.get('signature')
timestamp = request.GET.get('timestamp')
nonce = request.GET.get('nonce')
xml = request.body
# 实例化 WechatBasic 并检验合法性
wechat_instance = WechatBasic(token='CCPL')
# if not wechat_instance.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
# return HttpResponseBadRequest('Verify Failed')
# 解析本次请求的 XML 数据
try:
wechat_instance.parse_data(data=xml)
message = wechat_instance.get_message() # 获取解析好的微信请求信息
print message
except ParseError:
print xml
return HttpResponseBadRequest('Invalid XML Data')
ret = u'感谢关注“心理地图”!欢迎您登陆我们的网站:http://ccpl.psych.ac.cn/PsyMap/ (网站功能还在开发中,敬请期待。)' #message.type
response = wechat_instance.response_text(ret)
return HttpResponse(response)
示例8: wechatChatForFun
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import get_message [as 别名]
def wechatChatForFun(request):
signature = request.GET.get('signature')
timestamp = request.GET.get('timestamp')
nonce = request.GET.get('nonce')
wechat_instance = WechatBasic(conf=ChatForFunConf)
reply_text = ""
if not wechat_instance.check_signature(
signature=signature, timestamp=timestamp, nonce=nonce):
return HttpResponseBadRequest('Verify Failed')
else:
if request.method == 'GET':
response = request.GET.get('echostr', 'error')
else:
try:
wechat_instance.parse_data(request.body)
message = wechat_instance.get_message()
if isinstance(message, TextMessage):
reply_text = str(message.content).encode('utf8')
reply_text = ParserMsg(reply_text, message.source)
elif isinstance(message, ImageMessage):
reply_text = str(message.picurl).encode('utf8')
reply_text = ParserMsg(reply_text, message.source)
elif isinstance(message, LocationMessage):
reply_text = str(message.label).encode('utf8')
reply_text = ParserMsg(reply_text, message.source)
else:
reply_text = U"服务器:目前只支持文字消息,非常抱歉"
response = wechat_instance.response_text(content=reply_text)
except ParseError:
return HttpResponseBadRequest("Invalid XML Data")
return HttpResponse(response, content_type='application/xml')
示例9: smart_entry
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import get_message [as 别名]
def smart_entry(request):
signature = request.REQUEST.get('signature', None)
timestamp = request.REQUEST.get('timestamp', None)
nonce = request.REQUEST.get('nonce', None)
# if it's account authenticate request
echostr = request.REQUEST.get('echostr', None)
if echostr:
return HttpResponse(echostr)
wechat = WechatBasic(token=settings.WECHAT_TOKEN, appid=settings.WECHAT_ACCOUNT)
# 对签名进行校验
if wechat.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
# 对 XML 数据进行解析 (必要, 否则不可执行 response_text, response_image 等操作)
body_text = request.body
wechat.parse_data(body_text)
# 获得解析结果, message 为 WechatMessage 对象 (wechat_sdk.messages中定义)
message = wechat.get_message()
response = None
if isinstance(message, TextMessage):
response = wechat.response_text(content=u'文字信息')
elif isinstance(message, VoiceMessage):
response = wechat.response_text(content=u'语音信息')
elif isinstance(message, ImageMessage):
response = wechat.response_text(content=u'图片信息')
elif isinstance(message, VideoMessage):
response = wechat.response_text(content=u'视频信息')
elif isinstance(message, LinkMessage):
response = wechat.response_text(content=u'链接信息')
elif isinstance(message, LocationMessage):
response = wechat.response_text(content=u'地理位置信息')
elif isinstance(message, EventMessage): # 事件信息
if message.type == 'subscribe': # 关注事件(包括普通关注事件和扫描二维码造成的关注事件)
if message.key and message.ticket: # 如果 key 和 ticket 均不为空,则是扫描二维码造成的关注事件
response = wechat.response_text(content=u'用户尚未关注时的二维码扫描关注事件')
else:
response = wechat.response_text(content=u'普通关注事件')
elif message.type == 'unsubscribe':
response = wechat.response_text(content=u'取消关注事件')
elif message.type == 'scan':
response = wechat.response_text(content=u'用户已关注时的二维码扫描事件')
elif message.type == 'location':
response = wechat.response_text(content=u'上报地理位置事件')
elif message.type == 'click':
response = wechat.response_text(content=u'自定义菜单点击事件')
elif message.type == 'view':
response = wechat.response_text(content=u'自定义菜单跳转链接事件')
elif message.type == 'templatesendjobfinish':
response = wechat.response_text(content=u'模板消息事件')
return HttpResponse(response)
return HttpResponse("Not implemented yet")
示例10: index
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import get_message [as 别名]
def index():
token = settings.TOKEN
signature = request.args.get('signature', '')
timestamp = request.args.get('timestamp', '')
nonce = request.args.get('nonce', '')
# 实例化 wechat
wechat = WechatBasic(token=token)
if not wechat.check_signature(signature=signature,
timestamp=timestamp, nonce=nonce):
return 'fail'
# 对签名进行校验
echostr = request.args.get('echostr')
if echostr:
return echostr
wechat.parse_data(request.data)
message = wechat.get_message()
if message.type == 'text':
response = wechat.group_transfer_message()
elif message.type == 'image':
response = wechat.response_text(u'图片')
elif isinstance(message, EventMessage):
if message.type == 'subscribe':
if message.key and message.ticket:
scene = message.key.startswith(
'qrscene_') and message.key[8:] or 'default'
else:
scene = 'default'
SubscribeEvent.create_event(message.source, scene, message.time)
response = wechat.response_text(content=settings.GREETINGS)
elif message.type == 'unsubscribe':
UnsubscribeEvent.create_event(message.source, message.time)
# TODO
response = ''
elif message.type == 'scan':
# TODO
response = ''
elif message.type == 'location':
response = wechat.response_text(content=u'上报地理位置事件')
elif message.type == 'click':
content = settings.CLICK_MENU_TEXT_MAPPER.get(message.key, u'未知')
response = wechat.response_text(content=content)
elif message.type == 'view':
response = wechat.response_text(content=u'自定义菜单跳转链接事件')
elif message.type == 'templatesendjobfinish':
response = wechat.response_text(content=u'模板消息事件')
else:
response = wechat.response_text(u'未知')
return response
示例11: index
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import get_message [as 别名]
def index(request):
# 实例化 We_chat_Basic
we_chat = WechatBasic(token=WECHAT_TOKEN, appid=AppID, appsecret=AppSecret)
if request.method == "GET":
signature = request.GET.get("signature")
timestamp = request.GET.get("timestamp")
nonce = request.GET.get("nonce")
if not we_chat.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
return HttpResponse("Verify failed")
else:
return HttpResponse(request.GET.get("echostr"), content_type="text/plain")
else:
signature = request.GET.get("signature")
timestamp = request.GET.get("timestamp")
nonce = request.GET.get("nonce")
if not we_chat.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
return HttpResponse("Verify failed")
try:
we_chat.parse_data(data=request.body)
except ParseError:
return HttpResponseBadRequest("Invalid XML Data")
message = we_chat.get_message()
if isinstance(message, EventMessage):
if message.type == "click":
if message.key == "STEP_COUNT":
step_array = Record.objects.filter(user=message.source)
if step_array:
step = step_array[len(step_array) - 1].step
response = we_chat.response_text("跑了" + str(step) + "步咯")
# 里面的数字应由其他函数获取
return HttpResponse(response)
else:
response = we_chat.response_text("Sorry, there' no data about you in our database.")
return HttpResponse(response)
if message.key == "CHART":
print 1
response = we_chat.response_news(
[
{
"title": "Today's amount of exercise",
"description": "data analysis",
"picurl": "http://7xn2s5.com1.z0.glb.clouddn.com/ez.png",
"url": SERVER_IP + "TodayChart/" + message.source,
}
]
)
return HttpResponse(response)
response = we_chat.response_text("Cheer up!")
return HttpResponse(response)
示例12: index
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import get_message [as 别名]
def index(request):
# 实例化 We_chat_Basic
we_chat = WechatBasic(
token=WECHAT_TOKEN,
appid=AppID,
appsecret=AppSecret
)
if request.method == "GET":
signature = request.GET.get('signature')
timestamp = request.GET.get('timestamp')
nonce = request.GET.get('nonce')
if not we_chat.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
return HttpResponse("Verify failed")
else:
return HttpResponse(request.GET.get("echostr"), content_type="text/plain")
else:
signature = request.GET.get('signature')
timestamp = request.GET.get('timestamp')
nonce = request.GET.get('nonce')
if not we_chat.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
return HttpResponse("Verify failed")
try:
we_chat.parse_data(data=request.body)
except ParseError:
return HttpResponseBadRequest('Invalid XML Data')
message = we_chat.get_message()
if isinstance(message, EventMessage):
if message.type == 'click':
if message.key == 'STEP_COUNT':
step_array = Record.objects.filter(user=message.source)
if step_array:
step = step_array[len(step_array) - 1].step
response = we_chat.response_text(u'跑了' + str(step) + u'步咯')
# 里面的数字应由其他函数获取
return HttpResponse(response)
else:
response = we_chat.response_text(u'Sorry, there\' no data about you in our database.')
return HttpResponse(response)
if message.key == 'CHART':
print 1
response = we_chat.response_news([{
'title': u'Today\'s amount of exercise',
'description': 'data analysis',
'picurl': 'http://7xn2s5.com1.z0.glb.clouddn.com/ez.png',
'url': SERVER_IP + 'TodayChart/' + message.source}])
return HttpResponse(response)
response = we_chat.response_text(u'Cheer up!')
return HttpResponse(response)
示例13: wechat_auth
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import get_message [as 别名]
def wechat_auth():
wechat = WechatBasic(token=TOKEN)
if request.method == 'GET':
token = TOKEN # your token
query = request.args # GET 方法附上的参数
signature = query.get('signature', '')
timestamp = query.get('timestamp', '')
nonce = query.get('nonce', '')
echostr = query.get('echostr', '')
if wechat.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
return make_response(echostr)
else:
return 'Signature Mismatch'
else:
body_text=request.data
wechat.parse_data(body_text)
# 获得解析结果, message 为 WechatMessage 对象 (wechat_sdk.messages中定义)
message = wechat.get_message()
response = None
# 在这里解析 message.content 也就是用户发来的文字
if message.type == 'text':
if message.content.lower() in ('h', 'help'):
response = wechat.response_text(u'z 看知乎日报\nv 看 V2EX 十大\nh 为帮助\n输入其他文字与机器人对话 : )')
elif message.content == 'wechat':
response = wechat.response_text(u'^_^')
elif message.content[0:3] == 'test':
response = wechat.response_text(u'I\'m testing ' + message.content[4:])
elif (u'陆' or u'杰') in message.content:
response = wechat.response_text(u'爸爸是个天才')
elif (u'***' or u'内内') in message.content:
response = wechat.response_text(u'内内,为什么你名字那么难写')
elif message.content.upper() in ('V', 'V2EX'):
response = wechat.response_news(get_v2ex_news())
elif message.content.upper() in ('Z', 'ZHIHU'):
response = wechat.response_news(get_zhihudaily())
else:
response = wechat.response_text(get_turing(message.content))
elif message.type == 'image':
response = wechat.response_text(u'您发来了一个图片')
elif message.type == 'location':
response = wechat.response_text(u'您的位置我已收到')
elif message.type == 'subscribe':
response = wechat.response_text(u'oh...你居然关注了,其实我自己也不知道关注这个号有啥用.\nz 看知乎日报\nv 看 V2EX 十大\nh 为帮助\n输入其他文字与机器人对话 : )')
elif message.type == 'unsubscribe':
response = wechat.response_text(u'呜呜呜,爱我别走.....')
else:
response = wechat.response_text(u'未知类型。您发的是什么?')
return response
示例14: weixin_test
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import get_message [as 别名]
def weixin_test(request):
# 下面这些变量均假设已由 Request 中提取完毕
token = 'WECHAT_TOKEN' # 你的微信 Token
signature = 'f24649c76c3f3d81b23c033da95a7a30cb7629cc' # Request 中 GET 参数 signature
timestamp = '1406799650' # Request 中 GET 参数 timestamp
nonce = '1505845280' # Request 中 GET 参数 nonce
body_text = """
<xml>
<ToUserName><![CDATA[touser]]></ToUserName>
<FromUserName><![CDATA[fromuser]]></FromUserName>
<CreateTime>1405994593</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[锐捷]]></Content>
<MsgId>6038700799783131222</MsgId>
</xml>
"""
wechat = WechatBasic(token=token)
# 对签名进行校验
if wechat.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
# 对 XML 数据进行解析 (必要, 否则不可执行 response_text, response_image 等操作)
wechat.parse_data(body_text)
# 获得解析结果, message 为 WechatMessage 对象 (wechat_sdk.messages中定义)
message = wechat.get_message()
response = None
if message.type == 'text':
if message.content == 'wechat':
response = wechat.response_text(u'^_^')
else:
response = wechat.response_text(u'文字')
elif message.type == 'image':
response = wechat.response_text(u'图片')
else:
response = wechat.response_text(u'未知')
# 现在直接将 response 变量内容直接作为 HTTP Response 响应微信服务器即可,此处为了演示返回内容,直接将响应进行输出
print response
return HttpResponse(response)
示例15: WeChat
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import get_message [as 别名]
def WeChat(request):
signature = request.GET.get('signature')
timestamp = request.GET.get('timestamp')
nonce = request.GET.get('nonce')
# 实例化 WechatBasic 类
wechat_instance = WechatBasic(conf=conf)
# 验证微信公众平台的消息
if not wechat_instance.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
return HttpResponseBadRequest('Verify Failed')
else:
if request.method == 'GET':
response = request.GET.get('echostr', 'error')
else:
try:
wechat_instance.parse_data(request.body)
message = wechat_instance.get_message()
# 判断消息类型
if isinstance(message, TextMessage):
reply_text = 'text'
elif isinstance(message, VoiceMessage):
reply_text = 'voice'
elif isinstance(message, ImageMessage):
reply_text = 'image'
elif isinstance(message, LinkMessage):
reply_text = 'link'
elif isinstance(message, LocationMessage):
reply_text = 'location'
elif isinstance(message, VideoMessage):
reply_text = 'video'
elif isinstance(message, ShortVideoMessage):
reply_text = 'shortvideo'
else:
reply_text = 'other'
response = wechat_instance.response_text(content=reply_text)
except ParseError:
return HttpResponseBadRequest('Invalid XML Data')
return HttpResponse(response, content_type="application/xml")