Skip to content

Instantly share code, notes, and snippets.

@reichert621
Created June 9, 2020 14:38
Show Gist options
  • Save reichert621/273653aa3cd596b04c491f4bc1a3bdfb to your computer and use it in GitHub Desktop.
Save reichert621/273653aa3cd596b04c491f4bc1a3bdfb to your computer and use it in GitHub Desktop.
Taro Example: Send top subreddit post to Slack
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