Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jessedearing
Created June 26, 2018 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessedearing/a75c24f808fc00ce51003595fb5eb220 to your computer and use it in GitHub Desktop.
Save jessedearing/a75c24f808fc00ce51003595fb5eb220 to your computer and use it in GitHub Desktop.
const express = require('express')
const Webtask = require('webtask-tools')
const bodyParser = require('body-parser')
const MessagingResponse = require("twilio").twiml.MessagingResponse
const request = require('request-promise-native')
var app = express();
app.use(bodyParser.json());
app.post('/sms/reply', (req, res) => {
(async () => {
const msg = new MessagingResponse()
var ssMsg = await getRandomSadServer()
msg.message(ssMsg.join("\n"))
// Twilio's webhook expects XML
res.writeHead(200, {"Content-Type": "text/xml"})
res.end(msg.toString())
})()
})
async function getRandomSadServer() {
var quote = []
try {
const content = await request.get('https://raw.githubusercontent.com/jessedearing/dotfiles/master/fortunes/sadserver_tweets')
const a = content.split("\n")
const randomStart = Math.floor(Math.random() * a.length)
var storeQuote = false
for (let i=randomStart; i<a.length; i++) {
// We need to find a `%` to anchor our starting point
if (!storeQuote) {
if (a[i] == "%") {
storeQuote = true
}
continue
}
// We've found the starting `%` already, this tells us we're at the end
// so we break
if (a[i] === '%') {
break
}
quote.push(a[i])
}
return quote
}
catch (error) {
console.log(error)
}
}
module.exports = Webtask.fromExpress(app);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment