In this tutorial, we’ll build a simple system that detects when a new brand ranks for a tracked keyword and send an alert to a Slack channel.
Code examples for blog post: https://serpapi.com/blog/build-a-slack-bot-that-alerts-you-when-a-new-company-ranks-for-keywords/
To start, you’ll need a SerpApi account, a Slack workspace (and a Slack Incoming Webhook URL), and Python installed.
- Go to Slack → Apps
- Search for Incoming Webhooks
- Create a webhook for your channel
You will get a Webhook URL like: https://hooks.slack.com/services/XXXX/XXXX/XXXX
Save this, as we'll need it later.
- Install SerpApi's new Python serpapi library, the python-dotenv library, tldextract library (for parsing the domains from URLs), and the schedule library in your environment:
pip install serpapi python-dotenv schedule tldextract
-
To begin scraping data, create a free account on serpapi.com. You'll receive 250 free search credits each month to explore the API. Get your SerpApi API Key from this page.
-
[Optional but Recommended] Set your API key in an environment variable, instead of directly pasting it in the code. Refer here to understand more about using environment variables. For this tutorial, I have saved the API key in an environment variable named "SERPAPI_API_KEY" in my .env file.
-
[Optional but Recommended] Set up your Slack Webhook URL as an an environment variable as well. In your .env file, this will look like this:
SERPAPI_API_KEY=<YOUR PRIVATE API KEY>
SLACK_WEBHOOK_URL=<YOUR PRIVATE WEBHOOK URL>
- Set constants as needed in the code file
get_companies_ranking_for_keywords.py:
SERPAPI_KEY = os.environ["SERPAPI_API_KEY"] -> Set the environment variable OR replace with your API key if you're not using environment variables
queries = [] # List of queries for which you want to find the updated domains
DB_FILE = "known_domains.json"
SLACK_WEBHOOK = os.environ["SLACK_WEBHOOK_URL"] -> Set the environment variable OR replace with your API key if you're not using environment variables
Head to the project folder and run the code file using python get_companies_ranking_for_keywords.py
This will start the scheduler. To keep the tasks running even after you close your terminal or IDE, you can choose to run the script as a persistent background process. For linux/macOS, use disown to detach the script from the terminal, e.g., python3 script.py & disown.
If you want to just test your implementation, comment the scheduler related lines and just add a line to call the check_for_new_domains() function.
I ran the code twice with QUERY_LIST = ["ai images tool", "ai image generators", "image creation ai tools"]
The first time, I got some new pages (because a few new companies started ranking for the queries since when I ran it last) The next time, since all the webpages were already seen, I got a message letting me know that no new pages were detected.
These were the alerts I received:
You can use this to instantly find out when new companies start ranking for important keywords that matter for your brand.