Create a Custom Twitter Tweet Alert System with Python
Do you follow hundreds or thousands of Twitter accounts and miss important announcements from Google Search? Were you late to learn about the latest core update? In this tutorial I’ll show you how to build a simple Twitter alert system using Python. We’ll use the advertools module, created by Elias Dabbas, to connect to the Twitter API and scan today’s posts from specific accounts for keywords you care about. When a match is found you can take many actions; here I’ll show how to send either an email alert or an SMS. Never miss a core update announcement again!
Table of Contents
Requirements and Assumptions
- Python 3 is installed and basic Python syntax is understood.
- Access to a Linux installation (I recommend Ubuntu) or to Google Colab.
- A Twitter Developer account with API access.
- A Gmail account with yagmail configured, or a Textbelt account for SMS.
Note: If you copy and paste the code into your own script, double-check the indentation. Python is sensitive to indentation, and WordPress or copying may not preserve it.
Install Python Modules
First, install the advertools and yagmail modules. In your terminal, run the following commands. If using Google Colab, add an exclamation mark at the start of each line.
pip3 install advertools
pip3 install yagmail
Now let’s import the modules we’re going to use.
- advertools: Connects to the Twitter API
- pandas: Handles the dataframe response returned by advertools
- datetime: Grabs today’s date for the Twitter date range
- yagmail: Sends the email alert
import pandas as pd import advertools as adv import yagmail from datetime import date
Twitter API
To use the Twitter API you need to have registered a Twitter Developer account. Create a developer project and request API access — this can take a few days. After you’re approved, head to your project and click on the “Keys and Token” menu. There you will find your Key, Secret, and tokens. Fill in the code below.
auth_params = {
'app_key': '',
'app_secret': '',
'oauth_token': '',
'oauth_token_secret': '',
}
Create Send Function
Now create the alert function, which supports monitoring multiple accounts. I’ll point out places to change later. The function accepts three parameters: the Twitter handle(s) to monitor, the detected tweets, and the alert method. If you monitor more than two accounts, avoid SMS because messages may become long.
Method 1 sends an email using yagmail. It requires a small configuration; follow the yagmail documentation. Replace YOUR_GMAIL_ADDRESS and TO_EMAIL_ADDRESS with your addresses. The print statement is kept for simple diagnostics.
def send_alert(emailmessage, method):
if method == 1:
body = emailmessage
yag = yagmail.SMTP("YOUR_GMAIL_ADDRESS")
yag.send(
to="TO_EMAIL_ADDRESS",
subject="Twitter Alert",
contents=body)
print("Email sent")
Method 2 sends an SMS via Textbelt. It’s inexpensive and simple (for example, $10 for 1,000 messages). Add your phone number and API key in the code below.
if method == 0:
resp = requests.post('https://textbelt.com/text', {
'phone': '',
'message': twitterhandle + " tweeted out: " + tweet,
'key': '',
})
print(resp.json())
Initiate Advertools
With the alert function ready, return to the start. First, grab today’s date in the default yyyy-mm-dd format. This script is intended to run once daily, so we only detect tweets posted today. Then invoke the advertools Twitter functions using the authentication set earlier.
today = str(date.today()) adv.twitter.set_auth_params(**auth_params)
Setup Monitor List and Storage Dataframe
Next, define a dictionary mapping Twitter handles to keywords (use regex for multiple keywords) and create an empty dataframe to store any matches.
alerts_list = {"searchliaison":"core|update|algorithm","googlesearchc":"search/sconsole"}
df1 = pd.DataFrame(columns = ['TwitterHandle', 'Message'])
Grab Twitter Timelines and Filter
Loop over the accounts in the dictionary and use the advertools Twitter API function to fetch each account’s timeline.
for keys,values in alerts_list.items(): df = adv.twitter.get_user_timeline(screen_name=keys,tweet_mode="extended")
Convert tweet_created_at to a string since it is returned as a datetime object. Then filter the timeline to tweets that match the keywords and were posted today.
df['tweet_created_at'] = df['tweet_created_at'].astype('string')
df = df[df['tweet_full_text'].str.contains(values,regex=True) & df['tweet_created_at'].str.contains(today)]
If the filtered dataframe contains at least one row, append those results to the master dataframe df1 that stores all matched tweets across the accounts you are tracking, then continue to the next handle.
if len(df.index) > 0:
df1 = df1.append({'TwitterHandle' : keys, 'Message' : df['tweet_full_text']}, ignore_index = True)
Build Alert and Send Message
With matched tweets in df1, convert them to a dictionary using zip() so you can format a message for sending.
getlist = dict(zip(df1['TwitterHandle'].tolist(),df1['Message'].tolist())) emailmessage = ""
Finally, check whether the master dataframe has any matched records. If it has none, there are no alerts for today. If it has rows, concatenate each entry into emailmessage and choose the send method: method = 1 sends an email, method = 0 sends an SMS. Use SMS only when tracking one or two accounts.
if len(df1.index) > 0:
for key, value in getlist.items():
emailmessage += key + ": " + value + "\n\n"
method = 1
send_alert(emailmessage,method)
Automating the System
Automate the script so you don’t run it manually. If you are running locally, follow the instructions below. If you are using Google Colab, deploy the script to Google Cloud Functions and schedule it with Google Cloud Scheduler.
Linux provides a simple scheduler via crontab. The crontab contains entries that dictate when scripts execute. You can schedule the script for any minute, hour, day, or month. To edit the crontab, run:
crontab -e
This typically opens the crontab file in the vi editor. On a blank line at the bottom, add the following to run the script at midnight every Sunday. Use crontab.guru to customize the schedule and replace the path to your script.
0 0 * * SUN /usr/bin/python3 PATH_TO_SCRIPT/filename.py
If you want a log file recording each run, use this variant and set your log path:
0 0 * * SUN /usr/bin/python3 PATH_TO_SCRIPT/filename.py > PATH_TO_FILE/FILENAME.log 2>&1
Save the crontab and you’re done. Note that the computer must be powered on when the cronjob is scheduled to run.
Conclusion
You now have a framework to monitor your favorite accounts for keywords you care about. You can extend it with additional alert methods, more accounts, and more keywords. No longer will you miss a core update announcement or a new Search Console feature! Please follow me on Twitter for feedback and to see interesting ways to extend the script. Enjoy!
- Evaluate Subreddit Posts in Bulk Using GPT4 Prompting - December 12, 2024
- Calculate Similarity Between Article Elements Using spaCy - November 13, 2024
- Audit URLs for SEO Using ahrefs Backlink API Data - November 11, 2024













