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

twilio-stt-gspeech

v1.1.0

Published

Speech to text in Twilio over websocket using Google Speech API

Downloads

96

Readme

twilio-stt-gspeech

Speech to text in Twilio over websocket using Google Speech API

Introduction

This package is an extension of this article. In the above article, live transcribing phone calls using Twilio Media Streams and Google Speech-to-text is explained. This package helps not only in converting speech to text but also returns a promise so that it can be binded to a twilio webhook.

Requirements

API

init (websocketServer: Server, googleSpeechClient: SpeechClient, debugOption?: boolean): void
  • This is the very fist method that has to be invoked, when a web socket is created.
  • It takes the parameters
    • websocketServer, the created websocket server has to passed
    • googleSpeechClient, instance of google SpeechCLient has to be passed
    • debugOption, is an optional Parameter to view the debug messages over the console.
listen (callSid: string, timeout: number): Promise
  • This is the listener API to which for a particular caller's Speech To Text value will be received as an output
  • This function returns a promise.
    • When the promise resolved Speech To Text value is obtained
    • When the promise rejected might be due to timeout
  • This function takes the parameters such as
    • callSid, the caller's SID, which is an unique ID generated by Twilio
    • timeout, number in milliseconds. For how long one can wait for the promise to resolve.

Example

Setting up a local server

Before getting into this please configure your - twilio voice configuration's request url with https://{host}/voice - Download your Google Cloud Service account credentials as JSON and set the path accordingly - For Windows, in command prompt set GOOGLE_APPLICATION_CREDENTIALS=D:\path\to\credentials\file.json - For linux export GOOGLE_APPLICATION_CREDENTIALS=D:\path\to\credentials\file.json

Create a project folder, index.js file and install the mentioned modules

$ mkdir twilio-transcribe-test
$ cd twilio-transcribe-test
$ touch index.js
$ npm i express @google-cloud/speech twilio ws twilio-stt-gspeech

Now we need to setup the server and configure the twilio speech to text using google speech API. Open the index.js file and add the following code.

const express = require('express');
const http = require("http");
const speech = require('@google-cloud/speech');
const WebSocket = require('ws');

const { init } = require('twilio-stt-gspeech');

const app = express();
const server = http.createServer(app);

wss = new WebSocket.Server({ server });
init(wss, new speech.SpeechClient(), true);

app.use(express.json());
app.use(express.urlencoded({
    extended: true
}))

app.use('/voice', require('./router'));

server.listen(8080, () => {
    console.log("server running at 8080");
});

In the above code, init method has to be imported from twilio-stt-gspeech package. Next the parameters for the init method includes created websocket, google speech client, and an optional boolean parameter to display debug messages. Next, a listener is added for the route /voice. Create a folder with name as router and a file inside the folder and name it as index.js.

$ mkdir router
$ cd router
$ touch index.js

Open the index.js file and add the following code

const { VoiceResponse } = require('twilio').twiml;

exports.init = (host, { CallSid }) => {

    const twiml = new VoiceResponse();
    twiml.start()
    .stream({
        url: `wss://${host}/${CallSid}`,
        name: CallSid
    });
    twiml.redirect('/voice/stream');
    return twiml.toString();
}

exports.stream = ({ CallSid }) => {

    const twiml = new VoiceResponse();
    twiml.stop()
    .stream({
        name: CallSid
    });
    return new Promise( (resolve, reject) => {
    
        require('twilio-stt-gspeech').listen(CallSid)
        .then( data => {
            twiml.say("You said "+data);
            resolve(twiml.toString());
        })
        .catch((err) => {
            console.log("Error occurred", err);
            reject();
        })
    });
}

When a call is made to a twilio number, and as per the twilio webhook endpoint which is configured to https://{host}/voice/ the init method is invoked. The init method opens up a websocket stream for the caller and expects the user to speak. The websocket is opened using the twilio API start and inside it stream API is invoked using the following parameters that has to be mentioned explicitly. url is the the websocket url with paths containing the caller's SID and language with country code (Eg. en-IN for Indian English). By default the language is en-US. The next parameter is the name the value should be the caller's SID. And finally it completes the request by redirecting to another route i.e., https://{host}/voice/stream/. The redirected route will listen for the speech to text value from the package's(twilio-stt-gspeech) listen API. This API can be configured with a timeout so that for how long the route can wait to receive the speech to text value. Hence, when the user speaks, it is received via websocket, and when the user pauses after speaking, then the Speech to Text value is resolved via the listen method's promise.