From fb1f273d6d0d605eeb8e517c1b5be0c7978d3181 Mon Sep 17 00:00:00 2001 From: Gunther Cox Date: Thu, 19 May 2016 21:45:30 -0400 Subject: [PATCH] Added hipchat input and output adapter. --- chatterbot/adapters/input/__init__.py | 1 + chatterbot/adapters/input/hipchat.py | 103 +++++++++++++++++++++++++ chatterbot/adapters/output/__init__.py | 1 + chatterbot/adapters/output/hipchat.py | 52 +++++++++++++ examples/hipchat_bot.py | 29 +++++++ 5 files changed, 186 insertions(+) create mode 100644 chatterbot/adapters/input/hipchat.py create mode 100644 chatterbot/adapters/output/hipchat.py create mode 100644 examples/hipchat_bot.py diff --git a/chatterbot/adapters/input/__init__.py b/chatterbot/adapters/input/__init__.py index 45dce5953..8681fd83a 100644 --- a/chatterbot/adapters/input/__init__.py +++ b/chatterbot/adapters/input/__init__.py @@ -1,3 +1,4 @@ from .input_adapter import InputAdapter +from .hipchat import HipChat from .terminal import TerminalAdapter from .variable_input_type_adapter import VariableInputTypeAdapter diff --git a/chatterbot/adapters/input/hipchat.py b/chatterbot/adapters/input/hipchat.py new file mode 100644 index 000000000..926d76ad6 --- /dev/null +++ b/chatterbot/adapters/input/hipchat.py @@ -0,0 +1,103 @@ +from chatterbot.adapters.input import InputAdapter +from chatterbot.conversation import Statement +from time import sleep +import requests + + +class HipChat(InputAdapter): + + def __init__(self, **kwargs): + super(HipChat, self).__init__(**kwargs) + + self.hipchat_host = kwargs.get("hipchat_host") + self.hipchat_access_token = kwargs.get("hipchat_access_token") + self.hipchat_room = kwargs.get("hipchat_room") + + authorization_header = "Bearer {}".format(self.hipchat_access_token) + + self.headers = { + 'Authorization': authorization_header, + 'Content-Type': 'application/json' + } + + # This is a list of the messages that have been responded to + self.recent_message_ids = self.get_initial_ids() + + def get_initial_ids(self): + """ + Returns a list of the most recent message ids. + """ + data = self.view_recent_room_history( + self.hipchat_room, + max_results=75 + ) + + results = set() + + for item in data['items']: + results.add(item['id']) + + return results + + def view_recent_room_history(self, room_id_or_name, max_results=1): + # https://www.hipchat.com/docs/apiv2/method/view_recent_room_history + + recent_histroy_url = "{}/v2/room/{}/history?max-results={}".format( + self.hipchat_host, + room_id_or_name, + max_results + ) + + response = requests.get( + recent_histroy_url, + headers=self.headers + ) + + return response.json() + + def get_most_recent_message(self, room_id_or_name): + data = self.view_recent_room_history(room_id_or_name) + + items = data['items'] + + if not items: + return None + return items[-1] + + def process_input(self, statement): + + new_message = False + + input_statement = self.context.get_last_input_statement() + response_statement = self.context.get_last_response_statement() + + if input_statement: + last_message_id = input_statement.extra_data.get( + 'hipchat_message_id', None + ) + if last_message_id: + self.recent_message_ids.add(last_message_id) + + if response_statement: + last_message_id = response_statement.extra_data.get( + 'hipchat_message_id', None + ) + if last_message_id: + self.recent_message_ids.add(last_message_id) + + while not new_message: + data = self.get_most_recent_message(self.hipchat_room) + + if data and data['id'] not in self.recent_message_ids: + self.recent_message_ids.add(data['id']) + new_message = True + else: + pass + sleep(3.5) + + text = data['message'] + + statement = Statement(text) + statement.add_extra_data('hipchat_message_id', data['id']) + + return statement diff --git a/chatterbot/adapters/output/__init__.py b/chatterbot/adapters/output/__init__.py index ebe851d01..34587c10c 100644 --- a/chatterbot/adapters/output/__init__.py +++ b/chatterbot/adapters/output/__init__.py @@ -2,3 +2,4 @@ from .terminal import TerminalAdapter from .output_format_adapter import OutputFormatAdapter from .mailgun import Mailgun +from .hipchat import HipChat diff --git a/chatterbot/adapters/output/hipchat.py b/chatterbot/adapters/output/hipchat.py new file mode 100644 index 000000000..0147e0e9f --- /dev/null +++ b/chatterbot/adapters/output/hipchat.py @@ -0,0 +1,52 @@ +from chatterbot.adapters.output import OutputAdapter +import requests +import json + + +class HipChat(OutputAdapter): + + def __init__(self, **kwargs): + super(HipChat, self).__init__(**kwargs) + + self.hipchat_host = kwargs.get("hipchat_host") + self.hipchat_access_token = kwargs.get("hipchat_access_token") + self.hipchat_room = kwargs.get("hipchat_room") + + authorization_header = "Bearer {}".format(self.hipchat_access_token) + + self.headers = { + 'Authorization': authorization_header, + 'Content-Type': 'application/json' + } + + def send_message(self, room_id_or_name, message): + # https://www.hipchat.com/docs/apiv2/method/send_message + + message_url = "{}/v2/room/{}/message".format( + self.hipchat_host, + room_id_or_name + ) + + response = requests.post( + message_url, + headers=self.headers, + data=json.dumps({ + 'message': message + }) + ) + + return response.json() + + def reply_to_message(self): + # https://www.hipchat.com/docs/apiv2/method/reply_to_message + pass + + def process_response(self, statement): + data = self.send_message(self.hipchat_room, statement.text) + + # Update the output statement with the message id + self.context.recent_statements[-1][1].add_extra_data( + 'hipchat_message_id', data['id'] + ) + + return statement diff --git a/examples/hipchat_bot.py b/examples/hipchat_bot.py new file mode 100644 index 000000000..bab00e397 --- /dev/null +++ b/examples/hipchat_bot.py @@ -0,0 +1,29 @@ +from chatterbot import ChatBot +from chatterbot.training.trainers import ChatterBotCorpusTrainer +from settings import HIPCHAT + +''' +See the HipChat api documentation for how to get a user access token. +https://developer.atlassian.com/hipchat/guide/hipchat-rest-api/api-access-tokens +''' + +chatbot = ChatBot( + "HipChatBot", + hipchat_host=HIPCHAT["HOST"], + hipchat_room=HIPCHAT["ROOM"], + hipchat_access_token=HIPCHAT["ACCESS_TOKEN"], + input_adapter="chatterbot.adapters.input.HipChat", + output_adapter="chatterbot.adapters.output.HipChat" +) + +chatbot.set_trainer(ChatterBotCorpusTrainer) +chatbot.train("chatterbot.corpus.english") + +# The following loop will execute each time the user enters input +while True: + try: + response = chatbot.get_response(None) + + # Press ctrl-c or ctrl-d on the keyboard to exit + except (KeyboardInterrupt, EOFError, SystemExit): + break