This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const functions = require('firebase-functions'); | |
const { WebhookClient, Card, Suggestion } = require('dialogflow-fulfillment'); | |
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements | |
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => { | |
const agent = new WebhookClient({ request, response }); | |
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Create Compute | |
$ gcloud beta compute --project=dialogflow-ivr instances create "freepbx" --zone "us-central1-a" --machine-type=n1-standard-1 --subnet=default --network-tier=PREMIUM --maintenance-policy=MIGRATE --service-account=293782603063-compute@developer.gserviceaccount.com --scopes=https://www.googleapis.com/auth/devstorage.read_only,https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring.write,https://www.googleapis.com/auth/servicecontrol,https://www.googleapis.com/auth/service.management.readonly,https://www.googleapis.com/auth/trace.append --image=centos-7-v20200420 --tags "http-server","https-server" --image-project=centos-cloud --boot-disk-size=40GB --boot-disk-type=pd-standard --boot-disk-device-name=instance-1 --no-shielded-secure-boot --shielded-vtpm --shielded-integrity-monitoring --reservation-affinity=any | |
## Edit the VM, add the following metadata label: | |
startup-script : sudo ufw ssh | |
## SSH into the machine | |
$ gcloud compute ssh <instancename> --project=p |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1) | |
function playOutput(arrayBuffer){ | |
let audioContext = new AudioContext(); | |
let outputSource; | |
try { | |
if(arrayBuffer.byteLength > 0){ | |
// 2) | |
audioContext.decodeAudioData(arrayBuffer, | |
function(buffer){ | |
// 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1) | |
async function transcribeAudioStream(audio, cb) { | |
// 2) | |
const recognizeStream = speechClient.streamingRecognize(request) | |
// 3) | |
.on('data', function(data){ | |
console.log(data); | |
cb(data); | |
}) | |
// 4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1) | |
speechClient = new speech.SpeechClient(); | |
// 2) | |
request = { | |
config: { | |
sampleRateHertz: sampleRateHertz, | |
encoding: encoding, | |
languageCode: languageCode | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1) | |
async function detectIntentStream(audio, cb) { | |
// 2) | |
const stream = sessionClient.streamingDetectIntent() | |
.on('data', function(data){ | |
// 3) | |
if (data.recognitionResult) { | |
console.log( | |
`Intermediate transcript: | |
${data.recognitionResult.transcript}` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1) | |
sessionId = uuid.v4(); | |
// 2) | |
sessionClient = new df.SessionsClient(); | |
sessionPath = sessionClient.sessionPath(projectId, sessionId); | |
// 3) | |
request = { | |
session: sessionPath, | |
queryInput: { | |
// 4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1 | |
io.on('connect', (client) => { | |
//2 | |
client.on('message', async function(data) { | |
const dataURL = data.audio.dataURL.split(',').pop(); | |
let fileBuffer = Buffer.from(dataURL, 'base64'); | |
//3 ... | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1) | |
timeSlice: 4000, | |
// 2) | |
// as soon as the stream is available | |
ondataavailable: function(blob) { | |
// 3 | |
// making use of socket.io-stream for bi-directional | |
// streaming, create a stream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1) | |
stopRecording.onclick = function() { | |
// recording stopped | |
startRecording.disabled = false; | |
stopRecording.disabled = true; | |
// stop audio recorder | |
recordAudio.stopRecording(function() { | |
// after stopping the audio, get the audio data | |
recordAudio.getDataURL(function(audioDataURL) { |