【发布时间】:2019-02-12 23:34:59
【问题描述】:
我是 Javascript 新手,只是被使用 Node.js 的 Javascript 异步回调卡住了。
我首先设置 Facebook webhook 并发出 Webhook POST 请求
这是我的代码: routes.js
**To set up facebook webhook**
var facebook_handler = require('../controllers/botkit').handler
module.exports = function (app) {
// public pages=============================================
// root
app.get('/', function (req, res) {
res.render('home')
})
app.get('/webhook', function (req, res) {
// Check to see which webhook password (FACEBOOK_VERIFY_TOKEN) to check for, from incoming request.
if (process.env.PORT ||process.env.VCAP_APP_PORT ) {
FB_VERIFY_TOKEN = process.env.FACEBOOK_VERIFY_TOKEN
} else {
FB_VERIFY_TOKEN = process.env.FACEBOOK_VERIFY_TOKEN_DEV
}
// This enables subscription to the webhooks
if (req.query['hub.mode'] === 'subscribe' && req.query['hub.verify_token'] === FB_VERIFY_TOKEN) {
res.send(req.query['hub.challenge'])
}
else {
res.send('Incorrect verify token')
}
})
app.post('/webhook', function (req, res) {
console.log("\n CALL HANDLER FUNCTION ---- \n");
facebook_handler(req.body)
console.log("call handler done");
res.send('okay')
})
}
从上面的代码中,我向 Facebook webhook 发出 POST 请求并获取 FB 消息的详细信息,然后在另一个文件 BotKit.js 中处理 webhook POST 请求
Botkit.js
var request = require('request');
require('dotenv').load();
var handler = function (obj) {
console.log("Message received from FB \n");
if (obj.entry ) {
for (var e = 0; e < obj.entry.length; e++) {
for (var m = 0; m < obj.entry[e].messaging.length; m++) {
var facebook_message = obj.entry[e].messaging[m]
test_message = facebook_message.message.text;
translatorEnglish (test_message) // calling the watson translator api to get translation for the received facebook message.
}
}
}
以上代码处理webhook POST请求并调用Translator函数(翻译POST请求)
翻译功能
var translationusername = "1234"
var translationpassowrd = "1234"
var transURL = "https://gateway.watsonplatform.net/language-
translator/api/v2/translate";
translatorEnglish = function(test_message) {
console.log("this should be called when translator called:" +test_message);
var parameters = {
text: test_message,
model_id: 'es-en'
};
languageTranslator.translate(
parameters,
function(error, response, body) {
if (error)
console.log(error)
else
english_message = response.translations[0].translation
console.log("The response should be:" +english_message);
translate = false
//console.log(JSON.stringify(response, null, 2));
}
);
};
问题是直到调用处理程序(即 webhook POST 请求完成)才执行翻译 POST 请求。翻译 POST 请求总是在 Webhook POST 完成后执行。
有没有办法在 Webhook POST 请求完成之前在 Webhook POST 请求中执行 Translator POST 请求。
类似的东西 Webhook POST --> 执行 --> 翻译 POST 执行并完成 ---> Webhook POST 完成
【问题讨论】:
-
如果问题不清楚请告诉我
标签: javascript node.js async-await webhooks async.js