加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
handle.py 2.81 KB
一键复制 编辑 原始数据 按行查看 历史
GuLu 提交于 2022-09-19 11:26 . python实现
# -*- coding: utf-8 -*-
# filename: handle.py
import hashlib
import web
import event
import receive
import time
import os
class Handle(object):
def __init__(self):
# os.path.dirname功能:去掉文件名,返回目录
# __file__表示了当前文件的path
self.app_root = os.path.dirname(__file__)
# os.path.join()函数用于路径拼接文件路径,可以传入多个路径。
self.templates_root = os.path.join(self.app_root, 'templates')
self.render = web.template.render(self.templates_root)
def GET(self):
try:
data = web.input()
print(data)
if len(data) == 0:
return "hello, this is handle view"
signature = data.signature
timestamp = data.timestamp
nonce = data.nonce
echostr = data.echostr
token = "token"
list = [token, timestamp, nonce]
list.sort()
s = list[0] + list[1] + list[2]
hashcode = hashlib.sha1(s.encode('utf-8')).hexdigest()
print("handle/GET func: hashcode, signature: ", hashcode, signature)
if hashcode == signature:
return echostr
else:
return echostr
except Exception as Argument:
return Argument
def POST(self):
try:
webData = web.data()
print("Handle Post webdata is:\n", webData)
# 打印消息体日志
recMsg = receive.parse_xml(webData)
# 文字类消息
if isinstance(recMsg, receive.Msg) and recMsg.MsgType == 'text':
toUser = recMsg.FromUserName
fromUser = recMsg.ToUserName
content = "这是一个demo(只回复text类消息)" + str(recMsg.Content)
print('Reply message info:\n')
print('toUser =', toUser)
print('fromUser = ', fromUser)
print('content = ', content)
return self.render.reply_text(toUser, fromUser, int(time.time()), content)
# 菜单点击类信息
elif isinstance(recMsg, receive.Event) and recMsg.MsgType == 'event':
toUser = recMsg.FromUserName
fromUser = recMsg.ToUserName
eventKey = recMsg.Eventkey
content = event.checkEvent_id(eventKey)
print('Reply message info:\n')
print('toUser =', toUser)
print('fromUser = ', fromUser)
print('content = ', content)
return self.render.reply_text(toUser, fromUser, int(time.time()), content)
else:
print("不支持的消息类型:", recMsg.MsgType)
return "success"
except Exception as Argment:
return Argment
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化