def whentextmsg(self, wxaccess): """ 文本消息处理 """ # 对于文本消息,调用handlemessage之后放回处理结果 wxaccess.context.wxaccess = wxaccess wxaccess.context.adapter = WXAdapter(wxaccess) msg = handlemessage(wxaccess.fromuser, wxaccess.get_textmsg(), wxaccess.context) if type(msg) != type((1,)): if len(msg.encode("utf-8")) > WX_TXTMSG_MAXLEN: # 需要截断 try: bts = msg.encode("utf-8") ix = bts.rfind("\n", 0, WX_TXTMSG_MAXLEN - 80) if ix > 0: rmsg = msg[0:ix] msg = '%s\n<a href="http://bulubulubuluz.sintrb.com/showmsg/%s/%s">显示更多消息...</a>' % ( rmsg.decode("utf-8"), wxaccess.fromuser, wxaccess.context.msgobj["time"], ) except: pass return wxaccess.response_textmessage(msg) else: return msg[1]
def POST(self): self.ctx.adapter = WebAdapter() if not web.cookies().get('usertoken'): usertk = hashlib.sha1(gentoken(l=20)).hexdigest() web.setcookie('usertoken', usertk, 3600 * 24 * 365) user = usertk whensubscribeevent(usertk, 'web', self.ctx) else: user = web.cookies().get('usertoken') msg = web.input(message='?').message res = handlemessage(user, msg, self.ctx) ret = { 'status': True, 'message': res if type(res) != type((0,)) else res[1] } return json.dumps(ret)