Skip to content

Instantly share code, notes, and snippets.

@sunnygleason
Created September 28, 2016 23:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunnygleason/49fe12f9ad09a8a8240acdb604ca30a4 to your computer and use it in GitHub Desktop.
Save sunnygleason/49fe12f9ad09a8a8240acdb604ca30a4 to your computer and use it in GitHub Desktop.
AngularJS Voice Commands to PubNub Channel
<!doctype html>
<html>
<head>
<script src="https://cdn.pubnub.com/pubnub-3.15.1.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script src="https://cdn.pubnub.com/sdk/pubnub-angular/pubnub-angular-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="http://cdglabs.github.io/ohm/dist/ohm.js"></script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" />
</head>
<body>
<div class="container" ng-app="PubNubAngularApp" ng-controller="MySpeechCtrl">
<pre>NOTE: make sure to update the PubNub keys below with your keys!</pre>
<h3>MyVoice to Commands</h3>
<pre>
KNOWN COMMANDS:
"set timer for 45 minutes"
"set alarm for 5 o'clock"
"what time is it"
</pre>
<input type="button" ng-click="dictateIt()" value="Speak a Command" />
<br /><br />
<ul>
<li ng-repeat="command in commands track by $index">
{{command.text}}
<a ng-click="handleIt(command)">(handle again)</a>
</li>
</ul>
</div>
<script type="text/ohm-js">
// An Ohm grammar for parsing voice commands
COMMAND {
command = timer_cmd | alarm_cmd | time_isit_cmd
timer_cmd = "set timer for " number " minutes"
alarm_cmd = "set alarm for " number " o'clock"
time_isit_cmd = "what time is it"
number = digit+
}
</script>
<script>
var gram = ohm.grammarFromScriptElement();
var sem = gram.semantics().addOperation('toList',{
number: function(a) { return parseInt(this.interval.contents,10); },
timer_cmd: function(_, number, _) { return ['timer_cmd', number.toList(), " minutes"]; },
alarm_cmd: function(_, hour, _) { return ['alarm_cmd', hour.toList(), " o'clock"]; },
time_isit_cmd: function(x) { return ['time_isit_cmd']; },
command: function(x) { return {text:this.interval.contents, cmd:x.toList()}; }
});
angular.module('PubNubAngularApp', ["pubnub.angular.service"])
.controller('MySpeechCtrl', function($rootScope, $scope, Pubnub) {
$scope.commands = [{text:"no commands yet"}];
$scope.msgChannel = 'MySpeech';
if (!$rootScope.initialized) {
Pubnub.init({
publish_key: 'YOUR_PUB_KEY',
subscribe_key: 'YOUR_SUB_KEY',
ssl:true
});
$rootScope.initialized = true;
}
var msgCallback = function(payload) {
$scope.$apply(function() {
$scope.commands.push(payload);
});
$scope.handleIt(payload);
};
Pubnub.subscribe({ channel: [$scope.msgChannel, $scope.prsChannel], message: msgCallback });
$scope.handleIt = function (command) {
if (command.cmd && command.cmd[0] === "time_isit_cmd") {
window.speechSynthesis.speak(new SpeechSynthesisUtterance("the current time is " + (new Date()).toISOString().split("T")[1].split(".")[0]));
} else if (command.cmd) {
window.speechSynthesis.speak(new SpeechSynthesisUtterance("okay, got it - I will " + command.text));
} else {
window.speechSynthesis.speak(new SpeechSynthesisUtterance("I'm not quite sure about that"));
}
};
$scope.dictateIt = function () {
var theText = "";
var recognition = new webkitSpeechRecognition();
recognition.onresult = function (event) {
for (var i = event.resultIndex; i < event.results.length; i++) {
if (event.results[i].isFinal) {
theText += event.results[i][0].transcript;
}
}
var match = gram.match(theText.toLowerCase());
var result = match.succeeded() ? sem(match).toList() : {text:theText};
Pubnub.publish({
channel: $scope.msgChannel,
message: result
});
};
recognition.start();
};
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment