Created
June 9, 2020 14:38
-
-
Save reichert621/273653aa3cd596b04c491f4bc1a3bdfb to your computer and use it in GitHub Desktop.
Taro Example: Send top subreddit post to Slack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require('superagent'); | |
const Taro = require('taro-client')( | |
// Replace this with your Taro API key | |
process.env.TARO_API_KEY | |
); | |
const getTopPost = async (subreddit) => { | |
const sub = `https://www.reddit.com/r/${subreddit}/top.json`; | |
const res = await request.get(sub).query({sort: 'top', t: 'day'}); | |
const {children: posts} = res.body.data; | |
const top = posts[0].data; | |
return { | |
title: top.title, | |
score: top.score, | |
url: top.url, | |
}; | |
}; | |
const main = async () => { | |
const {title, score, url} = await getTopPost('programming'); | |
// This will send to Slack as a formatted link! | |
const message = `<${url}|[${score} upvotes] ${title}>`; | |
const result = await Taro.notify.slack({message}); | |
return result; | |
}; | |
// Run function and verify output | |
main().then(console.log).catch(console.log); | |
// You must have a default export of the function you want to run | |
// in order for it to be deployed and scheduled | |
module.exports = main; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment