
What this tutorial covers
- Access and extract website posts and contents using Browserbase
- Write scheduled functions and APIs with Val Town
- Send automated Slack messages via webhooks
Getting started
In this tutorial, you’ll need aBrowserbase
Browserbase is a developer platform to run, manage, and monitor headless browsers at scale. This tutorial uses Browserbase to navigate and extract data from different news sources. It also uses Browserbase’s Proxies to provide consistent network identity across multiple browser sessions. Sign up for free to get started!Val Town
Val Town is a platform to write and deploy JavaScript. You’ll use Val Town for three things.- Create HTTP scripts that run Browserbase sessions. These Browserbase sessions will execute web automation tasks, such as navigating Hacker News and Reddit.
- Write Cron Functions (like Cron Jobs, but more flexible) that periodically run the HTTP scripts.
- Store persistent data in the Val Town provided SQLite database. This built-in database lets you track search results, so you only send Slack notifications for new, unrecorded keyword mentions.
Twitter (X)
For this tutorial, you’ll use the Twitter API to include Twitter post results.You’ll need to create a new Twitter account to use the API. It costs $100 /
month to have a Basic Twitter Developer account.
SLACK_WEBHOOK_URL, BROWSERBASE_API_KEY, and TWITTER_BEARER_TOKEN, input all of these as Val Town Environment Variables.
Creating the APIs
The same method applies to create scripts that search and extract data from Reddit, Hacker News, and Twitter. First, start with Reddit. To create a new script, go to Val Town → New → HTTP Val. The script takes in a keyword and returns all Reddit posts from the last day that include that keyword. For each Reddit post, the output should include the URL, date_published, and post title. For example:redditSearch script, start by importing Puppeteer and creating a Browserbase session with proxies enabled. Be sure to get your BROWSERBASE_API_KEY from your Browserbase settings.
- Navigate to Reddit and do a keyword search
- Extract each resulting post
title, date_published, and url.
convertRelativeDatetoString, to convert dates to a uniform date format. Import this at the top of the redditSearch script.
hackerNewsSearch, and use the Twitter API to create twitterSearch.
See all three scripts here:
Reddit → redditSearch
Hacker News → hackerNewsSearch
Twitter → twitterSearch
Creating the Cron Function
For the last step, create aslackScout cron job that calls redditSearch, hackerNewsSearch, and twitterSearch that runs every hour. To create the cron file, go to Val Town → New → Cron Val.
In the slackScout file, import the HTTP scripts.
-
createTable: creates the new SQLite table -
isURLInTable: for each new website returned, checks if the website is already in the table -
addWebsiteToTable: ifisURLInTableisFalse, adds the new website to the table
slackScout here.