Skip to content

Commit

Permalink
Added storage adapter that reads tweets from twitter api.
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Sep 12, 2015
1 parent a49d7f0 commit 0af2932
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion chatterbot/adapters/storage/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .database import DatabaseAdapter
from .jsondatabase import JsonDatabaseAdapter
from .mongodb import MongoDatabaseAdapter

from .twitter import TwitterAdapter
62 changes: 62 additions & 0 deletions chatterbot/adapters/storage/twitter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from chatterbot.adapters.storage import DatabaseAdapter
from chatterbot.conversation import Statement
from application_only_auth import Client


class TwitterAdapter(DatabaseAdapter):

def __init__(self, **kwargs):
super(TwitterAdapter, self).__init__(**kwargs)

CONSUMER_KEY = kwargs["consumer_key"]
CONSUMER_SECRET = kwargs["consumer_secret"]

self.client = Client(CONSUMER_KEY, CONSUMER_SECRET)

def search(self, q, count=1, result_type="mixed"):
url = "https://api.twitter.com/1.1/search/tweets.json"
url += "?q=" + q
url += "&result_type=" + result_type
url += "&count=" + str(count)

response = self.client.request(url)

return response

def count(self):
return 1

def find(self, statement_text):
data = self.search(statement_text)
result_text = data['statuses'][0]['text']

return Statement(result_text)

def filter(self, **kwargs):
"""
Returns a list of statements in the database
that match the parameters specified.
"""
statement = self.get_random()

return [statement]

def update(self, statement):
return statement

def get_random(self):
"""
Returns a random statement from the api.
"""
data = self.search("random")
statement_text = data['statuses'][0]['text']

return Statement(statement_text)

def drop(self):
"""
Twitter is only a simulated data source in
this case so it cannot be removed.
"""
pass

31 changes: 31 additions & 0 deletions examples/twitter_storage_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from chatterbot import ChatBot


chatbot = ChatBot("ChatterBot",
storage_adapter="chatterbot.adapters.storage.TwitterAdapter",
logic_adapter="chatterbot.adapters.logic.ClosestMatchAdapter",
io_adapter="chatterbot.adapters.io.TerminalAdapter",
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.
'''

user_input = "Type something to begin..."

print(user_input)

while True:
try:
user_input = chatbot.get_input()

bot_input = chatbot.get_response(user_input)

except (KeyboardInterrupt, EOFError, SystemExit):
break

0 comments on commit 0af2932

Please sign in to comment.