Skip to content

Commit 2f5c724

Browse files
committed
add context support for V2
1 parent c0d7660 commit 2f5c724

5 files changed

Lines changed: 61 additions & 60 deletions

File tree

README.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ npm install --save react-native-dialogflow
2424
2525
react-native link react-native-dialogflow
2626
react-native link react-native-speech-to-text-ios
27+
# for V2
2728
react-native link react-native-voice
29+
2830
```
2931

3032
### iOS: IMPORTANT xCode plist settings
@@ -46,9 +48,13 @@ Import Dialogflow:
4648
```javascript
4749
import Dialogflow from "react-native-dialogflow";
4850
```
51+
or for V2
52+
```javascript
53+
import { Dialogflow_V2 } from "react-native-dialogflow"
54+
```
4955

5056
### Configuration
51-
Set the `clientAccessToken` and the language in your constructor:
57+
Set the `accessToken` and the language in your constructor:
5258
```javascript
5359
constructor(props) {
5460
super(props);
@@ -60,6 +66,19 @@ Set the `clientAccessToken` and the language in your constructor:
6066

6167
```
6268

69+
For V2 you can set the OAUTH accessToken of the (auth setup)[https://dialogflow.com/docs/reference/v2-auth-setup]. In addition you have to set your projectId:
70+
```javascript
71+
constructor(props) {
72+
super(props);
73+
74+
Dialogflow_V2.setConfiguration(
75+
"4xxxxxxxe90xxxxxxxxc372", Dialogflow.LANG_GERMAN, 'testV2-3a5bc'
76+
);
77+
}
78+
79+
```
80+
81+
6382
### Listening
6483
Start listening with integrated speech recognition:
6584
```javascript
@@ -213,7 +232,7 @@ Dialogflow.setConfiguration("4xxxxxxxe90xxxxxxxxc372", Dialogflow.LANG_GERMAN);
213232
## Methods
214233
| name | platform | param1 | param2 | param3 | param4 |
215234
| --------------------- | -------- | --------- | --------- | --------- | --------- |
216-
| `setConfiguration` | both | clientAccessToken: String | languageTag: String | |
235+
| `setConfiguration` | both | accessToken: String | languageTag: String | (V2 only: projectId) |
217236
| `startListening` | both | resultCallback: (result: object)=>{} | errorCallback: (error: object)=>{} | |
218237
| `finishListening` | ios | | | |
219238
| `requestQuery` | both | query: String | resultCallback: (result: object)=>{} | errorCallback: (error: object)=>{} |
@@ -225,7 +244,7 @@ Dialogflow.setConfiguration("4xxxxxxxe90xxxxxxxxc372", Dialogflow.LANG_GERMAN);
225244
| `setContexts` | both | array || |
226245
| `resetContexts` | both | resultCallback: (result: object)=>{} | errorCallback: (error: object)=>{} | |
227246
| `setPermanentContexts`| both | array || |
228-
| `setEntities` | both | array || |
247+
| `setEntities` (V1 only)| both | array || |
229248

230249

231250
## Blogpost

example/App.android.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default class App extends Component {
2525
);
2626

2727
Dialogflow_V2.setConfiguration(
28-
"ya29.c.El92BV4aRCnGyaJ0VWKF0ewGNprivoW68g9lm0IrbsIi7T9m3QEL-JoPnfq3tWU07WhAjmIhD14z6X-1d8N_jzLz7o_8sB-c5OgAJE4cswJPoOzixZDTmCBvMbD901_0RA",
28+
"ya29.c.El93BfD5CZBWUrPJAyUdfWuHkmdzyjo0seL56z-reryvae4j8oVwLO38ARqrpj0Q5LxeUTndPLrA6GYPx4CyrlVmJRKvq_Dtwl9OO0zxiWTVK7boCV-aCVJVHbaFSGstgA",
2929
Dialogflow_V2.LANG_GERMAN,
3030
'testv2-3b5ca'
3131
);
@@ -41,16 +41,9 @@ export default class App extends Component {
4141
}];
4242

4343

44-
const contexts_V2 = [{
45-
"name": "deals",
46-
"lifespanCount": 1,
47-
"parameters": {
48-
"name": "Sam"
49-
}
50-
}];
5144

5245
Dialogflow.setContexts(contexts);
53-
//Dialogflow_V2.setContexts(contexts_V2);
46+
Dialogflow_V2.setContexts(contexts);
5447

5548

5649
const permanentContexts = [{
@@ -60,15 +53,9 @@ export default class App extends Component {
6053
}
6154
}];
6255

63-
const permanentContexts_V2 = [{
64-
"name": "config",
65-
"parameters": {
66-
"access_token": "42 yo 42 tiny rick"
67-
}
68-
}];
6956

