Twitter has many great people sharing valuable information in the form of threads. This is a twitter bot which can be used to compile threads and view it at one place or share it with others, this can also be used to print the thread and save it as pdf format for later consumption.
Creates a userThread object containing details about user and the thread together.
-
id - profile id of thread owner
name - name of the thread owner
username - username of the thread owner
profile_img - link to the profile image of thread owner
tweets - Array of tweets in the thread -
id - profile id of thread owner
name - name of the thread owner
username - username of the thread owner
profile_img - link to the profile image of thread owner
tweets - Array of tweets in the thread
- return dictionary of all the Attributes
-
text - full text of the tweet
date - date of the tweet
medias - array of dictionary of media with values:
tweet_id - tweet's unique id
urls - Array of url included in tweet
-
text - full text of the tweet
date - date of the tweet
medias - array of dictionary of media with values:- type - video/photo
- media_url_https - url of media file https(shorten)
- media_url - url of media file(shorten)
- expanded_url - expanded url
tweet_id - tweet's unique id
urls - Array of url included in tweet
- return dictionary of all the Attributes
-
tweet_id - id of the tweet under which request was method-ThreaderBot
user_id - twitter id of the thread owner
thread_request_id - id of the user's tweet who requested thread compile
easy_compile - rolling up to the parent (use when thread is too old)
max_tweets_to_look - max tweets you want to look for to reach to the thread -
id - id of the parent tweet_id
tweets - Array of Tweet object
tweet_id - id of the tweet under which request was made.
user_id - twitter id of the thread owner
thread_request_id - id of the user's tweet who requested thread compile
easy_compile - rolling up to the parent (use when thread is too old)
max_tweets_to_look - max tweets you want to look for to reach to the thread -
- returns array of Tweet object
- compiles the bottom tweet till since_id and returns array of Tweet object (all tweets including upper part)
-
- args:
- since_id - id of the tweet till which compiling is needed
- fetchBottomThread(tweets_track,tweet_id=None)
- returns the array of Tweet object possibly the longest thread till the tweet_id
-
- args:
-
tweets_track - contains all the tweets as dictionary with key as tweet id and elements tweet containing Tweet object and in_reply_to_status_id till tweet_id from threads owner profile.
tweet_id - id of the tweet under which request was made.
- returns userThread object
- Saves threadData to database.
-
- args:
- threaData - dictionary returned by to_dict method of userThread class.
- returns id of parent tweet of the thread as string
-
cred - google firebase firestore credentials
-
cred - contains google firebase firestore credentials
db - Database firestore client -
-
initialize the firebase app through the given credentials
-
utility for the bot to work with,fetches since_id from the database which ThreaderBot class uses to find the mentions in timeline.
- args:
-
read = if true it will read the data from the firestore location botData/lastMentions/since_id
data = if method is writing means read = False, this will be stored in the firestore at location botData/lastMentions/since_id
-
Returns true when document with given thread_id exists in database and has length greater than thread_len
-
-
thread_id - id of the parent tweet of the thread
thread_len - length of the thread
-
stores the thread data in database with given thread id if it doesn't exist before or data has length greater than stored thread length
-
-
thread_id - id of the parent tweet of the thread
data - dictionary containing parameters returned by to_dict() of userThread class.
-
file_name - name of the file to store the since_id
-
since_id = the id of the last request tweet
-
-
returns the since_id stored in the file
-
- args:
- file_name - name of the file where since_id is stored
-
Stores the since_id in given file_name
-
- args:
-
file_name - name of the file where since_id is stored
since_id - the id of the last request tweet
-
reads and write the since id from firestore, useful when working with platform like heroku which doesn't actually modifies the file.
- args:
-
read = if true it will read the data from the firestore location botData/lastMentions/since_id
data = if method is writing means read = False, this will be stored in the firestore at location botData/lastMentions/since_id
-
Fetches the tweet in timeline and looks for mentions of own id till the since_id i.e the last mention and returns the array of all mentions
Note:-Retweet will trigger this aswell -
returns false if fetchTweets() method returns empty array else return array of requests as tuples if it's a reply under a tweet with text containing compile or ezcompile
tuple - (in_reply_to_tweet_id,in_reply_to_user_id,request_username,request_user_id,request_id,easy_compile,dm_request)- Returned tuple elements
-
in_reply_to_status_id - id of the tweet which user has mentioned us.
in_reply_to_user_id - user id of the tweet which user has mentioned us.
request_username - username of the twitter id who has sent the compile request(used to respond)
request_user_id - id of the user who has made the request
request_id - id of the request tweet
easy_compile - whether the compile method is ezcompile or not (bool)
dm_request - whether the request wants respone as direct message of not (bool)
- sends response/reply to request tweet
-
- args:
-
text - string containing response text
request_username - username of the request tweet
rquest_id - id of the request tweet
Note: read and write permissions are required from twitter api project
- sends response as direct message.
-
- args:
-
text - string containing response text
id - user id
Note: read and write permissions are required from twitter api project
- compiles the tweet and save it to the database then send respond with id of parent tweet
- bot - ThreaderBot object.
- returns random response text
-
link - link of the Thread
tweetText - text of a tweet to be included with response text
thread_len - length of the thread
-
- You'll need Twitter API keys,if you don't have then create a twitter developer account
- Set up auth values for tweepy
API_KEY(API key)
API_SKEY(API Secret key)
ACC_Token(Access Token)
ACC_Token_Secret(Access Token Secret key)
- Setup firestore credentials by generating private key
- Check more about this here!
- Update since_id.txt(if running locally)
-
add the last tweet id which had mentioned the bot.
If you're planning to run it locally, change the ThreaderBot fetchTweet() method to read it from offline file using the methods available in the class. - add field in your firestore botData/lastMentions/since_id(if planning to run on system like heroku)
- add the last tweet id which had mentioned the bot.
- Running
- Now simply run the bot by executing main.py,it'll be surfing.
- Bot Testing Method
<username> ping
- Thread compiling commands
-
<username> compile
- mentioning bot with this command at any part of the thread,bot will compile every part of the thread and send a reply response containing a link to the compiled thread.
<username> ezcompile
- bot will compile every tweet above the tweet(inclduing) under which it's mentioned and send a reply response containing a link to the compiled thread.
<username> compile dm
- mentioning bot with this command at any part of the thread,bot will compile every part of the thread and send a direct message response containing a link to the compiled thread.
<username> ezcompile dm
- bot will compile every tweet above the tweet(inclduing) under which it's mentioned and send a direct message response containing a link to the compiled thread.
bot will respond with pong
- The saved thread's data in database will be shown in a webpage by the utility site
- The site will generate the elements according to the thread data under the database,data will be saved under threads directly with unique threadID which is the tweet id of the parent tweet of the thread.