微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我的geting_started实现有什么问题?

如何解决我的geting_started实现有什么问题?

当初次用户单击“入门”按钮时,我希望其有效负载值为“开始”,而不是认的“ ”。但这不是正在发生的事情。我对此实在很tom脚。我首先要做的是将请求函数放置在callSendAPI()函数内,该函数将设置get_started的有效负载值。但是我意识到,按下“入门”按钮后,它将被调用

这是我的网钩:

'use strict';

// Imports dependencies and set up http server
const
  express = require('express'),bodyParser = require('body-parser'),request = require('request'),app = express().use(bodyParser.json()); // creates express http server

// Sets server port and logs message on success
app.listen(process.env.PORT || 1337,() => console.log('webhook is listening'));

app.post('/webhook',(req,res) => {  
  // Parse the request body from the POST
  let body = req.body;

  // Check the webhook event is from a Page subscription
  if (body.object === 'page') {
    body.entry.forEach(function(entry) {
      // Gets the body of the webhook event
      let webhook_event = entry.messaging[0];
      console.log(webhook_event);
    
      // Get the sender PSID
      let sender_psid = webhook_event.sender.id;
      console.log('Sender PSID: ' + sender_psid);

      // Check if the event is a message or postback and
      // pass the event to the appropriate handler function
      if (webhook_event.message) {
        handleMessage(sender_psid,webhook_event.message); 
      } else if (webhook_event.postback) {
        handlePostback(sender_psid,webhook_event.postback);
      }
    });

    // Return a '200 OK' response to all events
    res.status(200).send('EVENT_RECEIVED');
  } else {
    // Return a '404 Not Found' if event is not from a page subscription
    res.sendStatus(404);
  }
});

// Adds support for GET requests to our webhook
app.get('/webhook',res) => {

  // Your verify token. Should be a random string.
  let VERIFY_TOKEN = "WHALE"
    
  // Parse the query params
  let mode = req.query['hub.mode'];
  let token = req.query['hub.verify_token'];
  let challenge = req.query['hub.challenge'];
    
  // Checks if a token and mode is in the query string of the request
  if (mode && token) {
  
    // Checks the mode and token sent is correct
    if (mode === 'subscribe' && token === VERIFY_TOKEN) {
      
      // Responds with the challenge token from the request
      console.log('WEBHOOK_VERIFIED');
      res.status(200).send(challenge);
    
    } else {
      // Responds with '403 Forbidden' if verify tokens do not match
      res.sendStatus(403);      
    }
  }
});

getStarted();

function getStarted() {
  // set the payload for get_started
  request(
    {
      "uri": "https://graph.facebook.com/v2.6/me/messages","qs": { "access_token": process.env.PAGE_ACCESS_TOKEN },"method": "POST","json": {
        "get_started": {"payload": "start"}
      }
    },(err) => {
      if (!err) {
        console.log('get_started set!')
      } else {
        console.error("Unable to send message:" + err);
      }
    }
  );
}  

// Handles messages events
function handleMessage(sender_psid,received_message) {
  let response;

  // Check if the message contains text
  if (received_message.text) {    
    
      // Create the payload for a basic text message
      response = {
        "text": `You sent the message: "${received_message.text}". Now send me an image!`
      }
    
  } else if (received_message.attachments) {
    // Get the URL of the message attachment
    let attachment_url = received_message.attachments[0].payload.url;
    response = {
      "attachment": {
        "type": "template","payload": {
          "template_type": "generic","elements": [{
            "title": "Is this the right picture?","subtitle": "Tap a button to answer.","image_url": attachment_url,"buttons": [
              {
                "type": "postback","title": "Yes!","payload": "yes",},{
                "type": "postback","title": "No!","payload": "no",}
            ],// buttons
          }] // elements
        } // payload
      } // attachment
    } // response
  } // else if

  // Sends the response message
  callSendAPI(sender_psid,response);   
}

// Handles messaging_postbacks events
function handlePostback(sender_psid,received_postback) {
  let response;
  
  // Get the payload for the postback
  let payload = received_postback.payload;

  // Set the response based on the postback payload
  if (payload === 'yes') {
    response = { "text": "Thanks!" }
  } else if (payload === 'no') {
    response = { "text": "Oops,try sending another image." }
  } 
  // when the user presses the get started button,reply with its payload
  else {
    response = {"text": payload}
  }
  // Send the message to ackNowledge the postback
  callSendAPI(sender_psid,response);
}

// Sends response messages via the Send API
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","json": request_body
    },(err,res,body) => {
      if (!err) {
        console.log('message sent!')
      } else {
        console.error("Unable to send message:" + err);
      }
    }
  ); 
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。