Skip to content
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

Integration with Microsoft Bot Framework #381

Merged
merged 11 commits into from
Nov 13, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions chatterbot/adapters/output/microsoft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from chatterbot.adapters.output import OutputAdapter
import requests
import json


class Microsoft(OutputAdapter):
"""
An output adapter that allows a ChatterBot instance to send
responses to a HipChat room.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy paste error

"""

def __init__(self, **kwargs):
super(Microsoft, self).__init__(**kwargs)

self.directline_host = kwargs.get("directline_host")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This get method should also have the default set.

kwargs.get('directline_host', 'https://directline.botframework.com')

self.direct_line_token = kwargs.get("direct_line_token")
self.conversation_id = kwargs.get("conversation_id")

authorization_header = 'BotConnector {}'.format(self.direct_line_token)

self.headers = {
'Authorization': authorization_header,
'Content-Type': 'application/json'
}

def get_most_recent_message(self, watermark='1'):
endpoint = '{host}/api/conversations/{id}/messages?watermark={watermark}'\
.format(host=self.directline_host,
id=self.conversation_id,
watermark=watermark)

response = requests.get(
endpoint,
headers=self.headers
)
self.logger.info(u'{} getting most recent message'.format(
response.status_code
))
self._validate_status_code(response)
data = response.json()
if data["messages"]:
return data["messages"][0]
return None

def send_message(self, conversation_id, message):
"""
Send a message to a HipChat room.
https://www.hipchat.com/docs/apiv2/method/send_message
"""

message_url = "{host}/api/conversations/{conversationId}/messages".\
format(host=self.directline_host, conversationId=conversation_id)

response = requests.post(
message_url,
headers=self.headers,
data=json.dumps({
'message': message
})
)

# Microsoft return 204 operation succeeded and no content was returned.
return self.get_most_recent_message()

def process_response(self, statement, confidence=None):
data = self.send_message(self.conversation_id, statement.text)



# Update the output statement with the message id
self.context.recent_statements[-1][1].add_extra_data(
'microsoft_msg_id', data['id']
)

return statement