Skip to content

Instantly share code, notes, and snippets.

View savelee's full-sized avatar

Lee Boonstra savelee

View GitHub Profile
@savelee
savelee / index.js
Last active August 18, 2022 09:38
Dialogflow Fulfillment Library
'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));
@savelee
savelee / gist:e2877fa44a0747344dde4d49c0e1039d
Last active August 2, 2021 20:32
Installing FreePBX15 with Asterisk 16 on Compute Engine
## 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
@savelee
savelee / index.js
Created April 8, 2020 12:42
Client - Text to Speech - Streaming
// 1)
function playOutput(arrayBuffer){
let audioContext = new AudioContext();
let outputSource;
try {
if(arrayBuffer.byteLength > 0){
// 2)
audioContext.decodeAudioData(arrayBuffer,
function(buffer){
// 3)
@savelee
savelee / simpleserver.js
Created April 8, 2020 12:30
Speech to Text - Streaming
// 1)
async function transcribeAudioStream(audio, cb) {
// 2)
const recognizeStream = speechClient.streamingRecognize(request)
// 3)
.on('data', function(data){
console.log(data);
cb(data);
})
// 4)
@savelee
savelee / simpleserver.js
Created April 8, 2020 12:28
Speech to Text - Streaming
// 1)
speechClient = new speech.SpeechClient();
// 2)
request = {
config: {
sampleRateHertz: sampleRateHertz,
encoding: encoding,
languageCode: languageCode
},
@savelee
savelee / simpleserver.js
Created April 8, 2020 12:22
Dialogflow - Streaming
// 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}`
@savelee
savelee / simpleserver.js
Created April 8, 2020 12:17
Dialogflow Streaming
// 1)
sessionId = uuid.v4();
// 2)
sessionClient = new df.SessionsClient();
sessionPath = sessionClient.sessionPath(projectId, sessionId);
// 3)
request = {
session: sessionPath,
queryInput: {
// 4)
@savelee
savelee / simpleserver.js
Created April 8, 2020 11:56
Server - streaming to Dialogflow
//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 ...
});
@savelee
savelee / index.js
Created April 8, 2020 11:34
Client - Streaming to Dialogflow
// 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
@savelee
savelee / index.js
Created April 8, 2020 11:29
Client - Streaming to Dialogflow
// 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) {