Skip to content

Commit

Permalink
Added hipchat input and output adapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Jun 24, 2016
1 parent 227f213 commit fb1f273
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 0 deletions.
1 change: 1 addition & 0 deletions chatterbot/adapters/input/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .input_adapter import InputAdapter
from .hipchat import HipChat
from .terminal import TerminalAdapter
from .variable_input_type_adapter import VariableInputTypeAdapter
103 changes: 103 additions & 0 deletions chatterbot/adapters/input/hipchat.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions chatterbot/adapters/output/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from .terminal import TerminalAdapter
from .output_format_adapter import OutputFormatAdapter
from .mailgun import Mailgun
from .hipchat import HipChat
52 changes: 52 additions & 0 deletions chatterbot/adapters/output/hipchat.py
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions examples/hipchat_bot.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fb1f273

Please sign in to comment.