8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/tsconfig.es6.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "target": "ES6",
4 | "outDir": "es6",
5 | "sourceMap": false,
6 | "typeRoots": ["node_modules/@types/"],
7 | "types": [],
8 | "declaration": false
9 | },
10 | "files": [
11 | "declarations.d.ts",
12 | "ts/ApiAiClient.ts"
13 | ]
14 | }
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | export * from "./es6/ApiAiClient";
18 |
--------------------------------------------------------------------------------
/index.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | ///
18 | export * from "./ts/ApiAiClient";
19 |
--------------------------------------------------------------------------------
/es6/Request/EventRequest.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | import Request from "./Request";
18 | export class EventRequest extends Request {
19 | }
20 |
--------------------------------------------------------------------------------
/es6/Request/TextRequest.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | import Request from "./Request";
18 | export default class TextRequest extends Request {
19 | }
20 |
--------------------------------------------------------------------------------
/ts/Request/EventRequest.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | import Request from "./Request";
18 | export class EventRequest extends Request {
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/ts/Request/TextRequest.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | import Request from "./Request";
18 | export default class TextRequest extends Request {
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/ts/Request/ContextsRequest.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | import Request from "./Request";
18 | /**
19 | * @todo: implement
20 | */
21 |
22 | export class ContextsRequest extends Request {}
23 |
--------------------------------------------------------------------------------
/ts/Request/LocalTTSRequest.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | export class LocalTTSRequest {
18 | private isSupported() {
19 | return !!window.speechSynthesis;
20 | }
21 | private getLanguage() {
22 |
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/demo/typescript-project/src/index.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | import {ApiAiEnabledApplication} from "./Application";
18 | const app = new ApiAiEnabledApplication();
19 |
20 | app.init("ACCESS_TOKEN");
21 |
22 |
23 | window["app"] = app;
24 |
--------------------------------------------------------------------------------
/demo/typescript-project/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "api-ai-javascript-typescript-demo-project",
3 | "version": "2.0.0-beta.20",
4 | "description": "Javascript SDK for https://api.ai/ typescript demo project",
5 | "devDependencies": {
6 | "@types/es6-promise": "0.0.32",
7 | "api-ai-javascript": "^2.0.0-beta.21",
8 | "awesome-typescript-loader": "^3.0.8",
9 | "typescript": "^2.2.1",
10 | "webpack": "^2.2.1",
11 | "webpack-dev-server": "^2.4.1"
12 | },
13 | "scripts": {
14 | "build": "webpack",
15 | "start": "webpack-dev-server"
16 | },
17 | "repository": {
18 | "type": "git",
19 | "url": "git+https://github.com/api-ai/api-ai-javascript.git"
20 | },
21 | "author": "Eugeny Shingarev",
22 | "license": "Apache-2.0",
23 | "bugs": {
24 | "url": "https://github.com/api-ai/api-ai-javascript/issues"
25 | },
26 | "homepage": "https://github.com/api-ai/api-ai-javascript#readme"
27 | }
28 |
--------------------------------------------------------------------------------
/ts/Utils.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | export default class ApiAiUtils {
18 | /**
19 | * make it in more appropriate way
20 | * @param object
21 | * @returns object
22 | */
23 | public static cloneObject(object: T): T {
24 | return JSON.parse(JSON.stringify(object));
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/ts/Request/VoiceRequest.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | import {ApiAiClient} from "../ApiAiClient";
18 | import {IRequestOptions} from "../Interfaces";
19 | import Request from "./Request";
20 |
21 | /**
22 | * @todo: implement
23 | */
24 |
25 | class VoiceRequest extends Request {
26 | constructor(client: ApiAiClient, options: IRequestOptions = {}) {
27 | super(client, options);
28 |
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/demo/simple/demoFunctions.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | "use strict";
18 |
19 | /**
20 | * All this stuff is moved into global namespace and separate files just to be
21 | * MAXIMUM clear and easy to understand
22 | */
23 |
24 | var client;
25 | window.init = function(token) {
26 | client = new ApiAi.ApiAiClient({accessToken: token});
27 | };
28 |
29 | function sendText(text) {
30 | return client.textRequest(text);
31 | }
32 |
--------------------------------------------------------------------------------
/spec/Utils.spec.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | import Utils from "../ts/Utils";
18 | const expect = chai.expect;
19 |
20 | describe("ApiAi.Utils", () => {
21 | describe("#cloneObject", () => {
22 | it ("should clone object", () => {
23 | let originalObject = {};
24 | let clonedObject = Utils.cloneObject(originalObject);
25 |
26 | expect(originalObject).not.to.eq(clonedObject);
27 | expect(JSON.stringify(originalObject)).to.eq(JSON.stringify(clonedObject));
28 | });
29 | });
30 | });
31 |
--------------------------------------------------------------------------------
/ts/Models/Entity.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | export class Entity implements IEntity{
18 | public name: string;
19 | public entires: Entity.Entry[];
20 | }
21 |
22 | export namespace Entity {
23 | export class Entry implements IEntity.IEntry {
24 | public value: string;
25 | public synonyms: string[];
26 | }
27 | }
28 |
29 | export interface IEntity {
30 | name: string;
31 | entires: IEntity.IEntry[];
32 | }
33 |
34 | export namespace IEntity {
35 | export interface IEntry {
36 | value: string;
37 | synonyms: string[];
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/es6/Errors.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | export class ApiAiBaseError extends Error {
18 | constructor(message) {
19 | super(message);
20 | this.message = message;
21 | this.stack = new Error().stack;
22 | }
23 | }
24 | export class ApiAiClientConfigurationError extends ApiAiBaseError {
25 | constructor(message) {
26 | super(message);
27 | this.name = "ApiAiClientConfigurationError";
28 | }
29 | }
30 | export class ApiAiRequestError extends ApiAiBaseError {
31 | constructor(message, code = null) {
32 | super(message);
33 | this.message = message;
34 | this.code = code;
35 | this.name = "ApiAiRequestError";
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/ts/ApiAiConstants.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | export namespace ApiAiConstants {
18 | export enum AVAILABLE_LANGUAGES {
19 | EN = "en" as any, DE = "de" as any, ES = "es" as any, PT_BR = "pt-BR" as any, ZH_HK = "zh-HK" as any,
20 | ZH_CN = "zh-CN" as any, ZH_TW = "zh-TW" as any, NL = "nl" as any, FR = "fr" as any, IT = "it" as any,
21 | JA = "ja" as any, KO = "ko" as any, PT = "pt" as any, RU = "ru" as any, UK = "uk" as any
22 | }
23 |
24 | export const VERSION: string = "2.0.0-beta.20";
25 | export const DEFAULT_BASE_URL: string = "https://api.api.ai/v1/";
26 | export const DEFAULT_API_VERSION: string = "20150910";
27 | export const DEFAULT_CLIENT_LANG: AVAILABLE_LANGUAGES = AVAILABLE_LANGUAGES.EN;
28 | }
29 |
--------------------------------------------------------------------------------
/ts/Errors.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | export abstract class ApiAiBaseError extends Error {
18 |
19 | public abstract name: string;
20 | public stack: string;
21 | constructor(public message: string) {
22 | super(message);
23 | this.stack = new Error().stack;
24 | }
25 | }
26 |
27 | export class ApiAiClientConfigurationError extends ApiAiBaseError {
28 |
29 | public name: string = "ApiAiClientConfigurationError";
30 |
31 | constructor(message: string) {
32 | super(message);
33 | }
34 | }
35 |
36 | export class ApiAiRequestError extends ApiAiBaseError {
37 |
38 | public name: string = "ApiAiRequestError";
39 |
40 | constructor(public message: string, public code: number = null) {
41 | super(message);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/spec/api.spec.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | import {ApiAiClient} from "../ts/ApiAiClient";
18 | describe("API", () => {
19 |
20 | const client = new ApiAiClient({accessToken: "3485a96fb27744db83e78b8c4bc9e7b7"});
21 |
22 | describe("Text Query", () => {
23 | it ("should return response", function (done) {
24 | this.timeout(5000);
25 | client.textRequest("Hello!").then((response) => {
26 | chai.expect(response.result.action).to.eq("greeting");
27 | chai.expect(response.result.resolvedQuery).to.eq("Hello!");
28 | done();
29 | });
30 | });
31 |
32 | it("should respect UTF-8", function(done) {
33 | this.timeout(5000);
34 | client.textRequest("¿Cuál es la población de España?").then((response) => {
35 | chai.expect(response.result.resolvedQuery).to.eq("¿Cuál es la población de España?");
36 | done();
37 | });
38 | });
39 | });
40 |
41 | });
42 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # How to become a contributor and submit your own code
2 |
3 | ## Contributor License Agreements
4 |
5 | We'd love to accept your sample apps and patches! Before we can take them, we
6 | have to jump a couple of legal hurdles.
7 |
8 | Please fill out either the individual or corporate Contributor License Agreement
9 | (CLA).
10 |
11 | * If you are an individual writing original source code and you're sure you
12 | own the intellectual property, then you'll need to sign an [individual CLA](https://developers.google.com/open-source/cla/individual).
13 | * If you work for a company that wants to allow you to contribute your work,
14 | then you'll need to sign a [corporate CLA](https://developers.google.com/open-source/cla/corporate).
15 |
16 | Follow either of the two links above to access the appropriate CLA and
17 | instructions for how to sign and return it. Once we receive it, we'll be able to
18 | accept your pull requests.
19 |
20 | ## Contributing A Patch
21 |
22 | 1. Submit an issue describing your proposed change to the repo in question.
23 | 1. The repo owner will respond to your issue promptly.
24 | 1. If your proposed change is accepted, and you haven't already done so, sign a
25 | Contributor License Agreement (see details above).
26 | 1. Fork the desired repo, develop and test your code changes.
27 | 1. Ensure that your code adheres to the existing style in the sample to which
28 | you are contributing. Refer to the
29 | [Google Cloud Platform Samples Style Guide](https://github.com/GoogleCloudPlatform/Template/wiki/style.html) for the
30 | recommended coding standards for this organization.
31 | 1. Ensure that your code has an appropriate set of unit tests which all pass.
32 | 1. Submit a pull request.
33 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "api-ai-javascript",
3 | "version": "2.0.0-beta.21",
4 | "description": "Javascript SDK for https://api.ai/",
5 | "main": "index",
6 | "dependencies": {
7 | },
8 | "devDependencies": {
9 | "@types/chai": "^3.4.35",
10 | "@types/es6-promise": "0.0.32",
11 | "@types/mocha": "^2.2.39",
12 | "awesome-typescript-loader": "^3.0.8",
13 | "babel-polyfill": "^6.23.0",
14 | "chai": "^3.5.0",
15 | "eslint": "^3.16.1",
16 | "eslint-config-google": "^0.7.1",
17 | "karma": "^1.5.0",
18 | "karma-chai": "^0.1.0",
19 | "karma-mocha": "^1.1.1",
20 | "karma-phantomjs-launcher": "^1.0.2",
21 | "karma-sinon": "^1.0.5",
22 | "karma-typescript-preprocessor2": "^1.2.1",
23 | "karma-webpack": "^2.0.2",
24 | "mocha": "^3.2.0",
25 | "mocha-phantomjs": "^4.1.0",
26 | "phantomjs-prebuilt": "^2.1.12",
27 | "rimraf": "^2.6.1",
28 | "sinon": "^1.17.3",
29 | "tslint": "^4.5.1",
30 | "typescript": "^2.3.2",
31 | "webpack": "^2.5.1",
32 | "webpack-dev-server": "^2.4.5"
33 | },
34 | "scripts": {
35 | "start": "webpack-dev-server --port 8002",
36 | "build": "webpack && webpack --env.compress && rimraf ./es6 && tsc -p ./tsconfig.es6.json",
37 | "test": "karma start"
38 | },
39 | "repository": {
40 | "type": "git",
41 | "url": "git+https://github.com/api-ai/api-ai-javascript.git"
42 | },
43 | "keywords": [
44 | "api.ai",
45 | "nlu",
46 | "nlp",
47 | "chatbots",
48 | "conversational",
49 | "UX"
50 | ],
51 | "author": "Eugeny Shingarev",
52 | "license": "Apache-2.0",
53 | "bugs": {
54 | "url": "https://github.com/api-ai/api-ai-javascript/issues"
55 | },
56 | "homepage": "https://github.com/api-ai/api-ai-javascript#readme",
57 | "eslintConfig": {
58 | "extends": "google"
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/karma.conf.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | var webpackConfig = require('./webpack.config')({});
18 |
19 | module.exports = function(config) {
20 | config.set({
21 | basePath: '',
22 | frameworks: ['mocha', 'chai'],
23 | files: [
24 | 'node_modules/babel-polyfill/dist/polyfill.js',
25 | 'spec/**/*.spec.ts',
26 | ],
27 | exclude: [],
28 | preprocessors: {
29 | 'spec/**/*.spec.ts': ['webpack'],
30 | },
31 | webpack: {
32 | module: webpackConfig.module,
33 | resolve: webpackConfig.resolve,
34 | },
35 | phantomjsLauncher: {
36 | // Have phantomjs exit if a ResourceError is encountered
37 | // (useful if karma exits without killing phantom)
38 | // exitOnResourceError: true,
39 | base: 'PhantomJS',
40 | flags: [
41 | '--web-security=false',
42 | '--load-images=true',
43 | '--ignore-ssl-errors=yes',
44 | '--ssl-protocol=any',
45 | ],
46 | },
47 | reporters: ['progress'],
48 | port: 9876,
49 | colors: true,
50 | logLevel: config.LOG_INFO,
51 | autoWatch: true,
52 | browsers: ['PhantomJS'],
53 | singleRun: false,
54 | concurrency: Infinity,
55 | });
56 | };
57 |
--------------------------------------------------------------------------------
/demo/typescript-project/webpack.config.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | "use strict";
18 |
19 | const webpack = require('webpack');
20 | const path = require('path');
21 |
22 | module.exports = {
23 | entry: [
24 | path.join(__dirname, 'src', 'index.ts')
25 | ],
26 | output: {
27 | path: path.join(__dirname, 'deploy'),
28 | publicPath: "/deploy",
29 | filename: 'index.js'
30 | },
31 | plugins: [
32 | new webpack.LoaderOptionsPlugin({
33 | minimize: true,
34 | debug: false
35 | }),
36 | new webpack.optimize.UglifyJsPlugin({
37 | compress: {
38 | warnings: true
39 | },
40 | output: {
41 | comments: false
42 | },
43 | sourceMap: false
44 | })
45 | ],
46 | module: {
47 | loaders: [
48 | {test: /\.tsx?$/, loader: "awesome-typescript-loader"}
49 | ]
50 | },
51 | resolve: {
52 | modules: [
53 | 'node_modules',
54 | path.resolve(__dirname, 'app')
55 | ],
56 | extensions: ['.ts', '.js']
57 | },
58 | devServer: {
59 | port: 8000
60 | }
61 | };
62 |
--------------------------------------------------------------------------------
/demo/typescript-project/src/Application.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | import {ApiAiClient, IStreamClient} from "api-ai-javascript/ApiAiClient";
18 | import {ApiAiStreamClient} from "api-ai-javascript/ApiAiStreamClient";
19 |
20 | export class ApiAiEnabledApplication {
21 |
22 | private static BUTTON_ID = "button";
23 |
24 | private apiAiClient: ApiAiClient;
25 | private button: HTMLButtonElement;
26 |
27 | private isListening: boolean = false;
28 |
29 | public init(accessToken: string): ApiAiEnabledApplication {
30 |
31 | this.apiAiClient = new ApiAiClient({accessToken, streamClientClass: ApiAiStreamClient});
32 | this.button = document.getElementById(ApiAiEnabledApplication.BUTTON_ID) as HTMLButtonElement;
33 | this.button.addEventListener("click", this.handleClick.bind(this));
34 | return this;
35 | }
36 |
37 | private handleClick() {
38 | this.apiAiClient.textRequest("test").then((response) => {
39 | console.log(response);
40 | });
41 | }
42 |
43 | private setIsListening(isListening: boolean) {
44 | this.isListening = isListening;
45 | this.button.innerText = (isListening) ? "Stop listening" : "Start listening";
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/es6/Interfaces.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | export var IStreamClient;
18 | (function (IStreamClient) {
19 | let ERROR;
20 | (function (ERROR) {
21 | ERROR[ERROR["ERR_NETWORK"] = 0] = "ERR_NETWORK";
22 | ERROR[ERROR["ERR_AUDIO"] = 1] = "ERR_AUDIO";
23 | ERROR[ERROR["ERR_SERVER"] = 2] = "ERR_SERVER";
24 | ERROR[ERROR["ERR_CLIENT"] = 3] = "ERR_CLIENT";
25 | })(ERROR = IStreamClient.ERROR || (IStreamClient.ERROR = {}));
26 | let EVENT;
27 | (function (EVENT) {
28 | EVENT[EVENT["MSG_WAITING_MICROPHONE"] = 0] = "MSG_WAITING_MICROPHONE";
29 | EVENT[EVENT["MSG_MEDIA_STREAM_CREATED"] = 1] = "MSG_MEDIA_STREAM_CREATED";
30 | EVENT[EVENT["MSG_INIT_RECORDER"] = 2] = "MSG_INIT_RECORDER";
31 | EVENT[EVENT["MSG_RECORDING"] = 3] = "MSG_RECORDING";
32 | EVENT[EVENT["MSG_SEND"] = 4] = "MSG_SEND";
33 | EVENT[EVENT["MSG_SEND_EMPTY"] = 5] = "MSG_SEND_EMPTY";
34 | EVENT[EVENT["MSG_SEND_EOS_OR_JSON"] = 6] = "MSG_SEND_EOS_OR_JSON";
35 | EVENT[EVENT["MSG_WEB_SOCKET"] = 7] = "MSG_WEB_SOCKET";
36 | EVENT[EVENT["MSG_WEB_SOCKET_OPEN"] = 8] = "MSG_WEB_SOCKET_OPEN";
37 | EVENT[EVENT["MSG_WEB_SOCKET_CLOSE"] = 9] = "MSG_WEB_SOCKET_CLOSE";
38 | EVENT[EVENT["MSG_STOP"] = 10] = "MSG_STOP";
39 | EVENT[EVENT["MSG_CONFIG_CHANGED"] = 11] = "MSG_CONFIG_CHANGED";
40 | })(EVENT = IStreamClient.EVENT || (IStreamClient.EVENT = {}));
41 | })(IStreamClient || (IStreamClient = {}));
42 |
--------------------------------------------------------------------------------
/declarations.d.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | interface Navigator {
18 | Resampler: any;
19 | webkitGetUserMedia: any;
20 | mozGetUserMedia: any;
21 | }
22 | interface AudioContext {
23 | createResampleProcessor: Function;
24 | createEndOfSpeechProcessor: Function;
25 | }
26 | interface Window {
27 | webkitURL: any;
28 | }
29 |
30 | declare var webkitAudioContext: {
31 | new (): AudioContext;
32 | }
33 |
34 | declare var webkitOfflineAudioContext: {
35 | new (numberOfChannels: number, length: number, sampleRate: number): OfflineAudioContext;
36 | }
37 |
38 | interface AudioContextConstructor {
39 | new(): AudioContext;
40 | }
41 |
42 | interface Window {
43 | AudioContext: AudioContextConstructor;
44 | }
45 |
46 | interface AudioContext {
47 | createMediaStreamSource(stream: MediaStream): MediaStreamAudioSourceNode;
48 | }
49 |
50 | interface MediaStreamAudioSourceNode extends AudioNode {
51 |
52 | }
53 |
54 | interface MediaStreamAudioDestinationNode extends AudioNode {
55 | stream: MediaStream;
56 | }
57 |
58 | interface AudioBuffer {
59 | copyFromChannel(destination: Float32Array, channelNumber: number, startInChannel?: number): void;
60 |
61 | copyToChannel(source: Float32Array, channelNumber: number, startInChannel?: number): void;
62 | }
63 |
64 | interface AudioNode {
65 | disconnect(destination: AudioNode): void;
66 | }
67 |
68 | interface AudioContext {
69 | suspend(): Promise;
70 | resume(): Promise;
71 | close(): Promise;
72 | createMediaStreamDestination(): MediaStreamAudioDestinationNode;
73 | }
74 |
--------------------------------------------------------------------------------
/es6/ApiAiConstants.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | export var ApiAiConstants;
18 | (function (ApiAiConstants) {
19 | let AVAILABLE_LANGUAGES;
20 | (function (AVAILABLE_LANGUAGES) {
21 | AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["EN"] = "en"] = "EN";
22 | AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["DE"] = "de"] = "DE";
23 | AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["ES"] = "es"] = "ES";
24 | AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["PT_BR"] = "pt-BR"] = "PT_BR";
25 | AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["ZH_HK"] = "zh-HK"] = "ZH_HK";
26 | AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["ZH_CN"] = "zh-CN"] = "ZH_CN";
27 | AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["ZH_TW"] = "zh-TW"] = "ZH_TW";
28 | AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["NL"] = "nl"] = "NL";
29 | AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["FR"] = "fr"] = "FR";
30 | AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["IT"] = "it"] = "IT";
31 | AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["JA"] = "ja"] = "JA";
32 | AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["KO"] = "ko"] = "KO";
33 | AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["PT"] = "pt"] = "PT";
34 | AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["RU"] = "ru"] = "RU";
35 | AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["UK"] = "uk"] = "UK";
36 | })(AVAILABLE_LANGUAGES = ApiAiConstants.AVAILABLE_LANGUAGES || (ApiAiConstants.AVAILABLE_LANGUAGES = {}));
37 | ApiAiConstants.VERSION = "2.0.0-beta.20";
38 | ApiAiConstants.DEFAULT_BASE_URL = "https://api.api.ai/v1/";
39 | ApiAiConstants.DEFAULT_API_VERSION = "20150910";
40 | ApiAiConstants.DEFAULT_CLIENT_LANG = AVAILABLE_LANGUAGES.EN;
41 | })(ApiAiConstants || (ApiAiConstants = {}));
42 |
--------------------------------------------------------------------------------
/webpack.config.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | "use strict";
18 |
19 | const webpack = require('webpack');
20 | const path = require('path');
21 |
22 | let libraryName = 'ApiAi';
23 | let libraryTarget = 'var';
24 | let outputFile = libraryName;
25 | let sourceMaps = true;
26 | let plugins = [];
27 | let entry = "ApiAiClient.ts";
28 |
29 | module.exports = function(env) {
30 | if (!env) {
31 | env = {};
32 | }
33 |
34 | // handle minification
35 | if (env && env.compress) {
36 | plugins.push(
37 | new webpack.LoaderOptionsPlugin({
38 | minimize: true,
39 | debug: false
40 | })
41 | );
42 |
43 | plugins.push(
44 | new webpack.optimize.UglifyJsPlugin({
45 | compress: {
46 | warnings: true,
47 | keep_fnames: true
48 | },
49 | mangle: {
50 | keep_fnames: true
51 | }
52 | })
53 | );
54 | outputFile += '.min';
55 | sourceMaps = false;
56 | } else {
57 | // outputFile += libraryName;
58 | }
59 |
60 | // handle custom target
61 | if (env && env.target) {
62 | libraryTarget = env.target;
63 | outputFile += '.' + libraryTarget;
64 | }
65 |
66 | outputFile += '.js';
67 |
68 | return {
69 | entry: [
70 | path.join(__dirname, 'ts', entry)
71 | ],
72 | devtool: sourceMaps ? 'source-map' : false,
73 | output: {
74 | path: path.join(__dirname, 'target'),
75 | publicPath: "/target/",
76 | filename: outputFile,
77 | library: libraryName,
78 | libraryTarget: libraryTarget
79 | },
80 |
81 | module: {
82 | loaders: [
83 | {test: /\.tsx?$/, loader: "awesome-typescript-loader"}
84 | ]
85 | },
86 | resolve: {
87 | extensions: ['.js', '.ts']
88 | },
89 | plugins: plugins
90 | };
91 | };
92 |
--------------------------------------------------------------------------------
/es6/Request/Request.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2017 Google Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | import { ApiAiRequestError } from "../Errors";
18 | import XhrRequest from "../XhrRequest";
19 | class Request {
20 | constructor(apiAiClient, options) {
21 | this.apiAiClient = apiAiClient;
22 | this.options = options;
23 | this.uri = this.apiAiClient.getApiBaseUrl() + "query?v=" + this.apiAiClient.getApiVersion();
24 | this.requestMethod = XhrRequest.Method.POST;
25 | this.headers = {
26 | Authorization: "Bearer " + this.apiAiClient.getAccessToken(),
27 | };
28 | this.options.lang = this.apiAiClient.getApiLang();
29 | this.options.sessionId = this.apiAiClient.getSessionId();
30 | }
31 | static handleSuccess(xhr) {
32 | return Promise.resolve(JSON.parse(xhr.responseText));
33 | }
34 | static handleError(xhr) {
35 | let error = new ApiAiRequestError(null);
36 | try {
37 | const serverResponse = JSON.parse(xhr.responseText);
38 | if (serverResponse.status && serverResponse.status.errorDetails) {
39 | error = new ApiAiRequestError(serverResponse.status.errorDetails, serverResponse.status.code);
40 | }
41 | else {
42 | error = new ApiAiRequestError(xhr.statusText, xhr.status);
43 | }
44 | }
45 | catch (e) {
46 | error = new ApiAiRequestError(xhr.statusText, xhr.status);
47 | }
48 | return Promise.reject(error);
49 | }
50 | perform(overrideOptions = null) {
51 | const options = overrideOptions ? overrideOptions : this.options;
52 | return XhrRequest.ajax(this.requestMethod, this.uri, options, this.headers)
53 | .then(Request.handleSuccess.bind(this))
54 | .catch(Request.handleError.bind(this));
55 | }
56 | }
57 | export default Request;
58 |
--------------------------------------------------------------------------------
/demo/simple/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Title
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
26 |
27 |
28 |