11
12 try {
13 const DISCORD_PUBLIC_KEY = process.env.discordPublicKey;
14
15 if (!DISCORD_PUBLIC_KEY) {
16 throw new Error("Discord webhook public key is not set");
17 }
18
30
31 // Convert public key and signature to Uint8Array
32 const publicKeyBytes = sodium.from_hex(DISCORD_PUBLIC_KEY);
33 const signatureBytes = sodium.from_hex(signature);
34
44 }
45
46 // Parse the incoming JSON payload from the Discord webhook
47 const payload = await JSON.parse(body);
48
53
54 // Log the entire payload to the console
55 console.log("Discord Webhook Received:", JSON.stringify(payload, null, 2));
56
57 // Respond with a 200 OK to acknowledge receipt
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 = "\"val town\" OR \"val.town\" OR \"val.run\"";
6const DISCORD_API_KEY = Deno.env.get("mentionsDiscord");
7const SERP_API_KEY = Deno.env.get("SERP_API_KEY");
8
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 }
59 console.log("Relevant Reddit Posts:", content);
60
61 // Send Discord notification (only if isProd is true)
62 if (isProd) {
63 await discordWebhook({
64 url: DISCORD_API_KEY,
65 content,
66 });
67 console.log("Discord notification sent successfully.");
68 }
69 } catch (error) {
5This template will help you:
61. Search Reddit for specific keywords within a defined time range.
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/).
12
13## Example
14This val tracks mentions of "Val Town" and related terms on Reddit, filtering results from the last 7 days and sending alerts to a Discord webhook.
15
16
52
53### 5. Set Up Your Notification Method
54This template uses a Discord webhook for notifications. You can update this to your preferred platform:
55
56**Create a Discord webhook following [this guide](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks).**
57Save your webhook URL in your Val Town environment variables (you can find this on the left sidebar):
58- Key: `mentionsDiscord`
59- Value: Your Discord webhook URL.
60Notifications will be sent using this function:
61```
62 await discordWebhook({
63 url: Deno.env.get("mentionsDiscord"),
64 content,
65 });
66```
67
68To switch to another platform (e.g., Slack, email, or custom webhooks), replace the discordWebhook call with the appropriate integration ((e.g., [`@std/email`](https://docs.val.town/std/email/), [Slack](https://docs.val.town/integrations/slack/send-messages-to-slack/), or [anywhere else](https://docs.val.town/guides/creating-a-webhook/))
69
70---
9 let redirectUrl = DEFAULT_REDIRECT_TARGET; // Assume default redirect initially
10
11 // Check if User-Agent exists and contains 'twitter' or 'discord' (case-insensitive)
12 if (userAgent) {
13 const lowerCaseUserAgent = userAgent.toLowerCase();
14 if (
15 lowerCaseUserAgent.includes("twitter")
16 || lowerCaseUserAgent.includes("discord")
17 ) {
18 // If it matches, set the special redirect target
12This "migration" runs once on every app startup because it's imported in `index.ts`. You can comment this line out for a slight (30ms) performance improvement on cold starts. It's left in so that users who fork this project will have the migration run correctly.
13
14SQLite has much more limited support for altering existing tables as compared to other databases. Often it's easier to create new tables with the schema you want, and then copy the data over. Happily LLMs are quite good at those sort of database operations, but please reach out in the [Val Town Discord](https://discord.com/invite/dHv45uN5RY) if you need help.
15
16## Queries
12This "migration" runs once on every app startup because it's imported in `index.ts`. You can comment this line out for a slight (30ms) performance improvement on cold starts. It's left in so that users who fork this project will have the migration run correctly.
13
14SQLite has much more limited support for altering existing tables as compared to other databases. Often it's easier to create new tables with the schema you want, and then copy the data over. Happily LLMs are quite good at those sort of database operations, but please reach out in the [Val Town Discord](https://discord.com/invite/dHv45uN5RY) if you need help.
15
16## Queries
12This "migration" runs once on every app startup because it's imported in `index.ts`. You can comment this line out for a slight (30ms) performance improvement on cold starts. It's left in so that users who fork this project will have the migration run correctly.
13
14SQLite has much more limited support for altering existing tables as compared to other databases. Often it's easier to create new tables with the schema you want, and then copy the data over. Happily LLMs are quite good at those sort of database operations, but please reach out in the [Val Town Discord](https://discord.com/invite/dHv45uN5RY) if you need help.
15
16## Queries
12This "migration" runs once on every app startup because it's imported in `index.ts`. You can comment this line out for a slight (30ms) performance improvement on cold starts. It's left in so that users who fork this project will have the migration run correctly.
13
14SQLite has much more limited support for altering existing tables as compared to other databases. Often it's easier to create new tables with the schema you want, and then copy the data over. Happily LLMs are quite good at those sort of database operations, but please reach out in the [Val Town Discord](https://discord.com/invite/dHv45uN5RY) if you need help.
15
16## Queries
12This "migration" runs once on every app startup because it's imported in `index.ts`. You can comment this line out for a slight (30ms) performance improvement on cold starts. It's left in so that users who fork this project will have the migration run correctly.
13
14SQLite has much more limited support for altering existing tables as compared to other databases. Often it's easier to create new tables with the schema you want, and then copy the data over. Happily LLMs are quite good at those sort of database operations, but please reach out in the [Val Town Discord](https://discord.com/invite/dHv45uN5RY) if you need help.
15
16## Queries
12This "migration" runs once on every app startup because it's imported in `index.ts`. You can comment this line out for a slight (30ms) performance improvement on cold starts. It's left in so that users who fork this project will have the migration run correctly.
13
14SQLite has much more limited support for altering existing tables as compared to other databases. Often it's easier to create new tables with the schema you want, and then copy the data over. Happily LLMs are quite good at those sort of database operations, but please reach out in the [Val Town Discord](https://discord.com/invite/dHv45uN5RY) if you need help.
15
16## Queries