-
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
Integration with Microsoft Bot Framework #381
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ecac471
microsoft input adapter
f69d221
microsoft output adapter
980d537
name changes
cee691d
pep8
2fa1924
doc updation
34939ae
small correction
ff41ce7
example code
5eb4766
removed unused statement
c61e902
input adapter updation
1b33b04
output adapter updation
c38842f
comments incorporated
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 |
---|---|---|
@@ -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. | ||
""" | ||
|
||
def __init__(self, **kwargs): | ||
super(Microsoft, self).__init__(**kwargs) | ||
|
||
self.directline_host = kwargs.get("directline_host") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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.
Copy paste error