diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..77c7a90 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +variables.env \ No newline at end of file diff --git a/README.md b/README.md index e82b086..06c36fc 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,16 @@ ## Due: 17th June,2018 or Earlier #### This code challenge is due on the 17th of June,2018 or earlier. + +## How to test the application +1. Open the [africastalking sanbox simulator](https://simulator.africastalking.com:1517) in your browser. +2. Select your country and choose a phone number on the simulator page to test application with. +3. click on Launch +4. Select the SMS option and type in a message in the provided field. +5. Send the message to 41919. +6. Voila!!! You would receive a response almost instantly. + + ## Simple Unchanging Rules The code challenge is and will always be judged using the following criteria - A Correct fork, branch and pull request @@ -54,7 +64,3 @@ Please read the overview for all code challenges [here.](http://atdevoutreach.vi ## Get Support on the Africa's Talking Slack In case you have any questions, join our Slack [here](https://slackin-africastalking.now.sh/) - - - - diff --git a/app.js b/app.js new file mode 100644 index 0000000..4afe45a --- /dev/null +++ b/app.js @@ -0,0 +1,46 @@ +var createError = require('http-errors'); +var express = require('express'); +var path = require('path'); +var cookieParser = require('cookie-parser'); +var logger = require('morgan'); +const bodyParser = require('body-parser'); + +var indexRouter = require('./routes/index'); +var smsRouter = require('./routes/sms'); + +var app = express(); + +// view engine setup +app.set('views', path.join(__dirname, 'views')); +app.set('view engine', 'jade'); + +app.use(logger('dev')); +app.use(express.json()); +app.use(express.urlencoded({ extended: false })); +app.use(cookieParser()); +app.use(express.static(path.join(__dirname, 'public'))); + +app.use('/', indexRouter); +app.use('/sms', smsRouter); + +//Handle get to /sms +app.use(function(req, res, next) { + res.end('This is the sms endpoint') +}); +// catch 404 and forward to error handler +app.use(function(req, res, next) { + next(createError(404)); +}); + +// error handler +app.use(function(err, req, res, next) { + // set locals, only providing error in development + res.locals.message = err.message; + res.locals.error = req.app.get('env') === 'development' ? err : {}; + + // render the error page + res.status(err.status || 500); + res.render('error'); +}); + +module.exports = app; diff --git a/config/start.js b/config/start.js new file mode 100644 index 0000000..2835962 --- /dev/null +++ b/config/start.js @@ -0,0 +1,27 @@ +const mongoose = require('mongoose'); + +// // import environmental variables from our variables.env file +// require('dotenv').config({ path: 'variables.env' }); + +// // Connect to our Database and handle any bad connections +// mongoose.connect(process.env.DATABASE).then(()=>{ +// console.log('connection to DB successful') +// }).catch(err=>{ +// console.error(`Error connecting to DB→ ${err.message}`) +// }); +// mongoose.Promise = global.Promise; + +// mongoose.connection.on('error', (err) => { +// console.error(`Error connecting to DB→ ${err.message}`); +// }); + + +// Start our app! +const app = require('../app'); +app.set('port', process.env.PORT || 7777); +const server = app.listen(app.get('port'), () => { + console.log(`Express running → PORT ${server.address().port}`); +}); + +// process.on('SIGINT', () => { console.log("Bye bye!"); process.exit(); }); + diff --git a/controllers/smsController.js b/controllers/smsController.js new file mode 100644 index 0000000..dd28ed0 --- /dev/null +++ b/controllers/smsController.js @@ -0,0 +1,42 @@ +var _ = require('lodash') + +// import environmental variables from our variables.env file +require('dotenv').config({ path: 'variables.env' }); + +//= ==================== APP CONSTANTS +const message = 'I am a fisherman. I sleep all day and work all night!' +// Your login credentials +const shortCode = '41919' +const username = 'sandbox' +const apikey = process.env.KEY; +const options = { + apiKey: apikey, + username: username +} +const AfricasTalking = require('africastalking')(options) +const sms = AfricasTalking.SMS + +exports.received = (req, res) => { + //select needed properties from post object + var body = _.pick(req.body, ['from', 'to']) + + //Respond to message if from appropriate shortcode + if (body.to == '41919') { + sendResponse(body.from, message) + } else { + console.log('No be the correct guy you send give') + } +} + +function sendResponse (recipient, message) { + var opts = { + from: shortCode, + to: recipient, + message: message + } + sms.send(opts).then( + console.log('Message sent successfully') + ).catch( + console.log('Something went wrong with message sending') + ); +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..0a3e376 --- /dev/null +++ b/package.json @@ -0,0 +1,27 @@ +{ + "name": "express-template", + "version": "0.0.0", + "private": true, + "scripts": { + "start": "node ./config/start.js", + "prod": "node ./config/start.js", + "watch": "nodemon ./config/start.js" + }, + "dependencies": { + "africastalking": "^0.3.2", + "authy": "^1.3.0", + "bcrypt": "^2.0.1", + "cookie-parser": "~1.4.3", + "debug": "~2.6.9", + "dotenv": "^5.0.1", + "express": "~4.16.0", + "http-errors": "~1.6.2", + "jade": "~1.11.0", + "jsonwebtoken": "^8.2.1", + "lodash": "^4.17.10", + "mongoose": "^5.1.1", + "morgan": "~1.9.0", + "nodemon": "^1.17.4", + "validator": "^10.2.0" + } +} diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css new file mode 100644 index 0000000..9453385 --- /dev/null +++ b/public/stylesheets/style.css @@ -0,0 +1,8 @@ +body { + padding: 50px; + font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; +} + +a { + color: #00B7FF; +} diff --git a/routes/index.js b/routes/index.js new file mode 100644 index 0000000..8e6f38c --- /dev/null +++ b/routes/index.js @@ -0,0 +1,9 @@ +var express = require('express'); +var router = express.Router(); + +/* GET home page. */ +router.get('/', function(req, res, next) { + res.render('index', { title: 'SMS API APP' }); +}); + +module.exports = router; diff --git a/routes/sms.js b/routes/sms.js new file mode 100644 index 0000000..f05e2cd --- /dev/null +++ b/routes/sms.js @@ -0,0 +1,10 @@ +var express = require('express'); + +var router = express.Router(); +var sms = require('../controllers/smsController') + + +/* POST users listing. */ +router.post('/', sms.received); + +module.exports = router; diff --git a/variables.env b/variables.env new file mode 100644 index 0000000..0f95854 --- /dev/null +++ b/variables.env @@ -0,0 +1 @@ +KEY= a5b2374285f235ef8b8822dd3c20ad981610dbfd2a127143a13d8832304b253c \ No newline at end of file diff --git a/views/error.jade b/views/error.jade new file mode 100644 index 0000000..51ec12c --- /dev/null +++ b/views/error.jade @@ -0,0 +1,6 @@ +extends layout + +block content + h1= message + h2= error.status + pre #{error.stack} diff --git a/views/index.jade b/views/index.jade new file mode 100644 index 0000000..3d63b9a --- /dev/null +++ b/views/index.jade @@ -0,0 +1,5 @@ +extends layout + +block content + h1= title + p Welcome to #{title} diff --git a/views/layout.jade b/views/layout.jade new file mode 100644 index 0000000..15af079 --- /dev/null +++ b/views/layout.jade @@ -0,0 +1,7 @@ +doctype html +html + head + title= title + link(rel='stylesheet', href='/stylesheets/style.css') + body + block content