Skip to content

Instantly share code, notes, and snippets.

@daliborgogic
Last active March 29, 2019 19:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daliborgogic/8a9e41e2f3151e037fe1c04c222fb35b to your computer and use it in GitHub Desktop.
Save daliborgogic/8a9e41e2f3151e037fe1c04c222fb35b to your computer and use it in GitHub Desktop.
Speech Recognition
<template>
<div>
<IconMic @dictate="dictate"/>
<div>{{ text }}</div>
</div>
</template>
<script>
import IconMic from '@/components/icons/mic'
export default {
components: {
IconMic
},
data: () => ({
synth: null,
recognition: null,
complete: null,
text: null
}),
mounted () {
window.SpeechRecognition = window.webkitSpeechRecognition || window.SpeechRecognition
this.synth = window.speechSynthesis
this.recognition = new SpeechRecognition()
},
methods: {
dictate () {
this.recognition.start()
this.recognition.onresult = event => {
const speechToText = event.results[0][0].transcript
this.text = speechToText
if (event.results[0].isFinal) {
if (speechToText.includes('check cert')) {
this.speak(getCert)
}
}
}
},
speak (action) {
this.complete = new SpeechSynthesisUtterance(action())
this.synth.speak(complete)
},
async getCert () {
const x = await (await fetch(`https://api.devoops/v1/cert`)).json()
if (x.code === 404) {
this.complete = new SpeechSynthesisUtterance(`I cannot find the cert`);
this.synth.speak(this.complete)
return
}
this.complete = new SpeechSynthesisUtterance(`Cert is valid for ${x.validTo} days`);
this.synth.speak(this.complete)
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment