Permalink
Browse files

Version 0.1.0: First working version. Documentation soon.

  • Loading branch information...
1 parent 122eb56 commit 31be4295fc7ece5c2c347f10fb02bac7a9b5ffcd @mko committed Aug 3, 2012
Showing with 170 additions and 2 deletions.
  1. +8 −0 .npmignore
  2. +20 −0 LICENSE
  3. +37 −2 README.md
  4. +88 −0 lib/watson.js
  5. +17 −0 package.json
View
@@ -0,0 +1,8 @@
+*.md
+.DS_Store
+.git*
+Makefile
+docs/
+examples/
+support/
+test/
View
20 LICENSE
@@ -0,0 +1,20 @@
+(The MIT License)
+
+Copyright (c) 2012 Michael Owens
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -1,4 +1,39 @@
-watson-js
+Watson.js - AT&T Speech API Client Library for Node.js
=========
-Watson.js: Simple Node.js API Wrapper for the AT&T Speech API
+Watson.js: Simple Node.js API Client Library for the AT&T Speech API
+
+## Installation
+
+- `watson-js` is available on NPM and maintained in sync with this Github branch. Just include it in your dependencies via `package.json` and `npm install` in your app's root directory to install it in your `node_modules` directory.
+
+## Instructions on Use
+
+- Coming Soon
+
+## Contributors
+
+- [Michael Owens](https://github.com/mowens)
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2012 Michael Owens
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2012 Michael Owens. Licensed via The MIT License.
+ *
+ * Simple API Wrapper for the AT&T Speech API
+ */
+
+// !Dependencies
+var fs = require('fs'),
+ request = require('request');
+
+// !Set up the client options and defaults
+var Watson = exports.Watson = function(options) {
+ this.client_id = options.client_id || null ;
+ this.client_secret = options.client_secret || null;
+ this.access_token = options.access_token || null;
+ this.scope = options.scope || "SPEECH";
+ this.context = options.context || "Generic";
+ this.access_token_url = options.access_token_url || "https://api.att.com/oauth/token"
+ this.api_domain = options.api_domain || "api.att.com";
+};
+
+// Get Access Token Method
+Watson.prototype.getAccessToken = function(callback) {
+ // Set the params for the OAuth 2.0 Request
+ var request_params = {
+ client_id: this.client_id,
+ client_secret: this.client_secret,
+ grant_type: "client_credentials",
+ scope: this.scope
+ };
+
+ // Create the Param String
+ var paramlist = [];
+ for (pk in request_params) {
+ paramlist.push(pk + "=" + request_params[pk]);
+ };
+ var body_string = paramlist.join("&");
+
+ // !Details of the OAuth 2.0 Request
+ var request_details = {
+ method: "POST",
+ headers: {'content-type' : 'application/x-www-form-urlencoded'},
+ uri: this.access_token_url,
+ body: body_string
+ };
+
+ // !Make the request
+ request(request_details, function(error, response, body) {
+ if(error) {
+ error = new Error('Failed to get access token.');
+ }
+ // Pass any errors and the Access Token back to the app
+ callback(error, JSON.parse(body)['access_token']);
+ });
+};
+
+// !SpeechToText API Call Wrapper
+Watson.prototype.speechToText = function(speechFile, accessToken, callback) {
+ // !Details of the API Call Request
+ var request_details = {
+ method: 'POST',
+ headers: {
+ 'Accept': 'application/json',
+ 'Authorization': 'Bearer ' + accessToken, // Access Token must be retrieved before making API Calls
+ 'Content-Type': 'audio/wav', // TODO: Add dynamic support for audio/amr
+ 'X-SpeechContext': this.context // Possible Values: Generic, UVerseEPG, BusinessSearch, Websearch, SMS, Voicemail, QuestionAndAnswer
+ },
+ uri: "https://" + this.api_domain + "/rest/1/SpeechToText"
+ };
+ // !Pipe the Speech file from the Node.js server to the AT&T API server
+ fs.createReadStream(speechFile).pipe(request(
+ request_details,
+ function(error, response, body) {
+ // Try to read the `body` as JSON
+ try {
+ var parsed = JSON.parse(body);
+ } catch(e) {
+ // If it fails, it's an invalid API Reply
+ error = new Error('API Reply is not a valid JSON string.');
+ error.reply = body;
+ } finally {
+ // Toss the error and/or parsed response back to the app
+ callback(error, parsed);
+ }
+ }
+ )
+ );
+};
View
@@ -0,0 +1,17 @@
+{
+ "name": "watson-js",
+ "version": "0.1.0",
+ "description": "Watson.js: Simple Node.js API Wrapper for the AT&T Speech API",
+ "author": "Michael Owens <mk@mowens.com> (http://mowens.com/)",
+ "repository": {
+ "type": "git",
+ "url": "http://github.com/mowens/watson-js.git"
+ },
+ "main": "./lib/watson",
+ "dependencies": {
+ "oauth": ">= 0.9.8",
+ "request": ">= 1.9.0"
+ },
+ "engines": { "node": ">= 0.4.0" },
+ "keywords": ["att", "audio", "speechtotext", "speech2text", "speech", "api", "wrapper"]
+}

0 comments on commit 31be429

Please sign in to comment.