Am familiar with Twilio Studio and Twilio API (via PHP). Not familiar with Functions/node, so I'm a little lost on how to do this simple requirement...
In Studio I need to call a Function to return an integer of the total number of 'in-progress' and 'ringing' calls to a specific phone number (added together).
For example, if there are 7 'in-progress' and 5 'ringing' the function would return 12.
Any help appreciated. Thanks!
Twilio Functions allow you to run JavaScript in response to an HTTP request (or request from Studio). You can read about how Functions work in the documentation. To fetch the calls and filter them by their current status, you would use the Call Resource. There are examples in the documentation for fetching calls and filtering and all the documentation has examples in Node.js, as well as PHP so you can compare.
Here's a Function I put together quickly (I haven't tested it, but I think it looks good). It fetches the calls that are ringing and in-progress, and returns an object with individual totals and a total. You can then use this in your widget and refer to the values with variables like {{widgets.FUNCTION_WIDGET_NAME.parsed.ringing}}
.
exports.handler = async (context, event, callback) => {
const client = context.getTwilioClient();
const ringingPromise = client.calls.list({ status: 'ringing' });
const inProgressPromise = client.calls.list({ status: 'in-progress' });
const [ringing, inProgress] = await Promise.all([ringingPromise, inProgressPromise]);
const response = {
ringing: ringing.length,
inProgress: inProgress.length,
total: ringing.length + inProgress.length
};
callback(null, response);
}