Skip to content

Commit

Permalink
simsimi plugin updated
Browse files Browse the repository at this point in the history
  • Loading branch information
fopina committed Jan 21, 2016
1 parent 6795633 commit e70e213
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
16 changes: 13 additions & 3 deletions plugins/simsimi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@


class SimsimiPlugin(tgbot.TGPluginBase):
def __init__(self, key, trial_key=True):
super(SimsimiPlugin, self).__init__()
self._key = key
self._trial_key = trial_key
self._url = 'http://sandbox.api.simsimi.com/request.p' if trial_key else 'http://api.simsimi.com/request.p'

def simsimi(self, message, text):
self.bot.send_chat_action(message.chat.id, ChatAction.TEXT)

res = requests.get('http://www.simsimi.com/requestChat', params={
res = requests.get(self._url, params={
'key': self._key,
'lc': 'en',
'ft': '1.0',
'req': text,
'text': text,
}).json()

self.bot.send_message(message.chat.id, res['res']['msg'])
if res['result'] == 100:
self.bot.send_message(message.chat.id, res['response'])
else:
self.bot.send_message(message.chat.id, 'Sorry, sleeping at the moment...')

def chat(self, message, text):
if not text:
Expand Down
25 changes: 21 additions & 4 deletions tests/test_simsimi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
from tgbot import plugintest
from tgbot.botapi import Update
from plugins.simsimi import SimsimiPlugin
import os

from requests.packages import urllib3
urllib3.disable_warnings()


class SimsimiPluginTest(plugintest.PluginTestCase):
def setUp(self):
self.bot = self.fake_bot('', plugins=[], no_command=SimsimiPlugin())
self._key = os.environ.get('SIMSIMI_KEY', '')
self.bot = self.fake_bot('', plugins=[], no_command=SimsimiPlugin(self._key))
self.received_id = 1

def receive_message(self, text, sender=None, chat=None):
Expand Down Expand Up @@ -35,10 +40,22 @@ def receive_message(self, text, sender=None, chat=None):
self.received_id += 1

def test_reply(self):
self.receive_message('hello')
# if no key configured, use mock
if not self._key:
import mock

# any reply will do...
self.last_reply(self.bot)
def fget(*args, **kwargs):
r = type('Test', (object,), {})()
r.json = lambda: {'result': 100, 'response': 'hello'}
return r

with mock.patch('requests.get', fget):
self.receive_message('hello')
else:
self.receive_message('hello')

# any reply will do except the error reply
self.assertNotEqual(self.last_reply(self.bot), 'Sorry, sleeping at the moment...')

def test_no_reply(self):
self.receive_message('')
Expand Down

0 comments on commit e70e213

Please sign in to comment.