@@ -4,7 +4,6 @@ import { NativeModules, NativeAppEventEmitter } from 'react-native';
44import requestEvent from './ResetContextsRequest' ;
55import ResetContextsRequest from './ResetContextsRequest' ;
66export const DEFAULT_BASE_URL = "https://dialogflow.googleapis.com/v2beta1/projects/" ;
7- export const lol = "testv2-3b5ca/agent/sessions/0:detectIntent" ;
87
98export 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