0

I'm using Twilio Studio to generate an IVR. I want it to search a database for their records via them inputting an account number or something similar.

I have followed their documentation on using ngrok to connect to the database and used their sample 'read database' code, but I have an issue and a couple of questions.

  1. Upon calling into the IVR how would I grab the input from the input widget? I assume "SELECT * FROM Users WHERE account_number = "[input_from_user]", but how does one refer to that user input to plug it into the query?

  2. After that how does one retrieve just the name from the database for the 'say widget' to play a message like "John Doe, you have an unpaid balance...."? In the sample code i saw something like {{widgets.function_read_db.function.body}} to include the data in a text, but they didn't go more in-depth as to how to handle the data.

As for the error I followed all setup steps and replaced the names with my own database names and tables and it connects to the DB, but I get an error

Error: Packets out of order. Got: 45 Expected: 0

My full modified code:

const mysql = require("mysql");

exports.handler = async function(context, event, callback) {
  context.callbackWaitsForEmptyEventLoop = false;
  const config = {
    host: context.host,
    port: context.port,
    user: context.user,
    password: context.password,
    database: context.database
  };

  console.log("connected", config);
  try {
    const db = new Database(config);

    db.connection.connect();
    const users = await db.query("select * from Patients");
    await db.close();
    console.log(Patients);
    callback(null, Patients);
  } catch (e) {
    callback(e);
  }
};

class Database {
  constructor(config) {
    this.connection = mysql.createConnection(config);
  }
  query(sql, args) {
    return new Promise((resolve, reject) => {
      this.connection.query(sql, args, (err, rows) => {
        if (err) return reject(err);
        resolve(rows);
      });
    });
  }
  close() {
    return new Promise((resolve, reject) => {
      this.connection.end(err => {
        if (err) return reject(err);
        resolve();
      });
    });
  }
}
UzZzi
  • 55
  • 1
  • 7

1 Answers1

2

Twilio developer evangelist here.

There's not a lot around the internet on what that mysql error might be. There are similar issues on the mysql module's GitHub and they mostly suggest that the port number isn't correct and that you are connecting to something that isn't mysql. So, all I can suggest is that you confirm you have the correct host and port for your database.

As for accessing the data in the Studio widget. The best thing to do is to format your data and return it as JSON (that is, respond with a JavaScript object in the callback). Then, in Studio, you can refer to the response in other widgets using the liquid variable widgets.function_read_db.parsed.

For example, if you return: { "message": "Hi", "person": { "name": "Bob", "age": 40 } }, you can reference that data in subsequent widgets as:

widgets.function_read_db.parsed.message

widgets.function_read_db.parsed.person.name

widgets.function_read_db.parsed.person.age

You can read more about returning data and using it in the Run Function Widget reference.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • Thanks. Followed the documentation and turns out you need to run "./ngrok tcp 3306" command and leave it open or else the port number and host values change unless you pay for their service and that caused the error. As for formatting the data pulled from the data to JSON format, what would you suggest is the best way to do this? – UzZzi Aug 30 '21 at 19:54
  • I would recommend that you only return the data you need. – philnash Aug 30 '21 at 23:37