-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add HipChat input and output adapter #176
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
from chatterbot.adapters.input import InputAdapter | ||
from chatterbot.conversation import Statement | ||
from time import sleep | ||
import requests | ||
|
||
|
||
class HipChat(InputAdapter): | ||
""" | ||
An input adapter that allows a ChatterBot instance to get | ||
input statements from a HipChat room. | ||
""" | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from chatterbot.adapters.output import OutputAdapter | ||
import requests | ||
import json | ||
|
||
|
||
class HipChat(OutputAdapter): | ||
""" | ||
An output adapter that allows a ChatterBot instance to send | ||
responses to a HipChat room. | ||
""" | ||
|
||
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): | ||
""" | ||
Send a message to a HipChat room. | ||
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): | ||
""" | ||
The HipChat api supports responding to a given message. | ||
This may be a good feature to implement in the future to | ||
help with multi-user conversations. | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still deciding if this needs to be implemented.