Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.