Created
April 13, 2016 16:04
-
-
Save cagataygurturk/c844be12b032928048a4ab90db7e252c to your computer and use it in GitHub Desktop.
Messenger platform test
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
/** | |
* Facebook Chat SDK that tells you the hour | |
*/ | |
var request = require("request"); | |
var page_token = "page_token"; | |
module.exports.respond = function (event, cb) { | |
console.log(event); | |
if (typeof event.challenge !== 'undefined' && | |
typeof event.verify_token !== 'undefined' && | |
event.challenge != '' && event.verify_token != '') { | |
return cb(null, event.challenge); | |
} | |
var incomingMessage = JSON.parse(event.body); | |
var senderId = incomingMessage.entry[0].messaging[0].sender.id; | |
if (typeof incomingMessage.entry[0].messaging[0].message === 'undefined') { | |
return cb(null, {"status": "ack"}); | |
} | |
getName(senderId, function (error, name) { | |
var options = { | |
uri: 'https://graph.facebook.com/v2.6/me/messages?access_token=' + page_token, | |
method: 'POST', | |
json: { | |
"recipient": { | |
"id": senderId | |
}, | |
"message": { | |
"text": "selam " + name + " şu an saat " + getTime() + "!" | |
} | |
} | |
}; | |
request(options, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
console.log(body.id); // Print the shortened url. | |
} | |
return cb(null, incomingMessage); | |
}); | |
}) | |
}; | |
var getTime = function () { | |
var time = new Date(); | |
var hour = time.getHours(); | |
var minutes = time.getMinutes(); | |
var seconds = time.getSeconds(); | |
return hour + ":" + minutes + ":" + seconds; | |
}; | |
var getName = function (id, callback) { | |
var options = { | |
uri: 'https://graph.facebook.com/v2.6/' + id + '?access_token=' + page_token, | |
method: 'GET' | |
}; | |
request(options, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
var body = JSON.parse(body); | |
return callback(null, body.first_name + " " + body.last_name); | |
} | |
return callback(error); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment