Beispiel #1
0
    def test_gen_sign_without_given_ts(self, m_time):
        m_time.time.return_value = self.timestamp

        svc = olami.OlamiService(self.app_key, self.app_secret)
        sign_no_given_ts = svc._gen_sign('seq')
        sign_given_ts = svc._gen_sign('seq', timestamp=self.timestamp)

        self.assertEqual(sign_no_given_ts, sign_given_ts)
Beispiel #2
0
 def test_gen_rq_input_type(self):
     svc = olami.OlamiService(self.app_key, self.app_secret, input_type=0)
     rq = svc._gen_rq('ThisIsTestText')
     self.assertEqual(
         rq, {
             'data_type': 'stt',
             'data': {
                 'input_type': 0,
                 'text': 'ThisIsTestText'
             }
         })
Beispiel #3
0
 def test_gen_rq_as_text(self):
     svc = olami.OlamiService(self.app_key, self.app_secret)
     rq = svc._gen_rq('ThisIsTestText', as_text=True)
     self.assertEqual(
         rq,
         json.dumps({
             'data_type': 'stt',
             'data': {
                 'input_type': 1,
                 'text': 'ThisIsTestText'
             }
         }))
Beispiel #4
0
    def test_make_request_nli_response_error(self, m_time, m_requests):
        m_time.time.return_value = self.timestamp
        m_requests.post.return_value.status_code = 200
        m_requests.post.return_value.json.return_value = {
            'data': 'SomeData',
            'status': 'error'
        }

        svc = olami.OlamiService(self.app_key, self.app_secret)
        with self.assertRaisesRegex(NlpServiceError,
                                    r"NLI responded status != 'ok':.*"):
            svc._make_request('TextForNli')
Beispiel #5
0
    def test_make_request_http_status_error(self, m_time, m_requests):
        m_time.time.return_value = self.timestamp
        m_requests.post.return_value.status_code = 400
        m_requests.post.return_value.json.return_value = {
            'data': 'SomeData',
            'status': 'whatever'
        }

        svc = olami.OlamiService(self.app_key, self.app_secret)
        with self.assertRaisesRegex(NlpServiceError,
                                    r"HTTP Response != 200:.*"):
            svc._make_request('TextForNli')
Beispiel #6
0
    def test_gen_parameters(self, m_time):
        m_time.time.return_value = self.timestamp

        svc = olami.OlamiService(self.app_key, self.app_secret)
        params = svc._gen_parameters('TextForNli')
        self.assertEqual(
            params, {
                'appkey': self.app_key,
                'api': 'nli',
                'timestamp': int(self.timestamp * 1000),
                'sign': svc._gen_sign('nli'),
                'rq': svc._gen_rq('TextForNli', as_text=True)
            })
Beispiel #7
0
    def test_make_request(self, m_time, m_requests):
        m_time.time.return_value = self.timestamp
        m_requests.post.return_value.status_code = 200
        m_requests.post.return_value.json.return_value = {
            'data': {
                'nli': 'NliResult'
            },
            'status': 'ok'
        }

        svc = olami.OlamiService(self.app_key, self.app_secret)
        result = svc._make_request('TextForNli')

        self.assertEqual(result, 'NliResult')
        m_requests.post.assert_called_with(
            olami.OlamiService.BASE_URL,
            params=svc._gen_parameters('TextForNli'))
def handle_text_message(event):
    logger.debug('event: ' + str(event))
    olami_svc = olami.OlamiService(app.config['OLAMI_APP_KEY'],
                                   app.config['OLAMI_APP_SECRET'],
                                   cusid=event.source.user_id)
    try:
        resp = olami_svc(event.message.text)
        reply = resp.as_line_messages()
    except NlpServiceError as e:
        err_msg = 'NLP service is currently unavailable: {}'.format(repr(e))
        logger.error(err_msg)
        reply = TextSendMessage(text=err_msg)
    except Exception as e:
        err_msg = 'Unexpected error: {}'.format(repr(e))
        logger.exception(err_msg)
        reply = TextSendMessage(text=err_msg)
    finally:
        logger.info('Reply: {}'.format(reply))
        line_bot_api.reply_message(event.reply_token, reply)
def handle_text_message(event):
    logger.debug(event)
    olami_svc = olami.OlamiService(app.config['OLAMI_APP_KEY'],
                                   app.config['OLAMI_APP_SECRET'],
                                   cusid=None)  #event.source.user_id)
    msg_txt = event.message.text.strip()
    reply = None
    try:
        if '北一最' in msg_txt or '北一誰最' in msg_txt:
            adj = msg_txt.split('最')[1]
            for x in '的是誰呢ㄋ啊阿ㄚ嗎嘛ㄇ??':
                adj = adj.split(x)[0]
            if '=' in adj or '=' in adj:
                adj, who = adj.split('=' if '=' in adj else '=')
                if adj and who:
                    requests.get(app.config['GOOGLE_SHEETS'] + '?' + adj +
                                 '=' + who)
                    reply = TextSendMessage(text='是喔!')
                else:
                    reply = TextSendMessage(text='蛤?')
            else:
                who = requests.get(app.config['GOOGLE_SHEETS'] + '?' +
                                   adj).text
                reply = TextSendMessage(text=who)
        elif '口罩' in msg_txt:
            reply = TextSendMessage(text='geobingan.info/#/event/mask')
        else:
            reply = []
            likes = msg_txt.count('讚') + msg_txt.count('👍')
            for url in ig_urls(
            )[:likes if likes < 5 else 5]:  #random.sample(urls, count):
                #reply.append(TextSendMessage(text=url))
                reply.append(
                    ImageSendMessage(original_content_url=url,
                                     preview_image_url=url))
            #resp = olami_svc(msg_txt[2:])
            #reply = resp.as_line_messages()
        #if event.source.user_id == 'U277d1a8cf7717e27e5d7d46971a64f65':
        #    reply = ImageSendMessage(
        #        original_content_url='https://www.1001000.io/img/cucumber.gif',
        #        preview_image_url='https://www.1001000.io/img/cucumber.jpg')
        #if '發財' in msg_txt or '發大財' in msg_txt:
        #    reply = ImageSendMessage(
        #        original_content_url='https://www.1001000.io/img/whiteeye.gif',
        #        preview_image_url='https://www.1001000.io/img/whiteeye.gif')
    except NlpServiceError as e:
        err_msg = 'NLP service is currently unavailable: {}'.format(repr(e))
        logger.error(err_msg)
        reply = TextSendMessage(text=err_msg)
    except Exception as e:
        err_msg = 'Unexpected error: {}'.format(repr(e))
        logger.exception(err_msg)
        reply = TextSendMessage(text=err_msg)
    finally:
        payload = {'text': msg_txt, 'user_id': event.source.user_id}
        try:
            payload['group_id'] = event.source.group_id
        except:
            pass
        try:
            payload['room_id'] = event.source.room_id
        except:
            pass
        requests.post(app.config['GOOGLE_SHEETS'], data=payload)
        if reply:
            logger.info(reply)
            line_bot_api.reply_message(event.reply_token, reply)
Beispiel #10
0
    def test_gen_sign(self):
        svc = olami.OlamiService(self.app_key, self.app_secret)
        sign = svc._gen_sign('seq', timestamp=1492099200.0)

        self.assertEqual(sign, '7d80ae313ae5e0358e725e40d1773214')
Beispiel #11
0
 def test_invalid_input_type(self):
     # Invalid input_type argument
     with self.assertRaisesRegex(ValueError, r'Invalid input_type.*'):
         olami.OlamiService(self.app_key, self.app_secret, input_type=2)