-
Notifications
You must be signed in to change notification settings - Fork 64
/
index.js
99 lines (75 loc) · 2.57 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict';
import { NativeAppEventEmitter } from 'react-native';
import { Dialogflow } from './js/Dialogflow';
import { Dialogflow_V2 } from './js/Dialogflow_V2';
import Voice from './js/RCTVoice';
/**
* DIALOGFLOW V1
*/
var dialogflow = new Dialogflow();
dialogflow.setConfiguration = function (accessToken, languageTag) {
dialogflow.accessToken = accessToken;
dialogflow.languageTag = languageTag;
dialogflow.sessionId = dialogflow.sessionId ? dialogflow.sessionId : dialogflow.guid();
Voice.onSpeechStart = () => (c) => dialogflow.onListeningStarted(c);
Voice.onSpeechEnd = () => (c) => dialogflow.onListeningFinished(c);
}
dialogflow.startListening = function (onResult, onError) {
dialogflow.subscription = NativeAppEventEmitter.addListener(
'onSpeechResults',
(result) => {
if (result.value) {
dialogflow.requestQuery(result.value[0], onResult, onError);
}
}
);
Voice.start(dialogflow.languageTag);
}
dialogflow.finishListening = function () {
Voice.stop();
}
export default dialogflow;
/**
* DIALOGFLOW V2
*/
var dialogflow2 = new Dialogflow_V2();
dialogflow2.setConfiguration = async function (clientEmail, privateKey, languageTag, projectId) {
dialogflow2.accessToken = await dialogflow2.generateAccessToken(clientEmail, privateKey);
dialogflow2.languageTag = languageTag;
dialogflow2.projectId = projectId;
dialogflow2.sessionId = dialogflow2.sessionId ? dialogflow2.sessionId : dialogflow2.guid();
// set listeners
Voice.onSpeechStart = (c) => {
dialogflow2.speechResult = null;
if (dialogflow2.onListeningStarted) {
dialogflow2.onListeningStarted(c);
}
}
Voice.onSpeechEnd = (c) => {
if (dialogflow2.speechResult) {
dialogflow2.requestQuery(dialogflow2.speechResult[0], dialogflow2.onResult, dialogflow2.onError);
}
if (dialogflow2.onListeningFinished) {
dialogflow2.onListeningFinished(c);
}
}
Voice.onSpeechVolumeChanged = (c) => {
if (dialogflow2.onAudioLevel) {
dialogflow2.onAudioLevel(c);
}
}
Voice.onSpeechResults = (result) => {
if (result.value) {
dialogflow2.speechResult = result.value;
}
}
}
dialogflow2.startListening = function (onResult, onError) {
dialogflow2.onResult = onResult;
dialogflow2.onError = onError;
Voice.start(dialogflow2.languageTag);
}
dialogflow2.finishListening = function () {
Voice.stop();
}
export { dialogflow2 as Dialogflow_V2 };