Пример #1
0
    def get(self):
        signature = str(self.get_argument('signature',''))
        echostr = str(self.get_argument('echostr',''))
        timestamp = str(self.get_argument('timestamp',''))
        nonce = str(self.get_argument('nonce',''))

        weixin = WeiXin.on_connect(token=WX_TOKEN,
            timestamp=timestamp,
            nonce=nonce,
            signature=signature,
            echostr=echostr)
        if weixin.validate():
            return self.write(echostr)
        return self.write('')
Пример #2
0
    def get(self):
        signature = str(self.get_argument('signature', ''))
        echostr = str(self.get_argument('echostr', ''))
        timestamp = str(self.get_argument('timestamp', ''))
        nonce = str(self.get_argument('nonce', ''))

        weixin = WeiXin.on_connect(token=WX_TOKEN,
                                   timestamp=timestamp,
                                   nonce=nonce,
                                   signature=signature,
                                   echostr=echostr)
        if weixin.validate():
            return self.write(echostr)
        return self.write('')
Пример #3
0
    def post(self):
        body = self.request.body
        weixin = WeiXin.on_message(body)
        mydict = weixin.to_json()

        if mydict['MsgType'] == 'text':
            content = mydict['Content'].encode('utf-8')
            help = self.get_help_menu()
            reply = ""

            #TBD 分词这部分需要改造,urllib2不能用?
            if len(content) > 100000:
                _SEGMENT_BASE_URL = 'http://segment.sae.sina.com.cn/urlclient.php'
                payload = urllib.urlencode([('context', content),])
                args = urllib.urlencode([('word_tag', 1), ('encoding', 'UTF-8'),])
                url = _SEGMENT_BASE_URL + '?' + args
                result = urllib2.urlopen(url, payload).read()
                if result:
                    result = eval(result)
                    content = ' '.join(w["word"] for w in result)

            cmd = content[0].lower()
            if cmd == "n":
                rsp = self.wx_get_latest_articles()
                if rsp:
                    reply = weixin.pack_news_xml(mydict,rsp)
            elif cmd == "c":#获取分类列表
                if len(content) == 1:
                    rsp = self.wx_get_categories()
                    if rsp:
                        reply = weixin.pack_text_xml(mydict,rsp)
                else:
                    cid = content[1:]
                    rsp = self.get_category_articles(cid)
                    if rsp:
                        reply = weixin.pack_news_xml(mydict,rsp)
            elif cmd == "l":#列举最新20篇文章条列表
                rsp = self.wx_get_artlists()
                if rsp:
                    reply = weixin.pack_text_xml(mydict,rsp)
            elif cmd == "v":# 直接获取某篇文章
                article_id = int(content[1:])
                rsp = self.wx_get_article_by_id(article_id)
                if rsp:
                    reply = weixin.pack_news_xml(mydict,rsp)
            #elif cmd == "s":
            #    #搜索太耗费资源了,而且可能也会有sql注入等安全问题
            #    #代码只是写写,实际上不会使用
            #    k = str(content[1:])
            #    rsp = self.wx_search_article(k)
            #    if rsp:
            #        #reply = weixin.pack_news_xml(mydict,rsp)
            #        reply = weixin.pack_news_xml(mydict,"抱歉,此功能暂停使用")
            #    else:
            #        reply = weixin.pack_text_xml(mydict,"抱歉,没有搜到相关关键词")
            if not reply:
                reply = weixin.pack_text_xml(mydict,help)

            self.set_header('Content-Type','application/xml')
            return self.write(reply)

		#以下要后续再拓展了
        elif mydict['MsgType'] == 'image':
            pass
            #wx_handle_pic(mydict['PicUrl'])
        elif mydict['MsgType'] == 'location':
            pass
            #wx_handle_loc(mydict['Location_X'],mydict['Location_Y'],mydict['Scale'],mydict['Label'])
        elif mydict['MsgType'] == 'link':
            pass
            #wx_handle_link(mydict['Title'],mydict['Description'],mydict['Url'])
        elif mydict['MsgType'] == 'event':
            #print "mydict['Event'] = %s" % (mydict['Event'])
            if 'subscribe' == mydict['Event']:
                reply = weixin.to_xml(to_user_name=mydict['FromUserName'],
                            from_user_name=mydict['ToUserName'],
                            msg_type='text',
                            content='欢迎欢迎,热烈欢迎。',
                            create_time=str(int(time())),
                            )
                self.set_header('Content-Type','application/xml')
                return self.write(reply)
            elif 'unsubscribe' == mydict['Event']:
                reply = weixin.to_xml(to_user_name=mydict['FromUserName'],
                            from_user_name=mydict['ToUserName'],
                            msg_type='text',
                            content='青山不在,绿水长流,后会有期!',
                            create_time=str(int(time())),
                            )
                self.set_header('Content-Type','application/xml')
                return self.write(reply)
            else:
                pass
            wx_handletext(mydict['Event'],mydict['EventKey'])
        else:
            print "Nootice: mydict['MsgType'] = %s" % (mydict['MsgType'])
        return self.write('')
