Search Reddit and email me matching posts and comments
@pravin
code:
data:privatelast updated:3 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 800,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.
config
auth
to use OAuth tokens and API keys in code via theauths object
params
Search keyword

Enter the keyword or phrase (in quotes) to search for in Reddit posts and comments.

 
string ·params.search_keyword
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 executions with$checkpoint.
async (event, steps, params) => {
1
2
3
4
5
6
}
7
if(typeof params.search_keyword === 'undefined') {
  $end('REQUIRED: Enter a keyword in steps.config')
}

this.search_keyword = params.search_keyword
steps.
get_checkpoint
auth
to use OAuth tokens and API keys in code via theauths object
params
Q
{{steps.config.search_keyword}}
string ·params.q
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 executions with$checkpoint.
async (event, steps, params) => {
1
2
3
4
5
6
}
7
$checkpoint = $checkpoint || {}
$checkpoint[params.q] = $checkpoint[params.q] || {postCheckpoint: 0, commentCheckpoint: 0}

this.postCheckpoint = $checkpoint[params.q].postCheckpoint
this.commentCheckpoint = $checkpoint[params.q].commentCheckpoint 
steps.
search_reddit_posts
Search Reddit posts using the Pushshift.io API. Learn more at https://github.com/pushshift/api
auth
(auths.pushift_reddit_search)
params
Q

Search term. Will search ALL possible fields

{{steps.config.search_keyword}}
string ·params.q
Size

Number of results to return (Integer <= 500)

100
integer ·params.size
Sort

Sort results in a specific order ("asc" or "desc")

string ·params.sort
After

Return results after this date. Provide an epoch value or Integer + "s,m,h,d" (i.e. 30d for 30 days)

{{steps.get_checkpoint.postCheckpoint}}
string ·params.after
Optional
code
async (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
const options = {
  url: `https://api.pushshift.io/reddit/search/submission/`,
  method: 'get',
  params: {
    ids: params.ids,
    q: params.q,
    'q:not': params['q:not'],
    title: params.title,
    'title:not': params['title:not'],
    selftext: params.selftext,
    'selftext:not': params['selftext:not'],
    size: params.size,
    fields: params.fields,
    sort: params.sort,
    sort_type: params.sort_type,
    aggs: params.aggs,
    author: params.author,
    subreddit: params.subreddit,
    after: params.after,
    before: params.before,
    score: params.score,
    num_comments: params.num_comments,
    over_18: params.over_18,
    is_video: params.is_video,
    locked: params.locked,
    stickied: params.stickied,
    spoiler: params.spoiler,
    content_mode: params.content_mode,
    frequency: params.frequency,
    metadata: params.metadata
  }
}

return (await require("@pipedreamhq/platform").axios(this, options)).data
steps.
search_reddit_comments
Search Reddit comments using the Pushshift.io API. Learn more at https://github.com/pushshift/api
auth
(auths.pushift_reddit_search)
params
Q

Search term. Will search ALL possible fields

{{steps.config.search_keyword}}
string ·params.q
Size

Number of results to return (Integer <= 500)

100
integer ·params.size
Sort

Sort results in a specific order ("asc" or "desc")

string ·params.sort
After

Return results after this date. Provide an epoch value or Integer + "s,m,h,d" (i.e. 30d for 30 days)

{{steps.get_checkpoint.commentCheckpoint}}
string ·params.after
Optional
code
async (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
const options = {
  url: `https://api.pushshift.io/reddit/search/comment/`,
  method: 'get',
  params: {
    q: params.q,
    ids: params.ids,
    size: params.size,
    fields: params.fields,  
    sort: params.sort,
    sort_type: params.sort_type,
    aggs: params.aggs,
    author: params.author,
    subreddit: params.subreddit,
    after: params.after,
    before: params.before,
    frequency: params.frequency,
    metadata: params.metadata
  }
}

return (await require("@pipedreamhq/platform").axios(this, options)).data
steps.
set_checkpoints
auth
to use OAuth tokens and API keys in code via theauths object
params
Q
{{steps.config.search_keyword}}
string ·params.q
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 executions with$checkpoint.
async (event, steps, params) => {
1
2
3
4
5
6
7
8
9
10
11
12
}
13
let maxTimestamp = steps.get_checkpoint.postCheckpoint
steps.search_reddit_posts.$return_value.forEach(function(element){
  maxTimestamp = element.created_utc > maxTimestamp ? element.created_utc : maxTimestamp
})
this.newPostCheckpoint = $checkpoint[params.q].postCheckpoint = maxTimestamp

maxTimestamp = steps.get_checkpoint.commentCheckpoint
steps.search_reddit_comments.$return_value.forEach(function(element){
  maxTimestamp = element.created_utc > maxTimestamp ? element.created_utc : maxTimestamp
})
this.newCommentTimestamp = $checkpoint[params.q].commentCheckpoint = maxTimestamp
steps.
consolidate_results
auth
to use OAuth tokens and API keys in code via theauths object
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 executions with$checkpoint.
async (event, steps) => {
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
// consolidate the post and comment search results into a standard object with the keys: title, url and content
// we'll export this object so we can use it in later steps to generate message for email, slack, etc

const messages = []
const posts = steps.search_reddit_posts.$return_value
const comments = steps.search_reddit_comments.$return_value
let i

function generateMessage(type, obj) {
  let message = {}
  // identify the type of content and the author
  message.title = `New ${type} by ${obj.author}`
  message.timestamp = new Date(obj.created_utc * 1000)
  message.url = `https://reddit.com${obj.permalink}`
  if(type === 'post') {
    // concatenate the title and post content if the message was a post
    message.content = `${obj.title}: ${obj.selftext}`
  } else if (type === 'comment') {
    // save the body if the message was a comment
    message.content = obj.body
  }
  return message
}

posts.forEach(function(element) {
  messages.push(generateMessage('post', element))
})

comments.forEach(function(element) {
  messages.push(generateMessage('comment', element))
})



if (messages.length > 0) {
  this.summary = `${posts.length} new post${posts.length === 1 ? '' : 's' } and ${comments.length} comment${posts.length === 1 ? '' : 's' }`
} else {
  $end(`No new posts or comments`)
}

return messages
steps.
format_email
auth
to use OAuth tokens and API keys in code via theauths object
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 executions with$checkpoint.
async (event, steps) => {
1
2
3
4
5
6
7
8
}
9
const results = steps.consolidate_results
let plainText = `Reddit Search Results (${steps.config.search_keyword.toUpperCase()}): ${results.summary.toUpperCase()}\n\n`
for (let i=0; i<results.$return_value.length; i++) {
  let result = results.$return_value[i]
  plainText += [result.title, result.timestamp, result.url, result.content, "\n======\n\n"].join("\n")
}
this.plainText = plainText
steps.
email_me
Customize and send an email to the email address you registered with Pipedream. The email will be sent by notifications@pipedream.com.
params
Subject
Reddit Search Results ({{steps.config.search_keyword}}): {{steps.consolidate_results.summary}}
string ·params.subject
Text
{{steps.format_email.plainText}}
string ·params.text
Optional
code
async params => {
1
2
3
4
5
6
7
8
9
10
11
12
}
13
const options = {
  subject: params.subject,
  text: params.text,
}
if (params.html) {
  options.html = params.html
}
if (params.include_collaborators) {
  options.include_collaborators = params.include_collaborators
}
$send.email(options)
steps.
end
End execution at this step. Later steps in the workflow will not run.
params
Reason
{{steps.config.search_keyword}}: {{steps.consolidate_results.summary}}
string ·params.reason
code
async params => {
1
2
}
3
$end(params.reason)