72. Send notifications to your preferred platform (Discord, Slack, email, etc.)
8
9Reddit does not have an API that allows users to scrape data, so we are doing this with the Google Search API, [Serp](https://serpapi.com/).
10
11---
29
30---
31### 3. Get a SerpApi Key
32This template requires a [SerpApi](https://serpapi.com/) key to search Reddit posts via Google search results.
33
341. **Get a SerpApi key**:
35 - Sign up at [SerpApi](https://serpapi.com/) to create an account.
36 - Generate an API key from your account dashboard.
37
382. **Add the SerpApi key to your environment variables**:
39 - Go to your [Val Town environment variables](https://www.val.town/settings/environment-variables).
40 - Add a new key:
41 - Key: `SERP_API_KEY`
42 - Value: Your SERP API key.
43
44Without this key, the val will not function correctly.
75
76### NOTE: Usage Limits
77- **SerpApi:** Free SerpApi accounts have monthly call limits.
14- Twilio account (for SMS notifications)
15 - You can create an account with $15 free credits at https://www.twilio.com/ without putting in your credit card
16- SerpApi key (for searching Reddit)
17
18---
20**Customize all of these according to your preferences:**
21```
22 const apiKey = Deno.env.get("SERP_API_KEY");
23 const twilioSid = Deno.env.get("TWILIO_ACCOUNT_SID");
24 const twilioToken = Deno.env.get("TWILIO_AUTH_TOKEN");
19
20 try {
21 const response = await fetch(`https://api.twilio.com/2010-04-01/Accounts/${accountSid}/Messages.json`, {
22 method: "POST",
23 headers: {
35
36 if (!response.ok) {
37 throw new Error(`Twilio API error: ${responseText}`);
38 }
39
1import { searchWithSerpApi } from "https://esm.town/v/charmaine/searchWithSerpApi";
2import { discordWebhook } from "https://esm.town/v/stevekrouse/discordWebhook";
3
4// Customize your search parameters
5const KEYWORDS = "\"node\" OR \"node.js\"";
6const DISCORD_API_KEY = Deno.env.get("mentionsDiscord");
7const SERP_API_KEY = Deno.env.get("SERP_API_KEY");
8
9// Set isProd = false for testing and = true for production
13
14export async function redditAlert({ lastRunAt }: Interval) {
15 if (!SERP_API_KEY || !DISCORD_API_KEY) {
16 console.error("Missing SERP_API_KEY or Discord webhook URL. Exiting.");
17 return;
18 }
19
20 // Determine the time frame for the search
21 // Details on as_qdr: https://serpapi.com/advanced-google-query-parameters#api-parameters-advanced-search-query-parameters-as-qdr
22 const timeFrame = isProd
23 ? lastRunAt
27
28 try {
29 const response = await searchWithSerpApi({
30 query: KEYWORDS,
31 site: "reddit.com",
32 apiKey: SERP_API_KEY,
33 as_qdr: timeFrame,
34 });
62 if (isProd) {
63 await discordWebhook({
64 url: DISCORD_API_KEY,
65 content,
66 });
8
91. Click `Fork`
102. Change `location` (Line 4) to describe your location. It accepts fairly flexible English descriptions which it turns into locations via [nominatim's geocoder API](https://www.val.town/v/stevekrouse/nominatimSearch).
113. Click `Run`
12
1import { searchWithSerpApi } from "https://esm.town/v/charmaine/searchWithSerpApi";
2import { discordWebhook } from "https://esm.town/v/stevekrouse/discordWebhook";
3
5const query = "\"node.js\" OR \"node\"";
6const mentionsDiscord = Deno.env.get("mentionsDiscord");
7const serpApiKey = Deno.env.get("SERP_API_KEY");
8
9export async function redditAlert({ lastRunAt }: Interval) {
10 if (!serpApiKey || !mentionsDiscord) {
11 console.error("Missing SERP_API_KEY or Discord webhook URL. Exiting.");
12 return;
13 }
15 console.log(lastRunAt);
16
17 // Calculate the `as_qdr` parameter (https://serpapi.com/advanced-google-query-parameters#api-parameters-advanced-search-query-parameters-as-qdr) or default to 7 days
18 const asQdr = lastRunAt
19 ? `d${Math.floor((Date.now() - lastRunAt.getTime()) / (24 * 60 * 60 * 1000))}`
21
22 try {
23 const response = await searchWithSerpApi({
24 query,
25 site: "reddit.com",
26 apiKey: serpApiKey,
27 as_qdr: asQdr,
28 });
72. Send notifications to your preferred platform (Discord, Slack, email, etc.)
8
9Reddit does not have an API that allows users to scrape data, so we are doing this with the Google Search API, [Serp](https://serpapi.com/).
10
11---
29
30---
31### 3. Get a SerpApi Key
32This template requires a [SerpApi](https://serpapi.com/) key to search Reddit posts via Google search results.
33
341. **Get a SerpApi key**:
35 - Sign up at [SerpApi](https://serpapi.com/) to create an account.
36 - Generate an API key from your account dashboard.
37
382. **Add the SerpApi key to your environment variables**:
39 - Go to your [Val Town environment variables](https://www.val.town/settings/environment-variables).
40 - Add a new key:
41 - Key: `SERP_API_KEY`
42 - Value: Your SERP API key.
43
44Without this key, the val will not function correctly.
75
76### NOTE: Usage Limits
77- **SerpApi:** Free SerpApi accounts have monthly call limits.
72. Send notifications to your preferred platform (Discord, Slack, email, etc.)
8
9Reddit does not have an API that allows users to scrape data, so we are doing this with the Google Search API, [Serp](https://serpapi.com/).
10
11---
29
30---
31### 3. Get a SerpApi Key
32This template requires a [SerpApi](https://serpapi.com/) key to search Reddit posts via Google search results.
33
341. **Get a SerpApi key**:
35 - Sign up at [SerpApi](https://serpapi.com/) to create an account.
36 - Generate an API key from your account dashboard.
37
382. **Add the SerpApi key to your environment variables**:
39 - Go to your [Val Town environment variables](https://www.val.town/settings/environment-variables).
40 - Add a new key:
41 - Key: `SERP_API_KEY`
42 - Value: Your SERP API key.
43
44Without this key, the val will not function correctly.
75
76### NOTE: Usage Limits
77- **SerpApi:** Free SerpApi accounts have monthly call limits.
1import { searchWithSerpApi } from "https://esm.town/v/charmaine/searchWithSerpApi";
2import { discordWebhook } from "https://esm.town/v/stevekrouse/discordWebhook";
3
5const query = "\"val town\" OR \"val.town\"";
6const mentionsDiscord = Deno.env.get("mentionsDiscord");
7const serpApiKey = Deno.env.get("SERP_API_KEY");
8
9export async function redditAlert({ lastRunAt }: Interval) {
10 if (!serpApiKey || !mentionsDiscord) {
11 console.error("Missing SERP_API_KEY or Discord webhook URL. Exiting.");
12 return;
13 }
14
15 // Calculate the `as_qdr` parameter (https://serpapi.com/advanced-google-query-parameters#api-parameters-advanced-search-query-parameters-as-qdr) or default to 7 days
16 const asQdr = lastRunAt
17 ? `d${Math.floor((Date.now() - lastRunAt.getTime()) / (24 * 60 * 60 * 1000))}`
19
20 try {
21 const response = await searchWithSerpApi({
22 query,
23 site: "reddit.com",
24 apiKey: serpApiKey,
25 as_qdr: asQdr,
26 });
1import { searchWithSerpApi } from "https://esm.town/v/charmaine/searchWithSerpApi";
2import { discordWebhook } from "https://esm.town/v/stevekrouse/discordWebhook";
3
5const query = "\"node.js\" OR \"node\"";
6const mentionsDiscord = Deno.env.get("mentionsDiscord");
7const serpApiKey = Deno.env.get("SERP_API_KEY");
8
9export async function redditAlert({ lastRunAt }: Interval) {
10 if (!serpApiKey || !mentionsDiscord) {
11 console.error("Missing SERP_API_KEY or Discord webhook URL. Exiting.");
12 return;
13 }
14
15 // Calculate the `as_qdr` parameter (https://serpapi.com/advanced-google-query-parameters#api-parameters-advanced-search-query-parameters-as-qdr) or default to 7 days
16 const asQdr = lastRunAt
17 ? `d${Math.floor((Date.now() - lastRunAt.getTime()) / (24 * 60 * 60 * 1000))}`
19
20 try {
21 const response = await searchWithSerpApi({
22 query,
23 site: "reddit.com",
24 apiKey: serpApiKey,
25 as_qdr: asQdr,
26 });