Пример #4
0
    def post(self):
        body = self.request.body
        weixin = WeiXin.on_message(body)
        mydict = weixin.to_json()

        if mydict['MsgType'] == 'text':
            content = mydict['Content'].encode('utf-8')
            help = self.get_help_menu()
            reply = ""

            #TBD 分词这部分需要改造,urllib2不能用?
            if len(content) > 100000:
                _SEGMENT_BASE_URL = 'http://segment.sae.sina.com.cn/urlclient.php'
                payload = urllib.urlencode([
                    ('context', content),
                ])
                args = urllib.urlencode([
                    ('word_tag', 1),
                    ('encoding', 'UTF-8'),
                ])
                url = _SEGMENT_BASE_URL + '?' + args
                result = urllib2.urlopen(url, payload).read()
                if result:
                    result = eval(result)
                    content = ' '.join(w["word"] for w in result)

            cmd = content[0].lower()
            if cmd == "n":
                rsp = self.wx_get_latest_articles()
                if rsp:
                    reply = weixin.pack_news_xml(mydict, rsp)
            elif cmd == "c":  #获取分类列表
                if len(content) == 1:
                    rsp = self.wx_get_categories()
                    if rsp:
                        reply = weixin.pack_text_xml(mydict, rsp)
                else:
                    cid = content[1:]
                    rsp = self.get_category_articles(cid)
                    if rsp:
                        reply = weixin.pack_news_xml(mydict, rsp)
            elif cmd == "l":  #列举最新20篇文章条列表
                rsp = self.wx_get_artlists()
                if rsp:
                    reply = weixin.pack_text_xml(mydict, rsp)
            elif cmd == "v":  # 直接获取某篇文章
                article_id = int(content[1:])
                rsp = self.wx_get_article_by_id(article_id)
                if rsp:
                    reply = weixin.pack_news_xml(mydict, rsp)
            #elif cmd == "s":
            #    #搜索太耗费资源了,而且可能也会有sql注入等安全问题
            #    #代码只是写写,实际上不会使用
            #    k = str(content[1:])
            #    rsp = self.wx_search_article(k)
            #    if rsp:
            #        #reply = weixin.pack_news_xml(mydict,rsp)
            #        reply = weixin.pack_news_xml(mydict,"抱歉,此功能暂停使用")
            #    else:
            #        reply = weixin.pack_text_xml(mydict,"抱歉,没有搜到相关关键词")
            if not reply:
                reply = weixin.pack_text_xml(mydict, help)

            self.set_header('Content-Type', 'application/xml')
            return self.write(reply)

#以下要后续再拓展了
        elif mydict['MsgType'] == 'image':
            pass
            #wx_handle_pic(mydict['PicUrl'])
        elif mydict['MsgType'] == 'location':
            pass
            #wx_handle_loc(mydict['Location_X'],mydict['Location_Y'],mydict['Scale'],mydict['Label'])
        elif mydict['MsgType'] == 'link':
            pass
            #wx_handle_link(mydict['Title'],mydict['Description'],mydict['Url'])
        elif mydict['MsgType'] == 'event':
            #print "mydict['Event'] = %s" % (mydict['Event'])
            if 'subscribe' == mydict['Event']:
                reply = weixin.to_xml(
                    to_user_name=mydict['FromUserName'],
                    from_user_name=mydict['ToUserName'],
                    msg_type='text',
                    content='欢迎欢迎,热烈欢迎。',
                    create_time=str(int(time())),
                )
                self.set_header('Content-Type', 'application/xml')
                return self.write(reply)
            elif 'unsubscribe' == mydict['Event']:
                reply = weixin.to_xml(
                    to_user_name=mydict['FromUserName'],
                    from_user_name=mydict['ToUserName'],
                    msg_type='text',
                    content='青山不在,绿水长流,后会有期!',
                    create_time=str(int(time())),
                )
                self.set_header('Content-Type', 'application/xml')
                return self.write(reply)
            else:
                pass
            wx_handletext(mydict['Event'], mydict['EventKey'])
        else:
            print "Nootice: mydict['MsgType'] = %s" % (mydict['MsgType'])
        return self.write('')