Sun Jun 21 2020Quickly Archive Public Twitter Accounts

I recently discovered that if a tweet link is shared on Telegram, it will stay there forever (possibly) even though the account's long gone or the tweet is deleted.

Since Twitter API allows to access a user's recent 3000 tweets, this gave me an idea to create a Telegram bot to archive other people's tweets.

I used Tweepy's Cursor method to access someone else's timeline, particularly their own tweets by implementing this bit:

username = "ekrem_imamoglu"
# This will get the tweets of <>
user_tweets = tweepy.Cursor(api.user_timeline, id=username).items()
for tweet in user_tweets:

This snippet can eventually be used for storing and analysing information of accounts on Twitter. In this script, I am passing each tweet to the conversation so the Telegram hosts will open up a connection and index the data to their database.

I am putting the full code below, I believe the lines are pretty much self-explanatory, but I added comments where necessary.

import telebot
import time
import tweepy
import os

# Twitter App keys
consumer_key = os.environ.get('CONSUMER_KEY')
consumer_secret = os.environ.get('CONSUMER_SECRET')

# Twitter access keys
access_token = os.environ.get('ACCESS_TOKEN')
access_token_secret = os.environ.get('ACCESS_TOKEN_SECRET')

# Authorize and initiate the Twitter API
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

# Bot keys and initialize the bot
api_token = os.environ.get('BOT_TOKEN')
bot = telebot.TeleBot(api_token)

def tweet_archive(message):
	Listener to extract information from Twitter
	The command message needs to be => "/user <TWITTER_USERNAME>"
	msg = message.text.split(' ')
	if len(msg) > 1 and len(msg) < 3:
		username = msg[1]
			# Use tweepy cursor to extract data
			user_tweets = tweepy.Cursor(api.user_timeline, id=username).items()
			i = 0
			for tweet in user_tweets:
				bot.send_message(, tweet.text)
				i += 1
				# Ensuring to not get HTTP 429 error by Telegram
				# The bot rests in every 75 tweets
				if i % 75 == 0:
		except Exception as e:
			bot.send_message(, 'Error happened.')
			# Sending an exception log to my account
			bot.send_message(86306879, str(e))
			bot.send_message(, 'Archive completed.')
		bot.send_message(, 'The format of this function is like this: "/user <TWITTER_USERNAME>".')


The repository can be forked here, and the following steps need to be done to create your bot.

  1. Create a telegram bot by messaging /newbot to BotFather.
  1. Register to Twitter Developer Portal and create an app
  1. Install libraries python install -r requirements.txt
  1. Change the environment variables with your ones
  1. Run the bot python
  1. Send the command to the bot, for example: /user ekremimamoglu (this command will download the latest 3000 tweets of the Mayor of Istanbul)
  1. Profit.

Here is an example of what will happen when you finish and execute the script.

notion image

Stay safe and have a nice day!


Stay up-to-date

Subscribe to my newsletter and stay up-to-date. Why not? It's free.