Skip to content

Instantly share code, notes, and snippets.

@jthomas jthomas/app.js
Created Jun 11, 2015

Embed
What would you like to do?
Phonebot mock API responses server
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded())
var requests = []
var store_request = function (req) {
requests.push({
body: req.body,
headers: req.headers,
url: req.url,
method: req.method
})
}
app.post('/slack/:channel', function (req, res) {
store_request(req)
res.setHeader('Content-Type', 'application/json')
res.send(JSON.stringify({}))
})
app.get('/2010-04-01/Accounts/:account/IncomingPhoneNumbers.json', function (req, res) {
store_request(req)
res.setHeader('Content-Type', 'application/json')
var data = {
'incoming_phone_numbers': [{
'phone_number': '+440123456789'
}]
}
res.send(JSON.stringify(data))
})
app.post('/2010-04-01/Accounts/:account/Calls.json', function (req, res) {
store_request(req)
res.setHeader('Content-Type', 'application/json')
var data = {
'sid': 'testing_sid',
'status': 'queued',
'to': '1234567890'
}
res.send(JSON.stringify(data))
})
app.post('/2010-04-01/Accounts/:account/Calls/testing_sid.json', function (req, res) {
store_request(req)
res.setHeader('Content-Type', 'application/json')
var data = {
'sid': 'testing_sid',
'status': 'completed',
'to': '1234567890'
}
res.send(JSON.stringify(data))
})
app.get('/audio.wav', function (req, res) {
store_request(req)
res.sendfile('audio.wav')
})
app.post('/v1/recognize', function (req, res) {
store_request(req)
res.setHeader('Content-Type', 'application/json')
var data = {
'results': [{'alternatives': [{transcript: 'Hello World'}]}],
'result_index': 0
}
res.send(JSON.stringify(data))
})
app.get('/requests', function (req, res) {
res.setHeader('Content-Type', 'application/json')
res.send(JSON.stringify({requests: requests}))
})
app.get('/requests/clear', function (req, res) {
requests = []
res.sendStatus(200)
})
var server = app.listen(process.env.VCAP_APP_PORT || 3000, function () {
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
})
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.