Пример #1
0
    def tick(self):
        if len(handle_queue) == 0:
            return

        recMsg = handle_queue.popleft()
        toUser = recMsg.FromUserName
        fromUser = recMsg.ToUserName
        replyMsg = reply.Msg(toUser, fromUser)
        if isinstance(recMsg, receive.TextMsg):
            content = recMsg.Content
            response = get_content(content)
            msgType = response['type']
            content = response['content']
            if msgType == 'text':
                replyMsg = reply.TextMsg(toUser, fromUser, content)
            elif msgType == 'news':
                replyMsg = reply.NewsMsg(toUser, fromUser, response['title'],
                                         response['content'],
                                         response['pic_url'], response['url'])
        elif isinstance(recMsg, receive.ImageMsg):
            pass
        elif isinstance(recMsg, receive.EventMsg):
            if recMsg.Event == 'subscribe':
                content = config.Welcome.format(key=get_keywords())
                replyMsg = reply.TextMsg(toUser, fromUser, content)

        sender_queue.append(replyMsg)
Пример #2
0
def wechat_auth():
    try:
        if request.method == 'GET':
            data = request.args
            print('Coming Get', data)
            if not data:
                return ''

            test = data.get('test', '')
            if test != '':
                content = get_content(test)
                return content['content']

            signature = data.get('signature', '')
            if signature == '':
                return 'error'

            timestamp = data.get('timestamp', '')
            nonce = data.get('nonce', '')
            echostr = data.get('echostr', '')
            s = [timestamp, nonce, token]
            s.sort()
            s = ''.join(s).encode('utf8')
            if (hashlib.sha1(s).hexdigest() != signature):
                return 'failed'

            return make_response(echostr)

        if request.method == 'POST':
            xml_str = request.stream.read()
            # print('Coming Post', xml_str)
            recMsg = receive.parse_xml(xml_str)
            toUser = recMsg.FromUserName
            fromUser = recMsg.ToUserName
            replyMsg = reply.Msg(toUser, fromUser)
            if isinstance(recMsg, receive.TextMsg):
                content = recMsg.Content
                response = get_content(content)
                msgType = response['type']
                content = response['content']
                if msgType == 'text':
                    replyMsg = reply.TextMsg(toUser, fromUser, content)
                elif msgType == 'news':
                    replyMsg = reply.NewsMsg(toUser, fromUser,
                                             response['title'],
                                             response['content'],
                                             response['pic_url'],
                                             response['url'])
            elif isinstance(recMsg, receive.ImageMsg):
                pass
            elif isinstance(recMsg, receive.EventMsg):
                if recMsg.Event == 'subscribe':
                    content = config.Welcome.format(key=get_keywords())
                    replyMsg = reply.TextMsg(toUser, fromUser, content)

            return replyMsg.send()

    except Exception as e:
        print(str(e))
        return ''
Пример #3
0
 def POST(self):
     webData = web.data()
     util.lstr("Handle Post webdata is: ")
     util.lstr(webData)
     # print(type(webData))  # str
     recMsg = receive.parse_xml(webData)
     if isinstance(recMsg, receive.Msg):
         toUser = recMsg.FromUserName
         fromUser = recMsg.ToUserName
         if recMsg.MsgType == 'text':
             # info = 'empty'
             # content = '收到问题: ' + recMsg.Content + ' info: ' + info
             content = self.process_txt(recMsg.Content)
             replyMsg = reply.TextMsg(toUser, fromUser, content.encode('utf-8'))
             return replyMsg.send()
         if recMsg.MsgType == 'image':
             # 发送信息
             # content = self.process_txt('image')
             # replyMsg = reply.TextMsg(toUser, fromUser, content.encode('utf-8'))
             # return replyMsg.send()
             replyMsg = reply.NewsMsg(toUser, fromUser, 1, '这可能是苹果白粉病', '苹果白粉病(Apple powdery mildew)在我国苹果产区发生普遍。除为害苹果外,还为害梨、沙果、海棠、槟子和山定子等,对山定子实生苗、小苹果类的槟沙果、海棠和苹果中的倭锦、祝、红玉、国光等品种为害重。', 'http://g.hiphotos.baidu.com/baike/w%3D268%3Bg%3D0/sign=442e0372f2d3572c66e29bdab228041a/18d8bc3eb13533facc74c7b5a8d3fd1f40345b6e.jpg', 'http://baike.baidu.com/item/%E8%8B%B9%E6%9E%9C%E7%99%BD%E7%B2%89%E7%97%85/10248498?fr=aladdin')
             return replyMsg.send()
         if recMsg.MsgType == 'voice':
             content = '用户: ' + recMsg.FromUserName + ' 发送了语音,转化为文字为: ' + recMsg.Recognition
             replyMsg = reply.TextMsg(toUser, fromUser, content)
             return replyMsg.send()
         else:
             return reply.Msg().send()
     else:
         util.lstr("暂且不处理")
         return reply.Msg().send()
