फेसबुक मैसेंजर बॉट में एकल पोस्टबैक पर एक से अधिक जबाब संदेश भेज रहा है

वोट
1

मैं मैसेंजर पर एक भी उपयोगकर्ता द्वारा ट्रिगर पोस्टबैक के लिए कई उत्तर भेजना चाहते हैं। मैं मैसेंजर के निम्नलिखित किया गया है डेवलपर दस्तावेज़ और वास्तव में ऐसा करने के तरीके नहीं पा सके।

मेरे कोड संरचना बहुत ट्यूटोरियल वे साइट पर दिया है के समान है, मैं एक 'है handlePostback ' समारोह जो प्राप्त पोस्टबैक को दिखाता है और 'को खोजने के लिए पूर्वनिर्धारित पेलोड का एक सेट करने के लिए यह तुलना प्रतिक्रिया ' JSON ऑब्जेक्ट। यह प्रतिक्रिया 'को दिया जाता है callSendAPI ' जो संदेश वापस मैसेंजर एपीआई के लिए भेजने के बुनियादी स्वरूप में यह JSON ऑब्जेक्ट डालता है।

function handlePostback(sender_psid,receivedPostback)
{ if(payload== 'defined_payload') {
  response = {
  text: 'Some text'
  };
callSendAPI(sender_psid,response);
}

function callSendAPI(sender_psid,response) {
let body = {
recipient: {
id= sender_psid
},
message: response
};
// Followed by code for POST request to the webhook
}

यह बुनियादी संरचना की जा रही है, अब मैं के लिए एक जवाब के रूप में कई संदेश भेजने के लिए चाहते हैं एक पोस्टबैक । मैं कुछ खुदाई किया था, और मैंने पाया कि समाधान संदेश [] सरणी बनाने के लिए हो सकता है। लेकिन मैं यह कैसे करते हो? क्योंकि मेरे 'प्रतिक्रिया' है कि समारोह के माध्यम से उत्पन्न किया जा रहा है, और संदेशों संरचना (मुझे लगता है कि) इस तरह दिखना चाहिए:

let body = {
 recipient: {
 id=sender_psid
 },
 messages: [ {
  response1
  },
  {
  response2
  }
 ]
};

मुझे आशा है कि मैं समझा सकता है मेरे सवाल का, मुझे पता है कि अगर मैं और अधिक विवरण प्रदान कर सकते हैं तो कृपया!

25/11/2017 को 06:17
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


4 जवाब

वोट
-1

संशोधित न करें callSendAPIकार्य करते हैं। अपने में handlePostbackफ़ंक्शन को कॉल callSendAPIकई बार।

callsendAPI(sender_psid,response1);
callsendAPI(sender_psid,response2);
26/11/2017 को 13:01
का स्रोत उपयोगकर्ता

वोट
1

अच्छा प्रश्न। आप जिस तरह से यह करने के लिए Node.js से परिचित नहीं हैं, तो भी स्पष्ट नहीं है और इस पर नहीं फेसबुक के संदेश API दस्तावेज़ अच्छी तरह से प्रलेखित है।

सबसे पहले, एक से अधिक संदेश भेजने, एक सरणी का उपयोग कर के रूप में आप देखा होगा काम नहीं करेगा के अपने दृष्टिकोण। फेसबुक एक अनुरोध के साथ 100 एपीआई कॉल करने के लिए भेज रहा है, लेकिन मेरी राय में यह अपनी स्थिति में जरूरत नहीं है के लिए एक समाधान है। आप और अधिक के बारे में यह बाहर की जाँच पता लगाने के लिए चाहते हैं Batched अनुरोध प्रलेखन , आप पता लगा लेंगे कार्यान्वयन तुम्हारा की तुलना में अलग है।

एक समाधान है कि काम करेंगे कॉल करने के लिए है callSendAPIसमारोह कई बार। लेकिन इस समाधान एक बड़ी खामी है : आप भेजे गए संदेशों की वास्तविक अनुक्रम को नियंत्रित करने में सक्षम नहीं होगा। उदाहरण के लिए यदि आप दो अलग-अलग संदेश भेजने के लिए चाहते हैं, तो आप गारंटी नहीं दे सकते, जो उपयोगकर्ताओं पर पहले भेजा जाएगा

इस मुद्दे को आप अपनी श्रृंखला की जरूरत है हल करने के लिए callSendAPIएक तरह से गारंटी देता है कि है कि अगले में कार्यों callSendAPIकॉल केवल क्या होगा के बाद पहला संदेश पहले से ही भेजा जाता है। आप द्वारा NodeJS में यह कर सकते हैं या तो कॉलबैक या वादों का उपयोग कर । आप उनमें से किसी के साथ परिचित नहीं हैं, तो आप पढ़ सकते हैं इस कॉलबैक के लिए और इस वादे के लिए।

आप अपने को संशोधित करना होगा callSendAPIसमारोह और विशेष रूप से बात यह है कि फेसबुक पर पोस्ट अनुरोध भेजता है। मैं का उपयोग करके अपने मुद्दे का समाधान पेश करेंगे वादों और मॉड्यूल नोड लाने

const fetch = require('node-fetch');

function handlePostback(sender_psid,receivedPostback){ 
  if (payload == 'defined_payload') {
    response = {
      text: 'Some text'
    };
    response2 = //... Another response
    response3 = //... Another response
  callSendAPI(sender_psid,response).then(() => {
    return callSendAPI(sender_psid, response2).then(() => {
      return callSendAPI(sender_psid, response3); // You can add as many calls as you want
      });
   });
  }
}

function callSendAPI(sender_psid,response) {
  let body = {
    recipient: {
      id= sender_psid
    },
    message: response
  };
  const qs = 'access_token=' + encodeURIComponent(FB_PAGE_TOKEN); // Here you'll need to add your PAGE TOKEN from Facebook
  return fetch('https://graph.facebook.com/me/messages?' + qs, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(body),
  });
}

26/11/2017 को 14:19
का स्रोत उपयोगकर्ता

वोट
0

मैं नीचे दिए गए लिंक एकल पोस्ट पर कई प्रतिक्रियाएं वापस लागू करने के लिए जिस तरह से सुलझाने के लिए उपयोगी पाया।

https://codingislove.com/build-facebook-chat-bot-javascript/

जैसे तुमने कहा, सरणी काम करना चाहिए। कई प्रतिक्रिया संदेश के साथ एक सरणी चर बनाएं

var multipleResponse = {
   messages: [{
      response1
   },
   {
      response2
   }]
};

और अपने कार्य करने के लिए सरणी चर धक्का

function callSendAPI(sender_psid,response) {

    let body = {
         recipient: {
            id= sender_psid
         },
         message: []
    };
    // Followed by code for POST request to the webhook
}

अंत में अपने समारोह सरणी सरणी धक्का

body.message.push(multipleResponse.messages);
24/01/2018 को 22:10
का स्रोत उपयोगकर्ता

वोट
0

@Christos Panagiotakopoulos। मैं अपने mainMenuResponse है जो तब का उपयोग कर जंजीर नहीं मिल रहा है। बल्कि, मैं प्रतिक्रिया तीन बार हो रही है। handlePostback समारोह =>

// 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 === 'fashionTip') {
    response = { "text": getFashionTip() } // calls a function which gives a fashion-tip

  // Send the message to acknowledge the postback
  callSendAPI(sender_psid, response).then(() => {
    return callSendAPI(sender_psid, mainMenuResponse)
  });

callSendAPI समारोह =>

// 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",
    "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 mesage:" + err);
    }
  });
}
21/04/2019 को 08:44
का स्रोत उपयोगकर्ता

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more