npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ari-read

v1.0.3

Published

Replicates the asterisk READ Dialplan

Downloads

19

Readme

node-ari-read

npm (scoped) npm bundle size (minified)

Replicates the asterisk READ Dialplan for nodejs. Is a helper for the official nodejs ARI client package ari-client

Install

$ npm i ari-read

Getting Started

Step 1:

After ari begins a new call use the readerFactory to create a read function for the channel, the factory requires 2 params exactly, the channel and ari variable. You need one read per channel.

Params

  • incoming the incoming channel
  • ari the ari application

Step 2:

To read one or more files and get a DTMF Response, await your read function. Pass a single object to this function, the object properties are below

The read function can be passed as a parameter to other parts of your application.

Object properties

  • soundFiles - array of filenames to read (required)
  • digitsInResponse - int number of digits requested, 0 will just read and return on completion, default 0
  • attempts - int required, number of invalid attempts to allow before terminating call, default 3
  • timeout - int required, number of seconds of inactivity to allow before terminating call, default 10
  • responseValidCallback - callback function to test whether the response is valid or not, default is that all responses are valid
  • debug - if set to true it will read out some debug commands, default false

Example

const client = require('ari-client');
const readerFactory = require('ari-read');

client.connect('http://localhost:8088', 'user', 'secret')
  .then((ari)=> {
    //handle new call
    ari.once('StasisStart',  async (event, incoming)=> {
        incoming.answer().then(async () => {
            //create reader
            const read = readerFactory(incoming, ari);

            //read without response
            await read(
                audioFiles: ['welcome'],
                digitsInResponse: 0,
            )
    
            //read with response, must be * or a digit between 1 and 3
            const selection = await read({
                audioFiles: [file1, file2], 
                digitsInResponse: 1, 
                responseValidCallback:  (num) => {
                    return num=='*' || (num > 0 && num <= 3)
                }
            }).catch(
                (err) => { console.log(err) }
            );

            //now you can do an action with your selection
            doSomething(selection); 
        })
    });
    //connect to the statis app
    ari.start('test');
  })
  .done(); // program will crash if it fails to connect

Example using TTS (Text-to-Speech)

This example replaces the readerFactory in the above example with one that will perform TTS before returning the file. Instead of sending soundFiles to this helper, send an array of text to read.

const readerFactory = require("ari-read");

const tts await (string) => {
    //use your favorite library and return the filename you generate.
}

const readerFactoryWithTTS = (channel, ari) => {
    const read = readerFactory(channel, ari);

    //the reader returns an async function for us to await
    return async (props) => {
        const { text} = props;

        let soundFiles = await Promise.all((typeof text === 'string' ? [text] : text).map(s => tts(s)));
        return await read({soundFiles, ...props})
    }
}
module.exports = readerFactoryWithTTS;

Concurrency Notes

This code has been optimized to run at high call load (Author has tested with 300 on concurrent on 4 CPUs)

  • Asterisk ships with low version of nodejs, nodejs 10 has improved performance with async/promises and should be faster when at high load.
  • Transcoding can use significant CPU, ensure that the source files use codecs that use the least CPU for best performance.
  • If you setup clustering on the ARI App, you can give each process a different statis app to connect to. The dialplan can load balance between the statis apps.