Twitter updates via Telegram bot

Harsh
4 min readJan 13, 2021

--

Telegram has a lot of potential as a messaging app. The Bot APIs can act as assistants to perform many functions in chats and channels. At the same time, Twitter provides easy-to-use APIs for developers to publish, manage, and analyze tweets.

Why is this bot useful to you ?

  • With the flexibility the APIs offer, the potential for Twitter and Telegram in data science is endless. You can gather a lot of data via twitter handles and also have a secure way of verifying them and twitter’s policy of handling fake news if decent ( according to me ). Also , the volume of data is huge. Now you don’t have to go around gathering data , you can just scrape it in mass from twitter , via twitter APIs.
  • The twitter Crypto Guru’s #bitcoin predictions - Now I don’t need to tell you about the current re-rise of bitcoin and other cryptocurrencies that’ve risen with it and TWITTER WAS THE FIRST TO TELL YOU SO!! Twitter has been buzzing with the news of rise since early September 2020 and a lot of updates were given with strong base and supportive evidence . Following the analysis myself (and making a few bucks out of it) , I can confirm it to be a reliable source for market analysis.
  • Last but not least — WhatsApp’s new privacy has me by the balls man. I’ve decided to restrict the use of the platform and shift to telegram and signal , both of which allow open source support and wont steal your data .

Enough reasons then , lets get into the nitty-gritties

1. Scraping the tweets

Before we start collecting data for twitter, we need to become Twitter developers.

  1. Login to twitter developer section.
  2. Click ‘Apps’, then ‘Create an app’.
  3. Complete the application
  4. You can see the app that you just created, and the consumer API key and consumer secret key.
  5. At ‘Create my access token’, you can get the access token and the access token secret.

You will need the script below to mine the tweets, save the auth keys as a dictionary.

import tweepy
import datetimeauth = {'consumer_key': 'XXXXXX',
'consumer_secret':'XXXXXX',
'access_token_key':'XXXXXX',
'access_token_secret': 'XXXXXX'
}class TweetMiner(object):result_limit = 20
data = []
api = Falsedef __init__(self, keys_dict=auth, api=api, result_limit = 20):

self.twitter_keys = keys_dict

auth = tweepy.OAuthHandler(keys_dict['consumer_key'], keys_dict['consumer_secret'])
auth.set_access_token(keys_dict['access_token_key'], keys_dict['access_token_secret'])

self.api = tweepy.API(auth)
self.twitter_keys = keys_dict

self.result_limit = result_limitdef mine_user_tweets(self, user="linusnhh",
mine_rewteets=False,
max_pages=5):data = []
last_tweet_id = False
page = 1

while page <= max_pages:
if last_tweet_id:
statuses = self.api.user_timeline(screen_name=user,
count=self.result_limit,
max_id=last_tweet_id - 1,
tweet_mode = 'extended',
include_retweets=True
)
else:
statuses = self.api.user_timeline(screen_name=user,
count=self.result_limit,
tweet_mode = 'extended',
include_retweets=True)

for item in statuses:mined = {
'tweet_id': item.id,
'name': item.user.name,
'screen_name': item.user.screen_name,
'retweet_count': item.retweet_count,
'text': item.full_text,
'mined_at': datetime.datetime.now(),
'created_at': item.created_at,
'favourite_count': item.favorite_count,
'hashtags': item.entities['hashtags'],
'status_count': item.user.statuses_count,
'location': item.place,
'source_device': item.source
}

try:
mined['retweet_text'] = item.retweeted_status.full_text
except:
mined['retweet_text'] = 'None'
try:
mined['quote_text'] = item.quoted_status.full_text
mined['quote_screen_name'] = status.quoted_status.user.screen_name
except:
mined['quote_text'] = 'None'
mined['quote_screen_name'] = 'None'

last_tweet_id = item.id
data.append(mined)

page += 1

return data

With the above TweetMiner function, we can start collecting tweets with just two lines of codes.

Tweet = TweetMiner(result_limit = 10)
uk_tweets = Tweet.mine_user_tweets(user=’BuyUcoin’ , max_pages=1)

Import pandas and run the following code to view the data in a dataframe

tweets_df = pd.DataFrame(uk_tweets)

Aaaand , Voila — You can extract the desired data from the dataframe.

Now , i needed to segregate the data according to dates. I.e — i want only todays latest tweet and not old tweets. you can do that via —

tweets_df = tweets_df[tweets_df[‘created_at’].astype(str).str.contains(str(“2021–01–13”))]

optional — Segregate it further via

tweets_df[‘date’] = tweets_df.created_at.dt.strftime(‘%Y-%m-%d’)
tweets_df = tweets_df[[‘screen_name’, ‘date’,’text’]].reset_index(drop=True)
tweets_df = tweets_df.apply(pd.to_numeric, errors=’ignore’)

DataFrame should look like this by now
DataFrame should look like this now

2. Sending tweets Via telegram

We have the message now! We need to figure out how we can send that msg to telegram with Python. To start, we need to create a telegram bot.

  1. On Telegram, search for ‘BotFather’ and send a ‘/start’ message.
  2. To create new bot, Send another ‘/newbot’ message, then follow the instructions to configure a name and a username. Your user name will be the bot_chatID.
  3. send ‘/token’ to get the token that access to HTTP API and then save it as bot_token.

What we are doing here is to use bot to send message to a target channel. So the bot_chatID should be the channel id. Remember to make sure you bot is the admin of the channel. Otherwise, the bot allowed to publish content to the channel.

import requests
def telegram_bot_sendtext(bot_token, bot_chatID, bot_message):
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
response = requests.get(send_text)

And there it is , your message on telegram

Additional info — to run the bot recurringly , I have put the entire code under an infinite loop and a try/except block. also , also I wrote a short code to prevent the bot from sending the same tweet again. Have a look at the complete code — https://github.com/hahaharsh7/twitter-to-telegram-bot

--

--