Wednesday 17 May 2017

Get started button in FB Messenger bot

i have used this code to add the get started button to my bot,knowing that i am testing the bot using a tester account and admin account and the "get started" button never showed up .

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=EAAaDuD7zTlwBAGMd13o2V4hjYPTZAgNWnjHY0HXl9weSEqsFfWcETl92kCZCZBIu0BdXSciTGmWVfdIcjQpb2L3NQG1P4tm6RdI5ZAHJoUmp7YnK63fNYg4jYGKxERJ264o61xpf1W9RZBiVzZCgtwseUdUStGCIm0jB9zj2yG7gZDZD',
            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);
            }
        });
    }     

after trying to open the bot from admin or tester account the button never appear and even the message (i am hereeee) never appear on the console too... which mean the code doesn't even call the function.

Note:i am using glitch .



via Osama Fawzi

No comments:

Post a Comment