Search code examples
pythongoogle-sheetsmessengerfacebook-messenger-botchatfuel

How to search for something in google sheets and retreive it in Chatfuel as a message if found?


So, as I said I need to to do the following in a messenger bot using Chatfuel.

  1. Get an input from the user
  2. Save it in a "user input" in Chatfuel
  3. Take this input and search in the first row in google sheet
  4. If found return the "text" in the second row in messenger
  5. If not, return a saved block from Chatfuel

I know how to do the first two steps but I struggle from the third.

My question is How to do the last three steps using any of Python, JSON or Chatfuel integrations.


I had searched for any source to learn something that helps me but I only ended up watched 10+ videos about chatfuel, google spreedsheets, APIs & JSON. And nothing led me to an answer, I even tried to ask in a programming facebook group but no answer also.


Solution

  • This script will search for the parameter in row one and return the value in the same column from row 2.

    function searchFirstTwoRows(s)
    {
      var s = (typeof(s) != 'undefined')?s:'';
      var rngA = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
      var r = '';
      if(s)
      {
        for(var i=0;i<rngA[0].length;i++)
        {
          if(rngA[0][i]==s)
          {
            r=(rngA[1][i] != 'undefined')?rngA[1][i]:'';
            break;
          }
        }
      }
      return r;
    }
    

    Dealing with Messenger and Chatfuel is up to you.