Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

challenge completed #5

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
variables.env
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/)




46 changes: 46 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -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;
27 changes: 27 additions & 0 deletions config/start.js
Original file line number Diff line number Diff line change
@@ -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(); });

42 changes: 42 additions & 0 deletions controllers/smsController.js
Original file line number Diff line number Diff line change
@@ -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')
);
}
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
8 changes: 8 additions & 0 deletions public/stylesheets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}

a {
color: #00B7FF;
}
9 changes: 9 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 10 additions & 0 deletions routes/sms.js
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions variables.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KEY= a5b2374285f235ef8b8822dd3c20ad981610dbfd2a127143a13d8832304b253c
6 changes: 6 additions & 0 deletions views/error.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extends layout

block content
h1= message
h2= error.status
pre #{error.stack}
5 changes: 5 additions & 0 deletions views/index.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extends layout

block content
h1= title
p Welcome to #{title}
7 changes: 7 additions & 0 deletions views/layout.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body
block content