Skip to content

Commit fab4bac

Browse files
committed
remove api-ai js dependencies
1 parent 8cdaff3 commit fab4bac

4 files changed

Lines changed: 194 additions & 275 deletions

File tree

index.android.js

Lines changed: 21 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -1,177 +1,38 @@
11
'use strict';
22

3-
import { NativeModules, NativeAppEventEmitter } from 'react-native';
4-
import requestEvent from './js/ResetContextsRequest';
5-
import ResetContextsRequest from './js/ResetContextsRequest';
3+
import { NativeAppEventEmitter } from 'react-native';
64
import Voice from './js/RCTVoice';
7-
export const DEFAULT_BASE_URL = "https://api.api.ai/v1/";
8-
export const DEFAULT_API_VERSION = "20150910";
5+
import { Dialogflow } from './js/Dialogflow';
96

10-
class Dialogflow {
117

12-
setConfiguration(accessToken, languageTag) {
13-
this.accessToken = accessToken;
14-
this.languageTag = languageTag;
15-
this.sessionId = this.sessionId ? this.sessionId : guid();
8+
var dialogflow = new Dialogflow();
169

10+
dialogflow.setConfiguration = function (accessToken, languageTag) {
11+
dialogflow.accessToken = accessToken;
12+
dialogflow.languageTag = languageTag;
13+
dialogflow.sessionId = dialogflow.sessionId ? dialogflow.sessionId : dialogflow.guid();
1714

15+
Voice.onSpeechStart = () => (c) => dialogflow.onListeningStarted(c);
16+
Voice.onSpeechEnd = () => (c) => dialogflow.onListeningFinished(c);
17+
}
1818

19-
Voice.onSpeechStart = () => (c) => this.onListeningStarted(c);
20-
Voice.onSpeechEnd = () => (c) => this.onListeningFinished(c);
21-
}
22-
23-
24-
startListening(onResult, onError) {
25-
26-
this.subscription = NativeAppEventEmitter.addListener(
27-
'onSpeechResults',
28-
(result) => {
29-
if (result.value) {
30-
console.log(result.value);
31-
this.requestQuery(result.value[0], onResult, onError);
32-
}
33-
34-
}
35-
);
36-
37-
Voice.start(this.languageTag);
38-
}
39-
40-
finishListening() {
41-
Voice.stopSpeech();
42-
}
43-
44-
onListeningStarted(callback) {
45-
callback();
46-
}
47-
48-
onListeningCanceled(callback) {
49-
callback();
50-
}
51-
52-
onListeningFinished(callback) {
53-
callback();
54-
}
55-
56-
setContexts(contexts) {
57-
this.contexts = contexts;
58-
}
19+
dialogflow.startListening = function (onResult, onError) {
5920

60-
setPermanentContexts(contexts) {
61-
// set lifespan to 1 if it's not set
62-
contexts.forEach((c, i, a) => {
63-
if (!c.lifespan) {
64-
a[i] = { ...c, lifespan: 1 };
21+
dialogflow.subscription = NativeAppEventEmitter.addListener(
22+
'onSpeechResults',
23+
(result) => {
24+
if (result.value) {
25+
dialogflow.requestQuery(result.value[0], onResult, onError);
6526
}
66-
});
67-
68-
this.permanentContexts = contexts;
69-
}
7027

71-
setEntities(entities) {
72-
this.entities = entities;
73-
}
74-
75-
onAudioLevel(callback) {
76-
77-
}
78-
79-
requestEvent = async (eventName, eventData, onResult, onError) => {
80-
81-
const data = {
82-
"event": {
83-
"name": eventName,
84-
"data": {
85-
...eventData
86-
}
87-
},
88-
'lang': this.languageTag,
89-
"sessionId": this.sessionId
90-
};
91-
92-
fetch(DEFAULT_BASE_URL + "query?v=" + DEFAULT_API_VERSION, {
93-
method: "POST",
94-
headers: {
95-
'Content-Type': 'application/json',
96-
'Authorization': 'Bearer ' + this.accessToken,
97-
'charset': "utf-8"
98-
},
99-
body: JSON.stringify(data)
100-
})
101-
.then(function (response) {
102-
var json = response.json().then(onResult)
103-
})
104-
.catch(onError);
105-
};
106-
107-
requestQuery = async (query, onResult, onError) => {
108-
109-
const data = {
110-
"contexts": this.mergeContexts(this.contexts, this.permanentContexts),
111-
"query": query,
112-
'lang': this.languageTag,
113-
"sessionId": this.sessionId.toString()
114-
};
115-
116-
fetch(DEFAULT_BASE_URL + "query?v=" + DEFAULT_API_VERSION, {
117-
method: "POST",
118-
headers: {
119-
'Content-Type': 'application/json',
120-
'Authorization': 'Bearer ' + this.accessToken,
121-
'charset': "utf-8"
122-
},
123-
body: JSON.stringify(data)
124-
})
125-
.then(function (response) {
126-
var json = response.json().then(onResult)
127-
})
128-
.catch(onError);
129-
};
130-
131-
mergeContexts(context1, context2) {
132-
if (!context1) {
133-
return context2;
134-
} else if (!context2) {
135-
return context1;
136-
} else {
137-
return [...context1, ...context2];
13828
}
139-
}
29+
);
14030

141-
resetContexts(onResult, onError) {
142-
let request = new ResetContextsRequest(this.client.getAccessToken(), this.client.getSessionId(), null);
143-
request.perform().then(res => onResult(res)).catch(err => onError(err));
144-
};
145-
146-
147-
LANG_CHINESE_CHINA = "zh-CN";
148-
LANG_CHINESE_HONGKONG = "zh-HK";
149-
LANG_CHINESE_TAIWAN = "zh-TW";
150-
LANG_DUTCH = "nl";
151-
LANG_ENGLISH = "en";
152-
LANG_ENGLISH_GB = "en-GB";
153-
LANG_ENGLISH_US = "en-US";
154-
LANG_FRENCH = "fr";
155-
LANG_GERMAN = "de";
156-
LANG_ITALIAN = "it";
157-
LANG_JAPANESE = "ja";
158-
LANG_KOREAN = "ko";
159-
LANG_PORTUGUESE = "pt";
160-
LANG_PORTUGUESE_BRAZIL = "pt-BR";
161-
LANG_RUSSIAN = "ru";
162-
LANG_SPANISH = "es";
163-
LANG_UKRAINIAN = "uk";
31+
Voice.start(dialogflow.languageTag);
16432
}
16533

166-
167-
/**
168-
* generates new random UUID
169-
* @returns {string}
170-
*/
171-
function guid() {
172-
const s4 = () => Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
173-
return s4() + s4() + "-" + s4() + "-" + s4() + "-" +
174-
s4() + "-" + s4() + s4() + s4();
34+
dialogflow.finishListening = function () {
35+
Voice.stop();
17536
}
17637

177-
export default new Dialogflow();
38+
export default dialogflow;

index.ios.js

Lines changed: 22 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,42 @@
11

22
import { NativeModules, NativeAppEventEmitter } from 'react-native';
3-
import { ApiAiClient } from 'api-ai-javascript';
43
import ResetContextsRequest from './js/ResetContextsRequest';
54

5+
import { Dialogflow } from './js/Dialogflow';
66
var SpeechToText = NativeModules.RNSpeechToTextIos;
77

8-
class Dialogflow {
8+
var dialogflow = new Dialogflow();
99

10+
dialogflow.setConfiguration = function (accessToken, languageTag) {
11+
dialogflow.language = languageTag;
12+
dialogflow.accessToken = accessToken;
13+
dialogflow.languageTag = languageTag;
14+
}
1015

11-
setConfiguration(clientAccessToken, languageTag) {
12-
this.language = languageTag;
13-
this.client = new ApiAiClient({ accessToken: clientAccessToken, apiLang: languageTag });
14-
}
15-
16-
onListeningStarted(callback) {
17-
18-
}
19-
20-
onListeningCanceled(callback) {
21-
22-
}
23-
24-
onListeningFinished(callback) {
25-
26-
}
27-
28-
startListening(onResult, onError) {
29-
30-
this.subscription = NativeAppEventEmitter.addListener(
31-
'SpeechToText',
32-
(result) => {
3316

34-
console.log(result);
17+
dialogflow.startListening = function (onResult, onError) {
3518

36-
if (result.error) {
37-
onError(result.error);
38-
} else {
39-
if (result.isFinal) {
40-
this.requestQuery(result.bestTranscription.formattedString, onResult, onError);
41-
}
19+
this.subscription = NativeAppEventEmitter.addListener(
20+
'SpeechToText',
21+
(result) => {
4222

23+
if (result.error) {
24+
onError(result.error);
25+
} else {
26+
if (result.isFinal) {
27+
this.requestQuery(result.bestTranscription.formattedString, onResult, onError);
4328
}
4429

4530
}
46-
);
47-
48-
SpeechToText.startRecognition(this.language);
49-
}
50-
51-
finishListening() {
52-
SpeechToText.finishRecognition();
53-
}
54-
55-
setContexts(contexts) {
56-
this.contexts = contexts;
57-
}
5831

59-
setPermanentContexts(contexts) {
60-
// set lifespan to 1 if it's not set
61-
contexts.forEach((c, i, a) => {
62-
if (!c.lifespan) {
63-
a[i] = { ...c, lifespan: 1 };
64-
}
65-
});
66-
67-
this.permanentContexts = contexts;
68-
}
69-
70-
resetContexts(onResult, onError) {
71-
let request = new ResetContextsRequest(this.client.getAccessToken(), this.client.getSessionId(), null);
72-
request.perform().then(res => onResult(res)).catch(err => onError(err));
73-
};
74-
75-
setEntities(entities) {
76-
this.entities = entities;
77-
}
78-
79-
requestQuery(query, onResult, onError) {
80-
if (this.contexts || this.permanentContexts || this.entities) {
81-
this.client.textRequest(query, {
82-
contexts: this.mergeContexts(this.contexts, this.permanentContexts),
83-
entities: this.entities
84-
}
85-
).then(res => onResult(res)).catch(err => onError(err));
86-
this.contexts = null;
87-
this.entities = null;
88-
} else {
89-
this.client.textRequest(query).then(res => onResult(res)).catch(err => onError(err));
9032
}
91-
}
33+
);
9234

93-
requestEvent(eventName, eventData = {}, onResult, onError) {
94-
this.client.eventRequest(eventName, eventData, {}).then(res => onResult(res)).catch(err => onError(err));
95-
}
96-
97-
onAudioLevel(callback) {
98-
99-
}
100-
101-
mergeContexts(context1, context2) {
102-
if (!context1) {
103-
return context2;
104-
} else if (!context2) {
105-
return context1;
106-
} else {
107-
return [...context1, ...context2];
108-
}
109-
}
35+
SpeechToText.startRecognition(this.language);
36+
}
11037

111-
LANG_CHINESE_CHINA = "zh-CN";
112-
LANG_CHINESE_HONGKONG = "zh-HK";
113-
LANG_CHINESE_TAIWAN = "zh-TW";
114-
LANG_DUTCH = "nl";
115-
LANG_ENGLISH = "en";
116-
LANG_ENGLISH_GB = "en-GB";
117-
LANG_ENGLISH_US = "en-US";
118-
LANG_FRENCH = "fr";
119-
LANG_GERMAN = "de";
120-
LANG_ITALIAN = "it";
121-
LANG_JAPANESE = "ja";
122-
LANG_KOREAN = "ko";
123-
LANG_PORTUGUESE = "pt";
124-
LANG_PORTUGUESE_BRAZIL = "pt-BR";
125-
LANG_RUSSIAN = "ru";
126-
LANG_SPANISH = "es";
127-
LANG_UKRAINIAN = "uk";
38+
dialogflow.finishListening = function () {
39+
SpeechToText.finishRecognition();
12840
}
12941

130-
export default new Dialogflow();
42+
export default dialogflow;

0 commit comments

Comments
 (0)