-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added storage adapter that reads tweets from twitter api.
- Loading branch information
1 parent
a49d7f0
commit 0af2932
Showing
3 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|