Skip to content

sritabh/Twitter-Threader-Bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Twitter-Threader-Bot

Table of Contents

  • Surfing with the bot(Running)
  • Bot Commands
  • Bot Utility Site
  • How to use?

  • About

    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.

    Arguments:

    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

    Attributes:

    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

    Methods:

    to_dict()

    return dictionary of all the Attributes
    Creates a tweet object to store details of a tweet.

    Arguments:

    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

    Attributes:

    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

    Methods:

    to_dict()

    return dictionary of all the Attributes

    Arguments:

    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

    Attributes:

    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

    Methods:

    compileTweets()

    returns array of Tweet object

    tweetCompilerBottom(since_id=None)

    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.

    compileThread()

    returns userThread object

    save(threadData)

    Saves threadData to database.
    args:
    threaData - dictionary returned by to_dict method of userThread class.

    getThreadID()

    returns id of parent tweet of the thread as string

    Arguments:

    cred - google firebase firestore credentials

    Attributes:

    cred - contains google firebase firestore credentials
    db - Database firestore client

    Methods:

    initialize()

    initialize the firebase app through the given credentials

    botWorkerUtils(read=True,data=None)

    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

    documentExists(thread_id:"Thread parent id",thread_len:"Length of fetched thread")

    Returns true when document with given thread_id exists in database and has length greater than thread_len

    Args:

    thread_id - id of the parent tweet of the thread
    thread_len - length of the thread

    storeData(thread_id,data:"dictionary")

    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

    Args:

    thread_id - id of the parent tweet of the thread
    data - dictionary containing parameters returned by to_dict() of userThread class.

    Arguments:

    file_name - name of the file to store the since_id

    Attributes:

    since_id = the id of the last request tweet

    Methods:

    retrieve_since_id(file_name="since_id.txt")

    returns the since_id stored in the file

    args:
    file_name - name of the file where since_id is stored

    store_since_id(since_id, file_name="since_id.txt")

    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

    read_write_since_id(read=True,data=None)

    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

    fetchTweets()

    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

    run()

    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)

    sendResponse(text,request_username,rquest_id)

    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

    sendResponseDirectMessage(self,text,id)

    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

    Arguments:

    bot - ThreaderBot object.
    returns random response text

    Arguments:

    link - link of the Thread
    tweetText - text of a tweet to be included with response text
    thread_len - length of the thread

    Set up credentials

    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

    bot will respond with pong

    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.
    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.

    About

    A twitter bot to view twitter thread more comfortably

    Resources

    Stars

    Watchers

    Forks

    Releases

    No releases published

    Packages

    No packages published

    Languages