Skip to content

Commit

Permalink
Added IO adapter for communicating with users through twitter.
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Nov 9, 2015
1 parent 4ea4936 commit e699608
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 32 deletions.
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)

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 e699608

Please sign in to comment.