Skip to content

Commit c0d7660

Browse files
committed
basic V2 support for reqest query and event
1 parent ecdda01 commit c0d7660

4 files changed

Lines changed: 341 additions & 4 deletions

File tree

example/App.android.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
Button
88
} from 'react-native';
99

10-
import Dialogflow from "react-native-dialogflow"
10+
import Dialogflow, { Dialogflow_V2 } from "react-native-dialogflow"
1111

1212
export default class App extends Component {
1313
constructor(props) {
@@ -24,6 +24,12 @@ export default class App extends Component {
2424
"57b6ce865e6e4b138a74a88cfd8bc526", Dialogflow.LANG_GERMAN
2525
);
2626

27+
Dialogflow_V2.setConfiguration(
28+
"ya29.c.El92BV4aRCnGyaJ0VWKF0ewGNprivoW68g9lm0IrbsIi7T9m3QEL-JoPnfq3tWU07WhAjmIhD14z6X-1d8N_jzLz7o_8sB-c5OgAJE4cswJPoOzixZDTmCBvMbD901_0RA",
29+
Dialogflow_V2.LANG_GERMAN,
30+
'testv2-3b5ca'
31+
);
32+
2733

2834

2935
const contexts = [{
@@ -34,7 +40,17 @@ export default class App extends Component {
3440
}
3541
}];
3642

43+
44+
const contexts_V2 = [{
45+
"name": "deals",
46+
"lifespanCount": 1,
47+
"parameters": {
48+
"name": "Sam"
49+
}
50+
}];
51+
3752
Dialogflow.setContexts(contexts);
53+
//Dialogflow_V2.setContexts(contexts_V2);
3854

3955

4056
const permanentContexts = [{
@@ -44,7 +60,15 @@ export default class App extends Component {
4460
}
4561
}];
4662

63+
const permanentContexts_V2 = [{
64+
"name": "config",
65+
"parameters": {
66+
"access_token": "42 yo 42 tiny rick"
67+
}
68+
}];
69+
4770
Dialogflow.setPermanentContexts(permanentContexts);
71+
//Dialogflow_V2.setPermanentContexts(permanentContexts_V2);
4872

4973

5074
const entities = [{
@@ -62,11 +86,13 @@ export default class App extends Component {
6286

6387

6488
Dialogflow.setEntities(entities);
89+
Dialogflow_V2.setEntities(entities);
6590
}
6691

6792

6893
render() {
6994
Dialogflow.requestEvent("WELCOME", null, r => console.log(r), e => console.log(e));
95+
Dialogflow_V2.requestEvent("WELCOME", null, r => console.log(r), e => console.log(e));
7096

7197

7298
return (
@@ -81,6 +107,7 @@ export default class App extends Component {
81107
<Button title="Start Listening" onPress={() => {
82108

83109

110+
// V1
84111
Dialogflow.onListeningStarted(() => {
85112
this.setState({ listeningState: "started" });
86113
});
@@ -103,7 +130,32 @@ export default class App extends Component {
103130
}, error => {
104131
this.setState({ result: JSON.stringify(error) });
105132
});
133+
}} />
106134

135+
<Button color="orange" title="Start Listening V2" onPress={() => {
136+
// V2
137+
Dialogflow_V2.onListeningStarted(() => {
138+
this.setState({ listeningState: "started" });
139+
});
140+
141+
Dialogflow_V2.onListeningCanceled(() => {
142+
this.setState({ listeningState: "canceled" });
143+
});
144+
145+
Dialogflow_V2.onListeningFinished(() => {
146+
this.setState({ listeningState: "finished" });
147+
});
148+
149+
Dialogflow_V2.onAudioLevel(level => {
150+
this.setState({ audioLevel: level });
151+
});
152+
153+
Dialogflow_V2.startListening(result => {
154+
console.log(result);
155+
this.setState({ result: JSON.stringify(result) });
156+
}, error => {
157+
this.setState({ result: JSON.stringify(error) });
158+
});
107159
}} />
108160
</View>
109161
</View>

index.android.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import { NativeAppEventEmitter } from 'react-native';
44
import Voice from './js/RCTVoice';
55
import { Dialogflow } from './js/Dialogflow';
6+
import { Dialogflow_V2 } from './js/Dialogflow_V2';
67

7-
8+
/**
9+
* DIALOGFLOW V1
10+
*/
811
var dialogflow = new Dialogflow();
912

1013
dialogflow.setConfiguration = function (accessToken, languageTag) {
@@ -36,3 +39,40 @@ dialogflow.finishListening = function () {
3639
}
3740

3841
export default dialogflow;
42+
43+
44+
/**
45+
* DIALOGFLOW V2
46+
*/
47+
var dialogflow2 = new Dialogflow_V2();
48+
49+
dialogflow2.setConfiguration = function (accessToken, languageTag, projectId) {
50+
dialogflow2.accessToken = accessToken;
51+
dialogflow2.languageTag = languageTag;
52+
dialogflow2.projectId = projectId;
53+
dialogflow2.sessionId = dialogflow2.sessionId ? dialogflow2.sessionId : dialogflow2.guid();
54+
55+
Voice.onSpeechStart = () => (c) => dialogflow2.onListeningStarted(c);
56+
Voice.onSpeechEnd = () => (c) => dialogflow2.onListeningFinished(c);
57+
}
58+
59+
dialogflow2.startListening = function (onResult, onError) {
60+
61+
dialogflow2.subscription = NativeAppEventEmitter.addListener(
62+
'onSpeechResults',
63+
(result) => {
64+
if (result.value) {
65+
dialogflow2.requestQuery(result.value[0], onResult, onError);
66+
}
67+
68+
}
69+
);
70+
71+
Voice.start(dialogflow2.languageTag);
72+
}
73+
74+
dialogflow2.finishListening = function () {
75+
Voice.stop();
76+
}
77+
78+
export { dialogflow2 as Dialogflow_V2 }

index.ios.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11

22
import { NativeModules, NativeAppEventEmitter } from 'react-native';
33
import ResetContextsRequest from './js/ResetContextsRequest';
4-
54
import { Dialogflow } from './js/Dialogflow';
5+
import { Dialogflow_V2 } from './js/Dialogflow_V2';
6+
67
var SpeechToText = NativeModules.RNSpeechToTextIos;
78

9+
/**
10+
* DIALOGFLOW V1
11+
*/
812
var dialogflow = new Dialogflow();
913

1014
dialogflow.setConfiguration = function (accessToken, languageTag) {
11-
dialogflow.language = languageTag;
1215
dialogflow.accessToken = accessToken;
1316
dialogflow.languageTag = languageTag;
17+
dialogflow.sessionId = dialogflow.sessionId ? dialogflow.sessionId : dialogflow.guid();
1418
}
1519

1620

@@ -40,3 +44,44 @@ dialogflow.finishListening = function () {
4044
}
4145

4246
export default dialogflow;
47+
48+
49+
50+
/**
51+
* DIALOGFLOW V2
52+
*/
53+
var dialogflow2 = new Dialogflow_V2();
54+
55+
dialogflow2.setConfiguration = function (accessToken, languageTag, projectId) {
56+
dialogflow2.accessToken = accessToken;
57+
dialogflow2.languageTag = languageTag;
58+
dialogflow2.projectId = projectId;
59+
dialogflow2.sessionId = dialogflow2.sessionId ? dialogflow2.sessionId : dialogflow2.guid();
60+
}
61+
62+
dialogflow2.startListening = function (onResult, onError) {
63+
64+
dialogflow2.subscription = NativeAppEventEmitter.addListener(
65+
'SpeechToText',
66+
(result) => {
67+
68+
if (result.error) {
69+
onError(result.error);
70+
} else {
71+
if (result.isFinal) {
72+
dialogflow2.requestQuery(result.bestTranscription.formattedString, onResult, onError);
73+
}
74+
75+
}
76+
77+
}
78+
);
79+
80+
SpeechToText.startRecognition(dialogflow2.language);
81+
}
82+
83+
dialogflow2.finishListening = function () {
84+
SpeechToText.finishRecognition();
85+
}
86+
87+
export { dialogflow2 as Dialogflow_V2 }

0 commit comments

Comments
 (0)