Skip to content

Commit

Permalink
Kwargs are now passed to io adapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Oct 18, 2015
1 parent 0fde83a commit d66faa9
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 33 deletions.
9 changes: 9 additions & 0 deletions chatterbot/adapters/io/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ class IOAdapter(object):
that all input-output adapters should implement.
"""

def __init__(self, **kwargs):
pass

def process_input(self):
"""
Returns data retrieved from the input source.
"""
raise AdapterNotImplementedError()

def process_response(self, input_value):
"""
Takes an input value.
Expand Down
1 change: 1 addition & 0 deletions chatterbot/adapters/io/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,4 @@ def tweet_to_friends(self, username, slug, greetings, debug=False):
else:
sleep(3600-time() % 3600)
t.statuses.update(status=message)

2 changes: 1 addition & 1 deletion chatterbot/chatterbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(self, name, **kwargs):
self.logic = LogicAdapter()

IOAdapter = import_module(io_adapter)
self.io = IOAdapter()
self.io = IOAdapter(**kwargs)

self.trainer = None

Expand Down
1 change: 1 addition & 0 deletions examples/terminal_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@

except (KeyboardInterrupt, EOFError, SystemExit):
break

41 changes: 20 additions & 21 deletions examples/twitter_example.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
'''
Respond to mentions on twitter.
The bot will follow the user who mentioned it and
favorite the post in which the mention was made.
'''
from chatterbot import ChatBot


chatbot = ChatBot("ChatterBot",
storage_adapter="chatterbot.adapters.storage.JsonDatabaseAdapter",
logic_adapter="chatterbot.adapters.logic.ClosestMatchAdapter",
io_adapter="chatterbot.adapters.io.TwitterAdapter",
database="../database.db")
database="../database.db",
consumer_key="xxx",
consumer_secret="xxx"
)

'''
Respond to mentions on twitter.
The bot will follow the user who mentioned it and
favorite the post in which the mention was made.
'''

for mention in chatbot.get_mentions():
user_input = "Type something to begin..."

'''
Check to see if the post has been favorited
We will use this as a check for whether or not to respond to it.
Only respond to unfavorited mentions.
'''
print(user_input)

if not mention["favorited"]:
screen_name = mention["user"]["screen_name"]
text = mention["text"]
response = chatbot.get_response(text)
while True:
try:
user_input = chatbot.get_input()

print(text)
print(response)
bot_input = chatbot.get_response(user_input)

chatbot.follow(screen_name)
chatbot.favorite(mention["id"])
chatbot.reply(mention["id"], response)
except (KeyboardInterrupt, EOFError, SystemExit):
break

17 changes: 6 additions & 11 deletions tests/io_adapter_tests/test_twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@
from chatterbot.adapters.io import TwitterAdapter


class TwitterTests(TestCase):
class TwitterAdapterTests(TestCase):

def test_consumer_stored(self):
TWITTER = {
"CONSUMER_KEY": "blahblahblah",
"CONSUMER_SECRET": "nullvoidnullvoidnullvoid"
}

chatbot = TwitterAdapter(twitter=TWITTER)

self.assertEqual(TWITTER["CONSUMER_KEY"], chatbot.consumer_key)
self.assertEqual(TWITTER["CONSUMER_SECRET"], chatbot.consumer_secret)
def setUp(self):
adapter = TwitterAdapter(
consumer_key="blahblahblah",
consumer_secret="nullvoidnullvoidnullvoid"
)

0 comments on commit d66faa9

Please sign in to comment.