def POST(self): try: webData = web.data() mylog = log.Mylog() mylog.printToLog("########\n[info]Handle Post webdata is " + webData.decode('utf-8') + "\n########\n") recMsg = receive.parse_xml(webData.decode()) if isinstance(recMsg, receive.Msg): toUser = recMsg.FromUserName fromUser = recMsg.ToUserName if recMsg.MsgType == 'text': replyMsg = keyword_reply.parse_keyword(recMsg.Content) return replyMsg.send(toUser, fromUser) if recMsg.MsgType == 'image': mediaId = recMsg.MediaId replyMsg = reply.ImageMsg(toUser, fromUser, mediaId) return replyMsg.send() if recMsg.MsgType == 'link': replyMsg = custom_reply.parse_link(recMsg.Description) #url=shorter(recMsg.Url) #print(url) replyMsg.update(toUser, recMsg.Url) return replyMsg.send(toUser, fromUser) else: return reply.Msg().send() else: mylog.printToLog("暂且不处理") return reply.Msg().send() except Exception as Argument: return Argument
def shorter(url): mylog = log.Mylog() key = '5eb55591b1b63c4a8d3cc637@10a7d0aa6c326884b44da4db8e64ae53' params = {"url": url, "key": key} apiurl = "http://suo.im/api.htm?format=json&expireDate=2020-05-21" response = requests.request("GET", apiurl, params=params) result = response.json() linkUrl = result['url'] mylog.printToLog("********\n[info]Shorter response " + response.content.decode() + "\n********\n") if len(linkUrl) == 0: return url return result['url']
class CustReply(object): mylog = log.Mylog() jsondata = data.MyData() def __init__(self): pass def send(self, toUser, fromUserName): content = "您发送的链接暂时无法互助" replyMsg = reply.TextMsg(toUser, fromUserName, content) return replyMsg.send() def update(self, User, linkUrl): pass
class KeyWordReply(object): my_log = log.Mylog() json_data = data.MyData() def __init__(self, content): pass def send(self, toUser, fromUserName): content = '''请选择以下关键字进行回复: 【天气预报】:请发送“城市+天气”,如:“北京+天气” 【国家地理每日一图】:请发送“每日一图” 暂不支持其他关键字,感谢使用! ''' reply_msg = reply.TextMsg(toUser, fromUserName, content) return reply_msg.send()
def replyHtml(type, index): if type == 'yqb': jsondata = data.MyData() contentlist = jsondata.readData("urls" + str(index) + ".json") mylog = log.Mylog() num = 1 Content = "<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><h1>请点击以下链接助力:</h1>\n" for k in contentlist.keys(): #print("key is"+k+"\n") if num % 10 == 1: Content = Content + "<br />\n" Content = Content + "<p><a href=\"" + contentlist[ k] + "\">[No. " + str(num) + "]点这里助力~</a></p>\n" num = num + 1 Content = Content + "<p>感谢使用!</p></html>" mylog.printToLog("********\n[info]Reply html is " + Content + "\n********\n") return (Content) else: return ( "<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><h1>互助页面,暂只支持壹钱包点亮城市</h1></html>" )