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

Update scope of external package imports #426

Merged
merged 2 commits into from
Nov 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 1 addition & 4 deletions chatterbot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
"""
ChatterBot is a machine learning, conversational dialog engine.
"""
import sys

if 'install' not in sys.argv and 'egg_info' not in sys.argv:
from .chatterbot import ChatBot
from .chatterbot import ChatBot

__version__ = '0.5.0'
__author__ = 'Gunther Cox'
Expand Down
22 changes: 20 additions & 2 deletions chatterbot/input/gitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from time import sleep
from chatterbot.input import InputAdapter
from chatterbot.conversation import Statement
import requests


class Gitter(InputAdapter):
Expand Down Expand Up @@ -42,6 +41,11 @@ def _validate_status_code(self, response):
raise self.HTTPStatusException('{} status code recieved'.format(code))

def join_room(self, room_name):
"""
Join the specified Gitter room.
"""
import requests

endpoint = '{}rooms'.format(self.gitter_host)
response = requests.post(
endpoint,
Expand All @@ -55,6 +59,8 @@ def join_room(self, room_name):
return response.json()

def get_user_data(self):
import requests

endpoint = '{}user'.format(self.gitter_host)
response = requests.get(
endpoint,
Expand All @@ -67,7 +73,14 @@ def get_user_data(self):
return response.json()

def mark_messages_as_read(self, message_ids):
endpoint = '{}user/{}/rooms/{}/unreadItems'.format(self.gitter_host, self.user_id, self.room_id)
"""
Mark the specified message ids as read.
"""
import requests

endpoint = '{}user/{}/rooms/{}/unreadItems'.format(
self.gitter_host, self.user_id, self.room_id
)
response = requests.post(
endpoint,
headers=self.headers,
Expand All @@ -80,6 +93,11 @@ def mark_messages_as_read(self, message_ids):
return response.json()

def get_most_recent_message(self):
"""
Get the most recent message from the Gitter room.
"""
import requests

endpoint = '{}rooms/{}/chatMessages?limit=1'.format(self.gitter_host, self.room_id)
response = requests.get(
endpoint,
Expand Down
11 changes: 8 additions & 3 deletions chatterbot/input/hipchat.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from __future__ import unicode_literals
from time import sleep
from chatterbot.input import InputAdapter
from chatterbot.conversation import Statement
from time import sleep
import requests


class HipChat(InputAdapter):
Expand Down Expand Up @@ -48,6 +47,7 @@ def view_recent_room_history(self, room_id_or_name, max_results=1):
"""
https://www.hipchat.com/docs/apiv2/method/view_recent_room_history
"""
import requests

recent_histroy_url = '{}/v2/room/{}/history?max-results={}'.format(
self.hipchat_host,
Expand All @@ -63,6 +63,9 @@ def view_recent_room_history(self, room_id_or_name, max_results=1):
return response.json()

def get_most_recent_message(self, room_id_or_name):
"""
Return the most recent message from the HipChat room.
"""
data = self.view_recent_room_history(room_id_or_name)

items = data['items']
Expand All @@ -72,7 +75,9 @@ def get_most_recent_message(self, room_id_or_name):
return items[-1]

def process_input(self, statement):

"""
Process input from the HipChat room.
"""
new_message = False

input_statement = self.chatbot.get_last_input_statement()
Expand Down
12 changes: 9 additions & 3 deletions chatterbot/input/mailgun.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import unicode_literals
import datetime
from chatterbot.input import InputAdapter
from chatterbot.conversation import Statement
import requests
import datetime


class Mailgun(InputAdapter):
"""
Get input from Mailgun.
"""

def __init__(self, **kwargs):
super(Mailgun, self).__init__(**kwargs)
Expand All @@ -17,6 +19,8 @@ def __init__(self, **kwargs):
self.endpoint = kwargs.get('mailgun_api_endpoint')

def get_email_stored_events(self):
import requests

yesterday = datetime.datetime.now() - datetime.timedelta(1)
return requests.get(
'{}/events'.format(self.endpoint),
Expand All @@ -38,13 +42,15 @@ def get_stored_email_urls(self):
yield item['storage']['url']

def get_message(self, url):
import requests

return requests.get(
url,
auth=('api', self.api_key)
)

def process_input(self, statement):
urls = m.get_stored_email_urls()
urls = self.get_stored_email_urls()
url = first(urls)

response = self.get_message(url)
Expand Down
16 changes: 10 additions & 6 deletions chatterbot/input/microsoft.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from __future__ import unicode_literals
from time import sleep
from chatterbot.input import InputAdapter
from chatterbot.conversation import Statement
from time import sleep
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)


class Microsoft(InputAdapter):
"""
Expand All @@ -15,9 +13,11 @@ class Microsoft(InputAdapter):

def __init__(self, **kwargs):
super(Microsoft, self).__init__(**kwargs)
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

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

# NOTE: Direct Line client credentials are different from your bot's
# credentials
Expand Down Expand Up @@ -45,6 +45,8 @@ def _validate_status_code(self, response):
format(code))

def start_conversation(self):
import requests

endpoint = '{host}/api/conversations'.format(host=self.directline_host)
response = requests.post(
endpoint,
Expand All @@ -58,6 +60,8 @@ def start_conversation(self):
return response.json()

def get_most_recent_message(self):
import requests

endpoint = '{host}/api/conversations/{id}/messages'\
.format(host=self.directline_host,
id=self.conversation_id)
Expand Down
7 changes: 4 additions & 3 deletions chatterbot/logic/time_adapter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from __future__ import unicode_literals
from .logic_adapter import LogicAdapter
from chatterbot.conversation import Statement
from textblob.classifiers import NaiveBayesClassifier
from datetime import datetime
from .logic_adapter import LogicAdapter


class TimeLogicAdapter(LogicAdapter):
Expand All @@ -12,6 +10,7 @@ class TimeLogicAdapter(LogicAdapter):

def __init__(self, **kwargs):
super(TimeLogicAdapter, self).__init__(**kwargs)
from textblob.classifiers import NaiveBayesClassifier

training_data = [
('what time is it', 1),
Expand All @@ -27,6 +26,8 @@ def __init__(self, **kwargs):
self.classifier = NaiveBayesClassifier(training_data)

def process(self, statement):
from chatterbot.conversation import Statement

now = datetime.now()

confidence = self.classifier.classify(statement.text.lower())
Expand Down
8 changes: 7 additions & 1 deletion chatterbot/output/gitter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import unicode_literals
from .output_adapter import OutputAdapter
import requests


class Gitter(OutputAdapter):
Expand Down Expand Up @@ -34,6 +33,11 @@ def _validate_status_code(self, response):
raise self.HTTPStatusException('{} status code recieved'.format(code))

def join_room(self, room_name):
"""
Join the specified Gitter room.
"""
import requests

endpoint = '{}rooms'.format(self.gitter_host)
response = requests.post(
endpoint,
Expand All @@ -50,6 +54,8 @@ def send_message(self, text):
"""
Send a message to a Gitter room.
"""
import requests

endpoint = '{}rooms/{}/chatMessages'.format(self.gitter_host, self.room_id)
response = requests.post(
endpoint,
Expand Down
4 changes: 2 additions & 2 deletions chatterbot/output/hipchat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import unicode_literals
from .output_adapter import OutputAdapter
import requests
import json
from .output_adapter import OutputAdapter


class HipChat(OutputAdapter):
Expand Down Expand Up @@ -29,6 +28,7 @@ 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
"""
import requests

message_url = "{}/v2/room/{}/message".format(
self.hipchat_host,
Expand Down
3 changes: 2 additions & 1 deletion chatterbot/output/mailgun.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import unicode_literals
from .output_adapter import OutputAdapter
import requests


class Mailgun(OutputAdapter):
Expand All @@ -22,6 +21,8 @@ def send_message(self, subject, text, from_address, recipients):
* from_email: The email address that the message will be sent from.
* recipients: A list of recipient email addresses.
"""
import requests

return requests.post(
self.endpoint,
auth=('api', self.api_key),
Expand Down
40 changes: 25 additions & 15 deletions chatterbot/output/microsoft.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import unicode_literals
from .output_adapter import OutputAdapter
import requests
import json
from .output_adapter import OutputAdapter


class Microsoft(OutputAdapter):
Expand All @@ -13,14 +12,18 @@ class Microsoft(OutputAdapter):
def __init__(self, **kwargs):
super(Microsoft, self).__init__(**kwargs)

self.directline_host = kwargs.get('directline_host',
'https://directline.botframework.com')
self.direct_line_token_or_secret = kwargs.get\
('direct_line_token_or_secret')
self.directline_host = kwargs.get(
'directline_host',
'https://directline.botframework.com'
)
self.direct_line_token_or_secret = kwargs.get(
'direct_line_token_or_secret'
)
self.conversation_id = kwargs.get('conversation_id')

authorization_header = 'BotConnector {}'.\
format(self.direct_line_token_or_secret)
authorization_header = 'BotConnector {}'.format(
self.direct_line_token_or_secret
)

self.headers = {
'Authorization': authorization_header,
Expand All @@ -30,13 +33,17 @@ def __init__(self, **kwargs):
def _validate_status_code(self, response):
status_code = response.status_code
if status_code not in [200, 204]:
raise self.HTTPStatusException('{} status code recieved'.
format(status_code))
raise self.HTTPStatusException('{} status code recieved'.format(status_code))

def get_most_recent_message(self):
endpoint = '{host}/api/conversations/{id}/messages'\
.format(host=self.directline_host,
id=self.conversation_id)
"""
Return the most recently sent message.
"""
import requests
endpoint = '{host}/api/conversations/{id}/messages'.format(
host=self.directline_host,
id=self.conversation_id
)

response = requests.get(
endpoint,
Expand All @@ -62,9 +69,12 @@ def send_message(self, conversation_id, message):
Send a message to a HipChat room.
https://www.hipchat.com/docs/apiv2/method/send_message
"""
import requests

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

response = requests.post(
message_url,
Expand Down
Loading