Search code examples
twitter-bootstraptwiliotwilio-functionstwilio-studio

Is it possible, in Twilio Studio, to track and sync data from a gather input widget to a database file such as google spreadsheet or JSON?


The project looks like this. When someone calls my Twilio number,

  1. The IVR will ask for Mortgage id number using the Gather Input widget.

  2. Once the caller input their Mortgage id number.

  3. The IVR will read back the following information that is equivalent to Mortgage id number data:

    -BORROWER NAME ( Borrower...John Doe)
    

    -LENDER NAME (Your loan with.... Wells Fargo Bank)

    • CLOSED DATE ( Closed on... March 16, 2021)...

    (I believe I need a database for this it could be a google spreadsheet or JSON file that will serve as Data Assets to look for the specific information written above.)

Let me know guys if this is possible in twilio studio?

Thanks in advance.


Solution

  • This is certainly possible and a very common use case for Twilio Studio. For development, you could use a flat file hosted on Twilio Assets to host the JSON but in production, you would interface with a REST API exposed by your backend data source you are using to lookup the Mortgage id and return the associated information.

    Below is an example parsing JSON from a Twilio Asset. You could also use Twilio Sync, here is a CRUD code example using Twilio Functions, understanding that Sync is not a replacement for a high performance DB.

    You could also use Airtable.

    Using Airtable as a database for your Twilio app

    Writing to Airtable from your Twilio app

    Reading / Parsing a Twilio Asset

    const fs = require('fs');
    
    exports.handler = function(context, event, callback) {
        let fileName = 'blockist.json';
        let file = Runtime.getAssets()[fileName].path;
        let text = fs.readFileSync(file);
        let blocklist = Object.values(JSON.parse(text));
        console.log(blocklist);
        
        let twiml = new Twilio.twiml.VoiceResponse();
      
        let blocked = true;
        if (blockist.length > 0) {
            if (blockist.indexOf(event.From) === -1) {
            blocked = false;
        }
      }
      if (blocked) {
        twiml.reject();
      }
      else {
      // if the caller's number is not blocked, redirect to your existing webhook
        twiml.redirect("XXXXXX");
      }
      callback(null, twiml);
    };