Comments (7)
Okay, I finally got this to work.
The key was learning how Struct worked, which is explained here - https://developers.google.com/protocol-buffers/docs/reference/python-generated
Doing this protocol buffer tutorial is helpful -
https://developers.google.com/protocol-buffers/docs/pythontutorial
Really the SDK should abstract this away.
import dialogflow
import os
GOOGLE_AUTHENTICATION_FILE_NAME = "file.json"
current_directory = os.path.dirname(os.path.realpath(__file__))
path = os.path.join(current_directory, GOOGLE_AUTHENTICATION_FILE_NAME)
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = path
GOOGLE_PROJECT_ID = "exampleproject"
session_id = "abc-123"
context_short_name = "does_not_matter"
context_name = "projects/" + GOOGLE_PROJECT_ID + "/agent/sessions/" + session_id + "/contexts/" + \
context_short_name.lower()
parameters = dialogflow.types.struct_pb2.Struct()
parameters["foo"] = "bar"
context_1 = dialogflow.types.context_pb2.Context(
name=context_name,
lifespan_count=2,
parameters=parameters
)
query_params_1 = {"contexts": [context_1]}
language_code = 'en'
def detect_intent_with_parameters(project_id, session_id, query_params, language_code):
"""Returns the result of detect intent with texts as inputs.
Using the same `session_id` between requests allows continuation
of the conversaion."""
session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, session_id)
print('Session path: {}\n'.format(session))
text = "this is as test"
text_input = dialogflow.types.TextInput(
text=text, language_code=language_code)
query_input = dialogflow.types.QueryInput(text=text_input)
response = session_client.detect_intent(
session=session, query_input=query_input,
query_params=query_params
)
print('=' * 20)
print('Query text: {}'.format(response.query_result.query_text))
print('Detected intent: {} (confidence: {})\n'.format(
response.query_result.intent.display_name,
response.query_result.intent_detection_confidence))
print('Fulfillment text: {}\n'.format(
response.query_result.fulfillment_text))
detect_intent_with_parameters(
project_id=GOOGLE_PROJECT_ID,
session_id=session_id,
query_params=query_params_1,
language_code=language_code
)
from python-dialogflow.
After a lot of work I have managed to solve this problem, here is a sample code of how to use it
import json
import uuid
import os
from google.protobuf.json_format import MessageToJson
from google.protobuf.struct_pb2 import Struct, Value
import dialogflow
context=dialogflow.types.context_pb2.Context(name='projects/blabla-31936/agent/sessions/637996ed-90a8-4900-83b0-b5f1f9a9xxxx/contexts/prueba_dialog_context')
credentials_file = os.path.expanduser('cloud_speech.json')
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credentials_file
project_id = format(json.load(open(credentials_file))['project_id'])
session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, str(uuid.uuid4()))
print('Session path: {}\n'.format(session))
text_input = dialogflow.types.TextInput(text="soy Ruben Baena Perez", language_code="es-ES")
query_input = dialogflow.types.QueryInput(text=text_input)
query_parameter = dialogflow.types.QueryParameters(contexts=[context])
response = session_client.detect_intent(session=session, query_input=query_input,query_params=query_parameter)
print(response.query_result.fulfillment_text)
parameters=response.query_result.parameters
print(parameters)
parameters_json = json.loads(MessageToJson(parameters))
print(parameters_json)
from python-dialogflow.
Have the same problem :( Can not figure out how to pass parameters
from python-dialogflow.
Having the same issue! Please help
from python-dialogflow.
Also having this problem; it's very unclear how parameters are supposed to be formatted.
from python-dialogflow.
same problem here!, any know where i can found context id??
from python-dialogflow.
how do i get this context data which is passed as output context can any one help ? (In python only)
from python-dialogflow.
Related Issues (20)
- Exporting Agent Bytes Then Fails to Import HOT 13
- Migrate to main branch
- samples.snippets.participant_management_test: test_analyze_content_text failed HOT 11
- samples.snippets.participant_management_test: test_analyze_content_text failed HOT 1
- Dependency Dashboard
- samples.snippets.create_document_test: test_create_document failed HOT 5
- samples.snippets.create_knowledge_base_test: test_create_knowledge_base failed HOT 5
- participant_management.py test failed.
- Segmentation fault with multiprocessing HOT 2
- Increase code coverage from 99% to 100% HOT 1
- No DesignTimeAgent found for project `python-docs-samples-*` HOT 2
- AttributeError: module 'google.protobuf.descriptor' has no attribute '_internal_create_key' HOT 9
- Resolve sphinx docs warnings and enable warnings as errors HOT 1
- seminar HOT 1
- Warning: a recent release failed
- AttributeError: module 'google.cloud.dialogflow_v2' has no attribute 'Entity' HOT 1
- Add sample code for StreamingAnalyzeContent with audio live transcription HOT 1
- Add Code Snippet for Pulling Session ID from WebhookRequests HOT 1
- AttributeError: 'dict' object has no attribute 'index' HOT 2
- unable to import name "common_pb2" from "google.cloud.dialogflow_v2.types" HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-dialogflow.