Skip to content

Instantly share code, notes, and snippets.

@hypnoJerk
Last active July 3, 2023 16:51
Show Gist options
  • Save hypnoJerk/8614bd7e4947f190161b to your computer and use it in GitHub Desktop.
Save hypnoJerk/8614bd7e4947f190161b to your computer and use it in GitHub Desktop.
from_twitter_to_telegram

Twitter >> Telegram

Auto post tweets from Twitter, to a Telegram bot

This script simply takes a twitter user name, and fetches the latest tweets from their timeline. Then reposts those tweets to a telegram bot(or channel via a bot)

####Requirements: You need Twython installed. Twython is a twitter-python API

####Notes: The script makes a txt file that holds the tweet ID of the latest tweet it recieved. That way, we can compare the tweet ID later, and make sure that we only get the newest tweets, since last time we ran the script.

You can schedule the script to run with something like using cron jobs, every 10 min for example. I run mine at every 5 min for my current bot.

####If you have Lots of Tweets Already!: You may want to adjust the count of the get_timeline request. Other wise, it may continually post up to the last 200 messages!

import os, time
import twython as Twython
from urllib import quote
## Twitter application authentication ##
# The following strings are placeholders, with dummy keys that will not work!
# Replace these keys, with your own. http://dev.twitter.com/docs/api/1.1/overview/.
APP_KEY = 'f123abcjfdj9939123abcwe90u'
APP_SECRET = '123123AbCAbc4asoiDgF8HDF123lkjreogDHFNnweDHJHNBF'
OAUTH_TOKEN = '1231231111111191919-asdfj39ejfjasljDJLf0wFhhskFHgg9'
OAUTH_TOKEN_SECRET = 'oNsdfhNFJD123123njn2A94ufdsa4JFRehf9i43FHewhf'
api = Twython.Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
latest_tweet_id = 0
## Your Telegram Bot Name ##
bot_name = 'geremysays'
## Telegram Access Token ##
telegram_token = '111101011123:Asd63Sdfh8HS3YdgQksd843'
## Twitter User Name to get Timeline ##
user_name = 'GeremySays'
def get_timeline(latest_tweet_id):
user_timeline = api.get_user_timeline(screen_name=user_name, since_id=latest_tweet_id)
return user_timeline
def writeToLog(msg):
log_file = open(bot_name+"_latest_id.txt", "w")
log_file.write(str(msg))
log_file.close()
def read_latest_id():
file_exists = os.path.exists(bot_name+"_latest_id.txt")
if file_exists is False:
writeToLog('0')
else:
log_file = open(bot_name+"_latest_id.txt", "r")
line = log_file.read()
log_file.close()
if len(str(line)) < 2:
return 0
else:
return line
def send_message(msg):
msg = quote(msg, safe='')
link = 'https://api.telegram.org/bot'+telegram_token+'/sendMessage?chat_id=@'+bot_name+'\&text="' + msg + '"'
os.system('curl '+ link)
def main():
latest_tweet_id = read_latest_id()
user_timeline = get_timeline(latest_tweet_id)
number_of_tweets = len(user_timeline)
if number_of_tweets > 0:
for i in reversed(range(0,number_of_tweets)):
if user_timeline[i]['text']:
print user_timeline[i]['text']
send_message(user_timeline[i]['text'])
time.sleep(4)
latest_tweet_id = user_timeline[0]['id']
writeToLog(latest_tweet_id)
main()
@hitesh83
Copy link

hitesh83 commented Jan 2, 2022

Hi,
Line 3 change to - from urllib.parse import quote
Line 54 print statement needs opening and closing ( ) - print (user_timeline[i]['text'])
on python3 scripts works for me after abvoe two changes.

Also for me, bot_name was not working, so used chat_id number specific to the user,

Rest all worked fine !!!

Thanks for the script !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment