FB Messenger bot中的入门按钮

时间:2017-05-17 22:51:06

标签: node.js facebook facebook-graph-api facebook-javascript-sdk bots

我已经使用此代码将启动按钮添加到我的机器人,因为我知道我正在使用测试人员帐户和管理员帐户测试机器人,并且#34;开始使用#34;按钮永远不会出现。

use strict';
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const path = require('path');


var messengerButton = "<html><head><title>Facebook Messenger Bot</title></head><body><h1>Facebook Messenger Bot</h1>This is a bot based on Messenger Platform QuickStart. For more details, see their <a href=\"https://developers.facebook.com/docs/messenger-platform/guides/quick-start\">docs</a>.<script src=\"https://button.glitch.me/button.js\" data-style=\"glitch\"></script><div class=\"glitchButton\" style=\"position:fixed;top:20px;right:20px;\"></div></body></html>";

// The rest of the code implements the routes for our Express server.
let app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));

// Webhook validation
app.get('/webhook', function(req, res) {
  if (req.query['hub.mode'] === 'subscribe' &&
      req.query['hub.verify_token'] === process.env.VERIFY_TOKEN) {
    console.log("Validating webhook");
    res.status(200).send(req.query['hub.challenge']);
    setupGetStartedButton(res);

  } else {
    console.error("Failed validation. Make sure the validation tokens match.");
    res.sendStatus(403);          
  }
});





// Display the web page
app.get('/', function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write(messengerButton);
  res.end();
  setupGetStartedButton(res);
});



   function setupGetStartedButton(res){
         console.log("i am hereeee");

        var messageData = {
                "get_started":[
                {
                    "payload":"HEllo for the first time"
                    }
                ]
        };

        // Start the request
        request({
            url: 'https://graph.facebook.com/v2.6/me/messenger_profile?access_token=your_access_token',
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            form: messageData
        },
        function (error, response, body) {
            if (!error && response.statusCode == 200) {
                // Print out the response body
                res.send(body);

            } else { 
                // TODO: Handle errors
                res.send(body);
            }
        });
    }     
尝试从管理员或测试人员帐户打开机器人后,按钮永远不会出现,甚至消息(我是heeeee)也从未出现在控制台上......这意味着代码甚至不会调用该功能。

注意:我正在使用故障。

1 个答案:

答案 0 :(得分:0)

您需要一个POST处理程序。消息通过POST发送到机器人。

app.post('/webhook', function (req, res) {

  var data = req.body;
  ... 

有一个nodejs工作示例here。你可以看到它here