0
我正在製作Facebook chatbot並需要一些基本的用戶信息。從NodeJS獲取請求中獲取Facebook用戶信息
我正在寫我的機器人在NodeJS,現在我有我的機器人作爲回聲機器人運行。它所做的只是接收用戶消息並用相同的文本回復給他們。
我想從Facebook獲取用戶姓氏和姓氏以便在郵件中使用。我正在使用Facebook Graph API,並在Facebook文檔中發現了這個問題:
curl -X GET "https://graph.facebook.com/v2.6/<USER_ID>?fields=first_name,last_name,profile_pic&access_token=<PAGE_ACCESS_TOKEN>"
所以我在終端上運行了這個程序並返回了正確的信息。
我只是想在NodeJS的Chatbot代碼中使用該CURL函數。我查看了這個文檔:https://nodejs.org/docs/v0.5.2/api/http.html#http.request,但無法弄清楚。
那麼如何在NodeJs中運行相同的CURL請求以獲取用戶名?
這裏是我的代碼:
var express = require('express');
var bodyParser = require('body-parser');
var request = require("request")
var app = express();
var port = process.env.PORT || 3000;
var mongoose = require('mongoose');
mongoose.connect('mongodb://#@ds013911.mlab.com:13911/wyrdbot');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("We're connceted")
});
// body parser middleware
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }));
app.listen(port, function() {
console.log('Listening on port ' + port);
});
app.get('/', function (req, res) {
if (req.query['hub.verify_token'] === '<myToken>') {
res.send(req.query['hub.challenge']);
console.log("app.get ran")
res.sendStatus(200)
}
res.send(req.query['hub.challenge']);
})
app.post('/', function (req, res) {
console.log("app.post ran")
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.message && event.message.text) {
text = event.message.text;
if (text === 'Generic') {
sendGenericMessage(sender);
continue;
}
sendTextMessage(sender, "Text received, echo: "+ text.substring(0, 200));
}
}
res.sendStatus(200);
});
var token = "<myToken>";
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('Error sending message: ', error);
} else if (response.body.error) {
console.log('Error: ', response.body.error);
}
});
}
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);
}
});
}
任何幫助表示讚賞。謝謝!
酷,謝謝! –