-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 41a8733
Showing
5 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
config/settings.local.json |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict'; | ||
|
||
const KoaRoute = require('koa-route'); | ||
const Bluebird = require('bluebird'); | ||
const Config = require('config.json')('./config/settings.json', process.env.NODE_ENV); | ||
let app = require('../server'); | ||
|
||
//require the Google APIs module and promisify it | ||
const Google = require('googleapis'); | ||
const Customsearch = Google.customsearch('v1'); | ||
let googleKey = Config.google.key; | ||
Bluebird.promisifyAll(Customsearch.cse); | ||
|
||
//require the Twilio module and promisify it | ||
var accountSid = Config.twilio.sid; | ||
var authToken = Config.twilio.token; | ||
var Twilio = require('twilio')(accountSid, authToken); | ||
Bluebird.promisifyAll(Twilio.messages); | ||
|
||
app.use(KoaRoute.get('/coolPics', function *() { | ||
// Either use a passed "topic" or just send them cute dogs if they don't want to decide | ||
let topic = (this.request.query.topic) ? this.request.query.topic : 'cute dogs'; | ||
let phone_number = this.request.query.phone_number; | ||
|
||
// Validate the query - we at least need a phone number | ||
this.checkQuery('phone_number').notEmpty().len(12, 12, "must be in +X XXX XXX XXXX format").match(/^\+1[0-9]{10}/); | ||
if (this.errors) { | ||
this.body = this.errors[0]; | ||
return; | ||
} | ||
|
||
// Google search params | ||
let searchParams = { | ||
fileType: 'jpg', | ||
imgSize: 'medium', | ||
safe: 'high', | ||
q: topic, | ||
searchType: 'image', | ||
cx: '013033848846758345769:tenrfa2t3dw', | ||
auth: googleKey | ||
}; | ||
|
||
// Search for images of the topic, pic a random one on the firs page, and pass that to twilio | ||
yield Customsearch.cse.listAsync(searchParams) | ||
.bind(this) | ||
.then(function(res) { | ||
this.log.info('Search Result', res); | ||
let selector = Math.round(Math.random() * res.items.length); | ||
let topicalImage = res.items[selector].link; | ||
let twilioParams = { | ||
to: phone_number, | ||
from: '+18043810322', | ||
body: `It is your lucky day! Someone sent you a message with the ${topic} API!`, | ||
mediaUrl: topicalImage | ||
}; | ||
return Twilio.messages.createAsync(twilioParams); | ||
}) | ||
.then(function(message) { | ||
// Send back the Twilio message deets | ||
this.log.info('Twilio Message', message); | ||
this.body = message; | ||
}) | ||
.catch(function(error) { | ||
// Catch any errors that may have occurred in the promise chain | ||
this.log.error(error); | ||
this.body = error; | ||
}); | ||
})); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
'use strict'; | ||
require('fs').readdirSync(__dirname).forEach(function(file) { | ||
if (file.indexOf('.js') > -1) require('./' + file); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
"name": "coapi", | ||
"version": "0.0.1", | ||
"description": "Company API server", | ||
"main": "server.js", | ||
"scripts": { | ||
"cloc": "cloc --exclude-dir=node_modules .", | ||
"local": "NODE_ENV=local LOG=trace BLUEBIRD_DEBUG=1 BLUEBIRD_W_FORGOTTEN_RETURN=0 nodemon --harmony-destructuring server.js | bunyan", | ||
"development": "NODE_ENV=development LOG=debug BLUEBIRD_DEBUG=1 BLUEBIRD_W_FORGOTTEN_RETURN=0 node --harmony-destructuring index.js", | ||
"production": "NODE_ENV=production LOG=info BLUEBIRD_DEBUG=0 BLUEBIRD_W_FORGOTTEN_RETURN=0 node --harmony-destructuring index.js", | ||
"test": "lab -e local" | ||
}, | ||
"engines": { | ||
"npm": ">=3.7.1", | ||
"node": ">=5.6.0" | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Charles Hearn", | ||
"email": "[email protected]", | ||
"url": "https://github.com/charleshearn" | ||
} | ||
], | ||
"license": "ISC", | ||
"homepage": "https://alloy.co", | ||
"dependencies": { | ||
"bluebird": "^3.3.1", | ||
"bunyan": "^1.6.0", | ||
"config.json": "0.0.4", | ||
"googleapis": "^2.1.7", | ||
"joi": "^7.3.0", | ||
"koa": "^1.1.2", | ||
"koa-body": "^1.4.0", | ||
"koa-bunyan-logger": "^1.3.0", | ||
"koa-route": "^2.4.2", | ||
"koa-validate": "^0.2.11", | ||
"lodash": "^4.4.0", | ||
"twilio": "^2.9.0" | ||
}, | ||
"devDependencies": { | ||
"code": "^2.1.0", | ||
"lab": "^8.2.0" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* This is a project containing a bunch of cool API endpoints...hopefully! | ||
* Contribute whatever you want. Don't mess with other people's endpoints though. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const Koa = require('koa'); | ||
const KoaBunyanLogger = require('koa-bunyan-logger'); | ||
const KoaRoute = require('koa-route'); | ||
const Bluebird = require('bluebird'); | ||
const app = Koa(); | ||
const Config = require('config.json')('./config/settings.json', process.env.NODE_ENV); | ||
|
||
app.use(require('koa-body')()); | ||
app.use(require('koa-validate')()); | ||
app.use(KoaBunyanLogger()); | ||
app.use(KoaBunyanLogger.requestIdContext()); | ||
app.use(KoaBunyanLogger.requestLogger()); | ||
|
||
// Trust headers from reverse proxy | ||
app.proxy = true; | ||
|
||
// Disable default error handling in favor of Bunyan plugin | ||
app.on('error', function() {}); | ||
|
||
// Heathcheck returns memory stats | ||
app.use(KoaRoute.get('/healthcheck', function *() { this.body = process.memoryUsage(); })); | ||
|
||
// Allow config host to override auto interface detection | ||
const host = Config.app.host || require('lodash').filter(require('os').networkInterfaces().eth0, (nic) => (nic.family === 'IPv4' && nic.internal === false))[0].address; | ||
app.listen(Config.app.port, host); | ||
console.log({ host, port: Config.app.port }, 'Server ready - let\'s get this party started!'); | ||
|
||
module.exports = app; | ||
|
||
require('./contributors'); |