Created
June 26, 2018 16:53
-
-
Save jessedearing/a75c24f808fc00ce51003595fb5eb220 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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