Skip to content

Instantly share code, notes, and snippets.

Last active July 3, 2016 16:39
Show Gist options
  • Save bendyorke/f4e3ac327b852a52ccfd89868c608996 to your computer and use it in GitHub Desktop.
Save bendyorke/f4e3ac327b852a52ccfd89868c608996 to your computer and use it in GitHub Desktop.
CatFacts code for
wt cron schedule --name cat-facts \
--param to=<ENEMY_PHONE_NUMBER> \
--secret SID=<TWILIO_SID> \
"use latest";
const Twilio = require('twilio')
const E164_REGEXP = /^\+?[1-9]\d{1,14}$/
const FACTS = [
"The heaviest cat ever recorded was 46 lbs.",
"A tabby named \"Dusty\" gave birth to 420 documented kittens in her lifetime.",
"A female tabby named \"Ma\" lived for 34 years making her the oldest reliably documented housecat.",
"Cats can't taste sweets.",
"A cat has 32 muscles in each ear.",
"Neutering a cat extends it's life span by two or three years.",
"A cat's tongue consists of small \"hooks\" which come in handy when tearing up food.",
"Cats must have fat in their diet because they can't produce it on their own.",
"Cat's urine glows under a black light.",
"Cats have a third eyelid called a haw and you will probably only see it when kitty isn't feeling well.",
"Cats sleep 16 to 18 hours per day.",
"Cats are the only animal that walk on their claws not the pads of their feet.",
"Newborn kittens have closed ear canals that don't begin to open for nine days.",
"A kittens eyes are always blue at first.",
"A cat cannot see directly under its nose.",
"Most deaf cats do not meow.",
* Returns a random fact from the FACTS collection
* @return {string}
const randomFact = () => {
return FACTS[Math.floor(Math.random() * FACTS.length)]
* Sends a SMS using the twilio client. Should be called with
* env variables in scope.
* @param {object} params - params to send to Twilio. Requires a `to` field.
* @param {function} cb - callback function. Is passed (error, response)
* @void
const sms = function(params, cb) {
const { SID, TOKEN, NUMBER: from } = this.secrets
const body = randomFact()
Twilio(SID, TOKEN).sendMessage({ from, body, ...params }, (err, data) => {
if (err) cb(err)
cb(null, `Successfully sent CatFact to ${}`)
* Checks to see whether the number is a valid E.164 number
* @param {string} number
* @return {bool}
const validateNumber = number => {
return E164_REGEXP.test(number)
* Strips extra characters from a phone number, leaving only
* a leading + followed by numbers.
* @param {string} number
* @return {string}
const formatNumber = number => {
return (number || "")
.replace(/[^\d+]/g, '')
.replace(/(.)\++/g, '$1')
* Sends a random cat fact to a given user. Expects 3 secrets: SID, TOKEN, and NUMBER.
* Also requires a `to` parameter.
* Use responsibly.
module.exports = (ctx, cb) => {
const to = formatNumber(
if (!to) cb('A `to` parameter is required.')
if (!validateNumber(to)) cb('`' + to + '` is not a valid number (must be E.164 format)'), {to}, cb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment