auths
objectreturn
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps, params, auths) => {
}
// You can use any npm package using just a require()
const axios = require("axios")
// Click Connect Account below to connect your Reddit account.
// The access token and other Reddit auth info is exposed in
// your code in the 'auths.reddit' object.
const { oauth_access_token, oauth_uid } = auths.reddit
// The "Subreddit" you enter in the form below this step
// is passed as 'params.subreddit' here.
const { subreddit } = params
// Saving data to properties of 'this' exports that data,
// making it available to other steps.
this.story = (await axios({
method: "GET",
url: `https://oauth.reddit.com/r/${subreddit}/hot`,
headers: {
"Authorization": `Bearer ${oauth_access_token}`,
"User-Agent": `pipedream/u/${oauth_uid}`
}
})).data.data.children[0].data // Story data is deeply nested in the Reddit API response
auths
objectreturn
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps) => {
}
// We save the stories we've seen before to the $checkpoint
// variable. You can think of $checkpoint like a key-value
// store specific to a workflow. You can read the contents of
// $checkpoint in future executions of our workflow.
// See https://docs.pipedream.com/workflows/steps/code/#managing-state
const { id } = steps.fetch_top_reddit_post.story
if ($checkpoint && $checkpoint.lastStoryId && $checkpoint.lastStoryId === id) {
$end(`We've already processed story ${id}. Exiting`)
}
$checkpoint = {
lastStoryId: id,
}
auths
objectreturn
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps, params, auths) => {
}
const axios = require("axios")
const { WebClient } = require('@slack/web-api')
// The "Channel" you enter in the form below this step
// is passed as params.channel here.
const { channel, bot_username } = params
const { permalink, title } = steps.fetch_top_reddit_post.story
const url = `https://reddit.com${permalink}`
const text = `*${title}* : ${url}`
// Send to Slack
const web = new WebClient(auths.slack.oauth_access_token)
await web.chat.postMessage({
channel,
text,
as_user: false,
username: bot_username,
})