Пример #4
0
    def POST(self):
        try:
            webData = web.data()
            print("Handle Post webData is ", webData)

            recMsg = receive.parse_xml(webData)
            if isinstance(recMsg, receive.Msg):
                toUser = recMsg.FromUserName
                fromUser = recMsg.ToUserName
                if recMsg.MsgType == 'text':
                    content = response(recMsg.Content)
                    if content == '':
                        content = '没有查询到序号对应内容\n回复0可查询菜单主页'
                    if isinstance(content, dict):
                        responseType = content['type']
                        if responseType == 'text':
                            replyMsg = reply.TextMsg(toUser, fromUser,
                                                     content['content'])
                        elif responseType == 'image':
                            replyMsg = reply.ImageMsg(toUser, fromUser,
                                                      content['id'])
                        elif responseType == 'news':
                            replyMsg = reply.NewsMsg(toUser, fromUser,
                                                     **content)
                    else:
                        replyMsg = reply.TextMsg(toUser, fromUser, content)
                    return replyMsg.send()
                elif recMsg.MsgType == 'image':
                    mediaId = recMsg.MediaId
                    replyMsg = reply.ImageMsg(toUser, fromUser, mediaId)
                    return replyMsg.send()
                elif recMsg.MsgType == 'event':
                    if recMsg.EventType == 'subscribe':  #订阅事件
                        pre = '欢迎关注无名小屋\n'
                        content = response('0') + '\n'
                        end = '回复序号即可获取下级菜单或详情'
                        content = pre + content + end
                        replyMsg = reply.TextMsg(toUser, fromUser, content)
                        return replyMsg.send()
            else:
                print('不处理')
                return 'success'

        except BaseException as e:
            print('Error:', e)
            return 'Error'
Пример #5
0
 def DoAction(self, actions, toUser, fromUser):
     ret = ''
     if not actions:
         ret = 'no action'
     elif len(actions) > 1:
         ret = 'too many actions'
     else:
         # actions[0].debugMsg()
         if actions[0]._type == 'ShowPlainText':
             ret = actions[0]._text.encode('utf-8')
         elif actions[0]._type == 'ShowNewsText':
             articles = []
             objs = json.loads(actions[0]._text)
             for obj in objs:
                 articles.append(reply.Article(**obj))
             replyMsg = reply.NewsMsg(toUser, fromUser, len(articles),
                                      articles)
             return replyMsg
     return reply.TextMsg(toUser, fromUser, ret)
Пример #6
0
    def POST(self):
        try:
            webData = web.data()
            print "Handle Post webdata is ", webData
            recMsg = receive.parse_xml(webData)
            if isinstance(recMsg, receive.Msg):
                toUser = recMsg.FromUserName
                fromUser = recMsg.ToUserName
                if recMsg.MsgType == 'text':
                    if recMsg.Content == 'help':
                        content = u'暂时啥都木有'
                        replyMsg = reply.TextMsg(toUser, fromUser,
                                                 content.encode("utf-8"))
                        return replyMsg.send()

                    elif recMsg.Content == "未茶":
                        mediaType = "news"
                        items = Material().batch_get(mediaType)
                        item = items['item'][0]
                        content = item['content']
                        sourceList = []
                        print content
                        for news_item in content['news_item']:
                            source = []
                            title = news_item['title']
                            description = news_item['digest']
                            thumb_url = news_item['thumb_url']
                            url = news_item['url']
                            source.append(title.encode("utf-8"))
                            source.append(description)
                            source.append(thumb_url)
                            source.append(url)
                            sourceList.append(source)
                        replyMsg = reply.NewsMsg(toUser, fromUser, sourceList)
                        return replyMsg.send()
                    else:
                        content = u'初入茶道 请输入help查看操作指令'
                        replyMsg = reply.TextMsg(toUser, fromUser,
                                                 content.encode("utf-8"))
                        return replyMsg.send()
                elif recMsg.MsgType == 'image':
                    mediaId = recMsg.MediaId
                    replyMsg = reply.ImageMsg(toUser, fromUser, mediaId)
                    return replyMsg.send()
                else:
                    return reply.Msg().send()

            elif isinstance(recMsg, receive.EventMsg):
                toUser = recMsg.FromUserName
                fromUser = recMsg.ToUserName
                if recMsg.Event == "subscribe":
                    content = u'''您好,欢迎关注未茶!
回复“未茶”浏览本期匠志'''
                    replyMsg = reply.TextMsg(toUser, fromUser,
                                             content.encode("utf-8"))
                    return replyMsg.send()

                elif recMsg.Event == "unsubscribe":
                    content = u'一茶未尽 何日盼君再来'
                    replyMsg = reply.TextMsg(toUser, fromUser,
                                             content.encode("utf-8"))
                    return replyMsg.send()

                elif recMsg.Event == 'CLICK':
                    print "test123 ", recMsg.EventKey
                    if recMsg.EventKey == 'myMedia':
                        content = u"编写中,尚未完成".encode('utf-8')
                        replyMsg = reply.TextMsg(toUser, fromUser, content)
                        return replyMsg.send()
                    else:
                        content = "test"
                        replyMsg = reply.TextMsg(toUser, fromUser, content)
                        return replyMsg.send()

                else:
                    content = "test"
                    replyMsg = reply.TextMsg(toUser, fromUser, content)
                    return replyMsg.send()

            else:
                print "nothing"
                return reply.Msg().send()
        except Exception, Argment:
            return Argment