"Google Alerts" for Hacker News
@dylburger
code:
data:privatelast updated:2 years ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 200,000+ developers using the Pipedream platform
steps.
trigger
Cron Scheduler
Deploy to configure a custom schedule
This workflow runs on Pipedream's servers and is triggered on a custom schedule.
steps.
nodejs
auth
to use OAuth tokens and API keys in code via theauths object
(auths.slack)
params
HN Keywords

Search for new HN stories with these keywords in the title (case-insensitive)

string ·params.keywords
Slack Channel

Slack channel to send stories to

string ·params.channel
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across invocations with$checkpoint.
async (event, steps, params, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
}
59
const axios = require('axios')
const allSettled = require('promise.allsettled')
const get = require("lodash.get")
const { WebClient } = require('@slack/web-api')

// Keywords are comma-separated, so we convert into an array
const { keywords, channel } = params
var KEYWORDS = keywords.split(",").map(k => k.trim())

const HN_BASE_URL = "https://hacker-news.firebaseio.com/v0/"

// Fetch all top HN stories, filtering by keywords,
const topStories = (await axios.get(`${HN_BASE_URL}/topstories.json`)).data
if (!topStories) {
  $end("No top stories found")
}

const responses = await allSettled(
  topStories.map(topStory => 
    axios.get(`${HN_BASE_URL}/item/${topStory}.json`)
  )
)

// Pull previously-posted stories from this.$checkpoint.
// See https://docs.pipedream.com/workflows/steps/code/#step-level-state-this-checkpoint
const previouslyPostedStories = get(this, '$checkpoint', [])

const matchingStories = []
for (response of responses) {
  const story = response.value.data
  for (keyword of KEYWORDS) {
    const re = new RegExp(keyword, 'gi')
    if (story.title.match(re) && !previouslyPostedStories.includes(story.id)) {
      console.log(`MATCHING TITLE: ${story.title}`)
      matchingStories.push(story)
    }
  }
}

if (!matchingStories.length) {
  $end("No new stories matching keywords")
}

// Send to Slack
const web = new WebClient(auths.slack.token)

for (story of matchingStories) {
  const storyURL = story.url ? story.url : `https://news.ycombinator.com/item?id=${story.id}`
  const text = `New Story on HN matching your keywords: <${storyURL}|${story.title}> (<https://news.ycombinator.com/item?id=${story.id}|comments>)`
  await web.chat.postMessage({
    channel,
    text,
  })
}

// Finally, store the processed stories in state so we don't process them next time
this.$checkpoint = previouslyPostedStories.concat(matchingStories.map(s => s.id))