Susi AI Bot: Create a susi ai bot.

This bot uses SUSI_SERVER API to get response.
This commit is contained in:
amanagr 2018-02-22 11:19:52 +05:30 committed by showell
parent 9175c451f6
commit 5fda59dde0
5 changed files with 156 additions and 1 deletions

View file

@ -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/game_handler_bot.py",
"zulip_bots/zulip_bots/bots/game_handler_bot/test_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/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.") parser = argparse.ArgumentParser(description="Run mypy on files tracked by git.")

View file

@ -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"
}
}

View file

@ -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

View file

@ -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)