From 5fda59dde0be727c9ce9f90d56c01d571b06ec61 Mon Sep 17 00:00:00 2001 From: amanagr Date: Thu, 22 Feb 2018 11:19:52 +0530 Subject: [PATCH] Susi AI Bot: Create a susi ai bot. This bot uses SUSI_SERVER API to get response. --- tools/run-mypy | 4 +- zulip_bots/zulip_bots/bots/susi/__init__.py | 0 .../bots/susi/fixtures/test_reply.json | 57 +++++++++++++++++++ zulip_bots/zulip_bots/bots/susi/susi.py | 51 +++++++++++++++++ zulip_bots/zulip_bots/bots/susi/test_susi.py | 45 +++++++++++++++ 5 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 zulip_bots/zulip_bots/bots/susi/__init__.py create mode 100644 zulip_bots/zulip_bots/bots/susi/fixtures/test_reply.json create mode 100644 zulip_bots/zulip_bots/bots/susi/susi.py create mode 100644 zulip_bots/zulip_bots/bots/susi/test_susi.py diff --git a/tools/run-mypy b/tools/run-mypy index 78a3e2b..eaf70a8 100755 --- a/tools/run-mypy +++ b/tools/run-mypy @@ -90,7 +90,9 @@ force_include = [ "zulip_bots/zulip_bots/bots/game_handler_bot/game_handler_bot.py", "zulip_bots/zulip_bots/bots/game_handler_bot/test_game_handler_bot.py", "zulip_bots/zulip_bots/bots/trello/trello.py", - "zulip_bots/zulip_bots/bots/trello/test_trello.py" + "zulip_bots/zulip_bots/bots/trello/test_trello.py", + "zulip_bots/zulip_bots/bots/susi/susi.py", + "zulip_bots/zulip_bots/bots/susi/test_susi.py", ] parser = argparse.ArgumentParser(description="Run mypy on files tracked by git.") diff --git a/zulip_bots/zulip_bots/bots/susi/__init__.py b/zulip_bots/zulip_bots/bots/susi/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/zulip_bots/zulip_bots/bots/susi/fixtures/test_reply.json b/zulip_bots/zulip_bots/bots/susi/fixtures/test_reply.json new file mode 100644 index 0000000..3079fce --- /dev/null +++ b/zulip_bots/zulip_bots/bots/susi/fixtures/test_reply.json @@ -0,0 +1,57 @@ +{ + "request": { + "api_url": "https://api.susi.ai/susi/chat.json?q=hi" + }, + "response": { + "query": "hi", + "count": 1, + "client_id": "aG9zdF8xNzIuNjguMTQ0LjI3XzNkZWNmYjA0", + "query_date": "2018-02-21T06:45:24.291Z", + "answers": [{ + "data": [ + { + "0": "hi", + "1": "", + "token_original": "hi", + "token_canonical": "hi", + "token_categorized": "hi", + "timezoneOffset": "0", + "language": "en", + "answer": "Hello!", + "skill_link": "https://github.com/fossasia/susi_server/blob/development/conf/susi/en_0700_ai_play.json", + "query": "hi", + "skill": "/susi_server/conf/susi/en_0700_ai_play.json", + "_username": "ABC" + }, + { + "answer": "C is the third letter in the English alphabet and a letter of the alphabets of many other writing systems which inherited it from the Latin alphabet.", + "skill_link": "https://github.com/fossasia/susi_server/blob/development/conf/susi/en_0090_fail.json", + "query": "c", + "skill": "/susi_server/conf/susi/en_0090_fail.json", + "_username": "ABC" + } + ], + "metadata": {"count": 5}, + "actions": [{ + "type": "answer", + "language": "en", + "expression": "Hello!" + }], + "skills": ["/susi_server/conf/susi/en_0700_ai_play.json"], + "persona": {} + }], + "answer_date": "2018-02-21T06:45:24.320Z", + "answer_time": 29, + "language": "en", + "session": {"identity": { + "type": "host", + "name": "172.68.144.27_3decfb04", + "anonymous": true + }} + }, + "response-headers": { + "status": 200, + "ok": true, + "content-type": "application/json; charset=utf-8" + } +} diff --git a/zulip_bots/zulip_bots/bots/susi/susi.py b/zulip_bots/zulip_bots/bots/susi/susi.py new file mode 100644 index 0000000..03e162b --- /dev/null +++ b/zulip_bots/zulip_bots/bots/susi/susi.py @@ -0,0 +1,51 @@ +import requests +import urllib +from typing import Dict, Any, Tuple, Union + +class SusiHandler(object): + ''' + Susi AI Bot + To create and know more of SUSI skills go to `https://skills.susi.ai/` + ''' + + def usage(self) -> str: + return ''' + Hi, I am Susi, people generally ask me these questions: + ``` + What is the exchange rate of USD to BTC + How to cook biryani + draw a card + word starting with m and ending with v + question me + random GIF + image of a bird + flip a coin + let us play + who is Albert Einstein + search wikipedia for artificial intelligence + when is christmas + what is hello in french + name a popular movie + news + tell me a joke + buy a dress + currency of singapore + distance between india and singapore + tell me latest phone by LG + ``` + ''' + + def handle_message(self, message: Dict[str, str], bot_handler: Any) -> None: + msg = message['content'] + if msg == 'help' or msg == '': + bot_handler.send_reply(message, self.usage()) + return + encoded_msg = urllib.parse.quote_plus(msg.encode('utf8')) + reply = requests.get("https://api.susi.ai/susi/chat.json?q=" + encoded_msg) + try: + answer = reply.json()['answers'][0]['actions'][0]['expression'] + except Exception: + answer = "I don't understand. Can you rephrase?" + bot_handler.send_reply(message, answer) + +handler_class = SusiHandler diff --git a/zulip_bots/zulip_bots/bots/susi/test_susi.py b/zulip_bots/zulip_bots/bots/susi/test_susi.py new file mode 100644 index 0000000..a2f4b16 --- /dev/null +++ b/zulip_bots/zulip_bots/bots/susi/test_susi.py @@ -0,0 +1,45 @@ +from zulip_bots.test_lib import ( + BotTestCase, +) + +from typing import Any + +class TestSusiBot(BotTestCase): + bot_name = "susi" + + def test_help(self) -> None: + bot_response = ''' + Hi, I am Susi, people generally ask me these questions: + ``` + What is the exchange rate of USD to BTC + How to cook biryani + draw a card + word starting with m and ending with v + question me + random GIF + image of a bird + flip a coin + let us play + who is Albert Einstein + search wikipedia for artificial intelligence + when is christmas + what is hello in french + name a popular movie + news + tell me a joke + buy a dress + currency of singapore + distance between india and singapore + tell me latest phone by LG + ``` + ''' + + self.verify_reply('', bot_response) + self.verify_reply('help', bot_response) + + def test_issue(self) -> None: + request = 'hi' + bot_response = 'Hello!' + + with self.mock_http_conversation('test_reply'): + self.verify_reply(request, bot_response)