简体   繁体   中英

Cloud Functions for Firebase: HTTP trigger - Promise / Async

I am developing a Facebook chat bot which hooked to Cloud Functions for Firebase.

pseudo code:

  1. Received user message
  2. Check if that is a valid URL
  3. Retrieve content / article + html from the URL (using node library read-art )
  4. Store there data to Firebase database
  5. Send response to user(Facebook message)

I wrote all 5 parts and tested all parts separately, but I have problem chaining up all of them. I would like to ask a couple of questions.

  1. where/when should I call the response.sendStatus(200); ? Is firebase function terminated right after I called response.sendStatus(200); ? (According to firebase doc, the answer seems yes). So I tried to place this line inside the promise block, and firebase prompts timeout messages in the log file

  2. any suggestion of solving the problem? how should i structure my function?


exports.messengerWebhook = functions.https.onRequest((request, response) => {
    var data = request.body;
    // Make sure this is a page subscription
    if (data.object === "page") {
        // Iterate over each entry - there may be multiple if batched
       data.entry.forEach(function(entry) {
           var pageID = entry.id;
           var timeOfEvent = entry.time;

           // Iterate over each messaging event
           entry.messaging.forEach(function(event) {
               if (event.message) {
                   receivedMessage(event);
               } else {
                   console.log("Webhook received unknown event: ", event);
               }
           });
       });
       response.sendStatus(200);
    }
});

function receivedMessage(event) {
  // Putting a stub for now, we'll expand it in the following steps
  var senderID = event.sender.id;
  var recipientID = event.recipient.id;
  var timeOfMessage = event.timestamp;
  var message = event.message;

  console.log(
    "Received message for user %d and page %d at %d with message:",
    senderID,
    recipientID,
    timeOfMessage
  );
  console.log(JSON.stringify(message));

  var messageId = message.mid;

  var messageText = message.text;
  var messageAttachments = message.attachments;

  if (validUrl.isHttpUri(messageText) || validUrl.isHttpsUri(messageText)) {
    // If we receive a text message, check to see if it matches a keyword
    // and send back the example. Otherwise, just echo the text we received.
    switch (messageText) {
      case "generic":
        extractContentAndSave(messageText);
        break;
      default:
        sendTextMessage(senderID, messageText);
    }
  } else {
    sendTextMessage(senderID, "This is not a url");
  }
}

function extractContentAndSave(url) {
  read(url, function(err, art, options, resp) {
    if (err) {
      throw err;
    }
    var title = art.title, // title of article
      content = art.content, // content of article
      html = art.html; // whole original innerHTML

    admin
      .database()
      .ref("webpage")
      .push({
        url: url,
        title: title,
        content: content,
        rawHtml: html
        //cachedPageUrl:
      })
      .then(
        {
          //Send response to user via Facebook Messenger
        }
      );
    console.log("[STATUS CODE]", resp && resp.statusCode);
    // sendTextMessage(senderID, title);
  });
}

I don't know if this qualify as an answer (as I don't have any time to modify your code) but as you said you should only call response.sendStatus(200); after everything is done, by everything I mean that every Promise you started is done.

So your function extractContentAndSave must return a Promise (your Firebase push), the caller of this function ie receivedMessage must handle the Promise received from extractContentAndSave and in every case should return a Promise.

And in the end messengerWebhook must handle the Promise returned by receivedMessage . You have to make sure your promises bubble up to the top function of your architecture.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM