Last active
January 26, 2017 12:56
-
-
Save mauriciord/cf386533efbf92010e176cb5cd098155 to your computer and use it in GitHub Desktop.
Facebook request
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
var express = require('express'); | |
var path = require('path'); | |
var favicon = require('serve-favicon'); | |
var logger = require('morgan'); | |
var cookieParser = require('cookie-parser'); | |
var bodyParser = require('body-parser'); | |
//requestify | |
var requestify = require('requestify'); | |
var routes = require('./routes/index'); | |
var users = require('./routes/users'); | |
var app = express(); | |
// view engine setup | |
app.set('views', path.join(__dirname, 'views')); | |
app.set('view engine', 'jade'); | |
// uncomment after placing your favicon in /public | |
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); | |
app.use(logger('dev')); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(cookieParser()); | |
app.use(express.static(path.join(__dirname, 'public'))); | |
app.use(function(req, res, next) { | |
res.header("Access-Control-Allow-Origin", "*"); | |
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); | |
next(); | |
}); | |
app.use('/', routes); | |
app.use('/users', users); | |
app.get('/tcfb/', function (req, res) { | |
if (req.query['hub.verify_token'] === 'minha_voz') { | |
res.send(req.query['hub.challenge']); | |
} | |
res.send('Error, token errado'); | |
}); | |
var token = "xxxxx"; | |
function sendTextMessage(sender, text) { | |
messageData = { | |
text:text | |
} | |
/* | |
request({ | |
url: 'https://graph.facebook.com/v2.6/me/messages', | |
qs: {access_token:token}, | |
method: 'POST', | |
json: { | |
recipient: {id:sender}, | |
message: messageData, | |
} | |
}, function(error, response, body) { | |
if (error) { | |
console.log('Erro ao enviar mensagem: ', error); | |
} else if (response.body.error) { | |
console.log('Erro: ', response.body.error); | |
} | |
});*/ | |
requestify.request('https://graph.facebook.com/v2.6/me/messages', { | |
method: 'POST', | |
qs: {access_token:token}, | |
json: { | |
recipient: {id:sender}, | |
message: messageData, | |
} | |
}).then(function(error, response, body) { | |
// get the response body | |
//response.getBody(); | |
if (error) { | |
console.log('Erro ao enviar mensagem: ', error); | |
} | |
// get the response headers | |
//response.getHeaders(); | |
// get specific response header | |
//response.getHeader('Accept'); | |
// get the code | |
//response.getCode(); | |
// Get the response raw body | |
//response.body; | |
}); | |
} | |
function sendGenericMessage(sender) { | |
messageData = { | |
"attachment": { | |
"type": "template", | |
"payload": { | |
"template_type": "generic", | |
"elements": [{ | |
"title": "First card", | |
"subtitle": "Element #1 of an hscroll", | |
"image_url": "http://messengerdemo.parseapp.com/img/rift.png", | |
"buttons": [{ | |
"type": "web_url", | |
"url": "https://www.messenger.com/", | |
"title": "Web url" | |
}, { | |
"type": "postback", | |
"title": "Postback", | |
"payload": "Payload for first element in a generic bubble", | |
}], | |
},{ | |
"title": "Second card", | |
"subtitle": "Element #2 of an hscroll", | |
"image_url": "http://messengerdemo.parseapp.com/img/gearvr.png", | |
"buttons": [{ | |
"type": "postback", | |
"title": "Postback", | |
"payload": "Payload for second element in a generic bubble", | |
}], | |
}] | |
} | |
} | |
}; | |
request({ | |
url: 'https://graph.facebook.com/v2.6/me/messages', | |
qs: {access_token:token}, | |
method: 'POST', | |
json: { | |
recipient: {id:sender}, | |
message: messageData, | |
} | |
}, function(error, response, body) { | |
if (error) { | |
console.log('Error sending message: ', error); | |
} else if (response.body.error) { | |
console.log('Error: ', response.body.error); | |
} | |
}); | |
} | |
app.post('/tcfb/', function (req, res) { | |
messaging_events = req.body.entry[0].messaging; | |
for (i = 0; i < messaging_events.length; i++) { | |
event = req.body.entry[0].messaging[i]; | |
sender = event.sender.id; | |
/*if (event.postback) { | |
text = JSON.stringify(event.postback); | |
sendTextMessage(sender, "Postback received: "+text.substring(0, 200), token); | |
continue; | |
}*/ | |
if (event.message && event.message.text) { | |
text = event.message.text; | |
/*if (text === 'Generic') { | |
sendGenericMessage(sender); | |
continue; | |
}*/ | |
sendTextMessage(sender, "Text received, echo: "+ text.substring(0, 200)); | |
console.log("Postou mensagem"); | |
console.log(text); | |
} | |
} | |
res.sendStatus(200); | |
}); | |
// catch 404 and forward to error handler | |
app.use(function(req, res, next) { | |
var err = new Error('Not Found'); | |
err.status = 404; | |
next(err); | |
}); | |
// error handlers | |
// development error handler | |
// will print stacktrace | |
if (app.get('env') === 'development') { | |
app.use(function(err, req, res, next) { | |
res.status(err.status || 500); | |
res.render('error', { | |
message: err.message, | |
error: err | |
}); | |
}); | |
} | |
// production error handler | |
// no stacktraces leaked to user | |
app.use(function(err, req, res, next) { | |
res.status(err.status || 500); | |
res.render('error', { | |
message: err.message, | |
error: {} | |
}); | |
}); | |
module.exports = app; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this :)
Best to remove your 'token' though dude!