Skip to content

Instantly share code, notes, and snippets.

@jthomas
Created June 11, 2015 10:49
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 jthomas/f573cb94de20b0e95940 to your computer and use it in GitHub Desktop.
Save jthomas/f573cb94de20b0e95940 to your computer and use it in GitHub Desktop.
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