Skip to content

Instantly share code, notes, and snippets.

@reichert621
Created June 5, 2020 02:41
Show Gist options
  • Save reichert621/2845ae10db777a9d45210bf1704bcbbf to your computer and use it in GitHub Desktop.
Save reichert621/2845ae10db777a9d45210bf1704bcbbf to your computer and use it in GitHub Desktop.
Taro TODOs

TODOs

Install Taro client

npm install taro-client

Create email templates

const Taro = require('taro-client')({
  key: API_KEY,
});

Taro.templates.create(
  'test',
  `
    <p>Hello __NAME__!</p>
    <p>Thanks for signing up. Your username is __USERNAME__</p>
    <p>Your free trial will expire in __DAYS_UNTIL_EXPIRATION__ days</p>
    <p>Best, <br />Taro</p>
  `
);

Send alerts through Taro

Taro.notify.email({
  subject: 'subject',
  message: 'text',
  html: 'html',
});
// TODO: integrate Twilio, Slack, etc.
Taro.notify.sms({message: 'message'});
Taro.notify.slack({message: 'message'});

// TODO: how should webhooks work?
Taro.notify.webhook('ais.success', {...data});
Taro.notify.webhook({
  event: 'ais.success',
  data: {...data},
});

// Send email with template (see above)
Taro.notify.email({
  subject: 'subject',
  template: 'test',
  params: {
    name: 'Alex',
    username: 'reichertjalex',
    daysUntilExpiration: 14, // or `days_until_expiration`
  },
});

Set up webhooks in Taro

Taro.webhooks.on('event', {
  // When 'event' is triggered, will send POST request with event body
  urls: ['gettaro.com/api/callback', 'me.com/api/notify'],
});

Save data to Taro

// TODO: set a simple storage system
Taro.storage.get(key);
Taro.storage.set(key, {...values});

Use cases

const Taro = require('taro-client')({
  key: API_KEY,
  namespace: 'my-first-function',
});

const scrape = () => {
  return request.get(url).then((res) => parse(res.body));
};

const main = async () => {
  Taro.log('Begin scraping AIS data');

  const results = await scrape();
  const ts = +new Date();
  const key = `AIS-DATA-${ts}`;
  // Save data in NoSQL DB in case we want to retrieve it later
  const {ok, url, error} = await Taro.set(key, {data: results});

  if (ok) {
    const msg = `Successfully scraped and saved data! View at ${url}`;

    Taro.log(msg); // Log message for debugging
    await Taro.notify.slack({message: msg}); // Alert in slack
    await Taro.notify.webhook('ais.success', {key, data, timestamp: ts}); // Notify webhooks
  } else {
    Taro.error(error || 'Something went wrong!');
  }
};

module.exports = main;

Scripts:

npm install -g taro-cli

taro deploy my-first-function
taro run my-first-function
taro delete my-first-function
taro repl my-first-function # ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment