显示json网址给Messenger机器人

时间:2018-07-24 06:56:50

标签: node.js bots facebook-messenger facebook-messenger-bot

我想在我的facebook Messenger机器人上显示来自JSON URL的数据。我尝试了很多代码方法。但是,我仍然无法理解。我只能用console.log在终端上显示数据。

这是我用于从JSON URL获取数据的代码:

const ambilDataProfil = (sender_psid) => {
//sorry this url below is not valid I write intentionally for my privacy data.
    request({
        url: 'https://sorry-url-privately.firebaseio.com/server/saving-data/users.json',
        method: "GET",
    }, function(error, response, body){
        let jsonBody = JSON.parse(response.body);
        let resp = jsonBody.Ndus;
        console.log(resp);
        let respon = {"text": "resp"};
        callSendAPI(sender_psid, respon);
    }
        // console.log(body.Ndus.goldar);
    );
};


function callSendAPI(sender_psid, response) {
    // Construct the message body
    let request_body = {
        "recipient": {
            "id": sender_psid
        },
        "message": response
    }

    // Send the HTTP request to the Messenger Platform
    request({
        "uri": "https://graph.facebook.com/v2.6/me/messages",
        "qs": { "access_token": PAGE_ACCESS_TOKEN },
        "method": "POST",
        "json": request_body
    }, (err, res, body) => {
        if (!err) {
            console.log('message sent!')
        } else {
            console.error("Unable to send message:" + err);
        }
    });
}

0 个答案:

没有答案