1
Trigger everytime a new item text on HackerNews match at least one mention Trigger
One script reply has been approved by the moderators Verified

For every new item since last trigger (as identified by being above the last max ID), collect the ones that match at least of the mention

Created by admin 932 days ago Viewed 18531 times
In Windmill, a trigger script is designed to pull data from an external source and return all the new items since the last run. It operates without resorting to external webhooks and is typically used with schedules and states to compare the current execution to the previous one.
0
Submitted by admin Deno
Verified 932 days ago
1
import {
2
  getState,
3
  setState,
4
} from "https://deno.land/x/windmill@v1.85.0/mod.ts";
5

6
const MAX_LOOKBACK = 100;
7

8
/**
9
 * @param mentions Case **insensitive** phrases that should be searched for.
10
 */
11
export async function main(mentions: string[]) {
12
  let lastState = await getState();
13
  let maxItem = await getMaxItem();
14

15
  if (!lastState) {
16
    lastState = maxItem - MAX_LOOKBACK;
17
  }
18
  maxItem = Math.min(maxItem, lastState + MAX_LOOKBACK);
19

20
  const items = [];
21
  for (let i = lastState; i < maxItem; i++) {
22
    const item = await getItem(i);
23
    if (
24
      mentions.find((mention) => {
25
        if (!item.text) return false;
26

27
        const m = mention.trim().toLowerCase();
28
        return item.text.toLowerCase().includes(m);
29
      })
30
    ) {
31
      items.push(item);
32
    }
33
  }
34
  await setState(maxItem);
35

36
  return items;
37
}
38

39
export async function getMaxItem() {
40
  const res = await fetch("https://hacker-news.firebaseio.com/v0/maxitem.json");
41
  return Number(await res.text());
42
}
43

44
export async function getItem(id: number) {
45
  const res = await fetch(
46
    `https://hacker-news.firebaseio.com/v0/item/${id}.json`,
47
  );
48
  return res.json();
49
}
50