From e699608e3b8dc016423fd26d310366b06ffe0d40 Mon Sep 17 00:00:00 2001 From: Gunther Cox Date: Sat, 12 Sep 2015 19:23:26 -0400 Subject: [PATCH] Added IO adapter for communicating with users through twitter. --- chatterbot/adapters/io/twitter.py | 1 + examples/terminal_example.py | 1 + examples/twitter_example.py | 41 +++++++++++++------------- tests/io_adapter_tests/test_twitter.py | 17 ++++------- 4 files changed, 28 insertions(+), 32 deletions(-) diff --git a/chatterbot/adapters/io/twitter.py b/chatterbot/adapters/io/twitter.py index 29f6c9c5e..3f082718c 100644 --- a/chatterbot/adapters/io/twitter.py +++ b/chatterbot/adapters/io/twitter.py @@ -217,3 +217,4 @@ def tweet_to_friends(self, username, slug, greetings, debug=False): else: sleep(3600-time() % 3600) t.statuses.update(status=message) + diff --git a/examples/terminal_example.py b/examples/terminal_example.py index a0fb3e40e..6f6c97693 100644 --- a/examples/terminal_example.py +++ b/examples/terminal_example.py @@ -37,3 +37,4 @@ except (KeyboardInterrupt, EOFError, SystemExit): break + diff --git a/examples/twitter_example.py b/examples/twitter_example.py index 8a38ef34c..cf6e31233 100644 --- a/examples/twitter_example.py +++ b/examples/twitter_example.py @@ -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 diff --git a/tests/io_adapter_tests/test_twitter.py b/tests/io_adapter_tests/test_twitter.py index a4d5b874e..f566555a3 100644 --- a/tests/io_adapter_tests/test_twitter.py +++ b/tests/io_adapter_tests/test_twitter.py @@ -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" + )