7057
Dialogflow.setPermanentContexts(permanentContexts);
71-
//Dialogflow_V2.setPermanentContexts(permanentContexts_V2);
58+
Dialogflow_V2.setPermanentContexts(permanentContexts);
7259

7360

7461
const entities = [{

example/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Example App
2+
3+
How to run:
4+
```
5+
git clone https://github.com/innFactory/react-native-dialogflow.git
6+
cd react-native-dialogflow/example
7+
npm install
8+
react-native run-android
9+
```
10+
11+
<img src="screenshot.png" alt="Screenshot"/>

example/screenshot.png

248 KB
Loading

js/Dialogflow_V2.js

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { NativeModules, NativeAppEventEmitter } from 'react-native';
44
import requestEvent from './ResetContextsRequest';
55
import ResetContextsRequest from './ResetContextsRequest';
66
export const DEFAULT_BASE_URL = "https://dialogflow.googleapis.com/v2beta1/projects/";
7-
export const lol = "testv2-3b5ca/agent/sessions/0:detectIntent";
87

98
export class Dialogflow_V2 {
109

@@ -21,16 +20,20 @@ export class Dialogflow_V2 {
2120
}
2221

2322
setContexts(contexts) {
24-
this.contexts = contexts;
23+
var array = contexts;
2524

26-
contexts.forEach(c => {
27-
this.createContext(c);
25+
array.forEach((c, i, a) => {
26+
a[i] = this.normalizeContext(c);
2827
})
28+
29+
this.contexts = array;
2930
}
3031

3132
setPermanentContexts(contexts) {
3233
// set lifespan to 1 if it's not set
3334
contexts.forEach((c, i, a) => {
35+
a[i] = this.normalizeContext(c);
36+
3437
if (!c.lifespanCount) {
3538
a[i] = { ...c, lifespanCount: 1 };
3639
}
@@ -39,22 +42,34 @@ export class Dialogflow_V2 {
3942
this.permanentContexts = contexts;
4043
}
4144

45+
normalizeContext(context) {
46+
// rename property lifespan to lifespanCount
47+
if (context.lifespan) {
48+
context.lifespanCount = context.lifespan;
49+
delete context.lifespan;
50+
}
51+
52+
// add context name path: projects/<Project ID>/agent/sessions/<Session ID>/contexts/<Context ID>
53+
// https://dialogflow.com/docs/reference/api-v2/rest/v2beta1/projects.agent.sessions.contexts#Context
54+
if (!context.name.startsWith("projects/")) {
55+
context.name = "projects/" + this.projectId + "/agent/sessions/" + this.sessionId + "/contexts/" + context.name;
56+
}
57+
58+
return context;
59+
}
60+
61+
/*
4262
setEntities(entities) {
4363
this.entities = entities;
4464
}
65+
*/
4566

4667
onAudioLevel(callback) {
4768

4869
}
4970

5071
requestEvent = async (eventName, eventParameters, onResult, onError) => {
5172

52-
/*
53-
this.permanentContexts.forEach(c => {
54-
this.createContext(c);
55-
})
56-
*/
57-
5873
const data = {
5974
"queryParams": {
6075
"contexts": this.mergeContexts(this.contexts, this.permanentContexts),
@@ -90,12 +105,6 @@ export class Dialogflow_V2 {
90105

91106
requestQuery = async (query, onResult, onError) => {
92107

93-
/*
94-
this.permanentContexts.forEach(c => {
95-
this.createContext(c);
96-
})
97-
*/
98-
99108
const data = {
100109
"queryParams": {
101110
"contexts": this.mergeContexts(this.contexts, this.permanentContexts),
@@ -127,31 +136,6 @@ export class Dialogflow_V2 {
127136
.catch(onError);
128137
};
129138

130-
/**
131-
* https://dialogflow.com/docs/reference/api-v2/rest/v2beta1/projects.agent.sessions.contexts
132-
*
133-
* @param {*} context
134-
*/
135-
createContext = async (context) => {
136-
137-
const data = context;
138-
139-
try {
140-
const response = await fetch(DEFAULT_BASE_URL + this.projectId + "/agent/sessions/" + this.sessionId + "/contexts:create", {
141-
method: "POST",
142-
headers: {
143-
'Content-Type': 'application/json',
144-
'Authorization': 'Bearer ' + this.accessToken,
145-
'charset': "utf-8"
146-
},
147-
body: JSON.stringify(data)
148-
});
149-
console.log(response);
150-
} catch (error) {
151-
console.log(error);
152-
}
153-
};
154-
155139

156140
mergeContexts(context1, context2) {
157141
if (!context1) {

0 commit comments

Comments
 (0)