Coder Social home page Coder Social logo

Comments (7)

owenbrown avatar owenbrown commented on May 29, 2024 20

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.

lordrubenbp avatar lordrubenbp commented on May 29, 2024 5

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.

mikozh avatar mikozh commented on May 29, 2024

Have the same problem :( Can not figure out how to pass parameters

from python-dialogflow.

atimzhao avatar atimzhao commented on May 29, 2024

Having the same issue! Please help

from python-dialogflow.

jpi-car avatar jpi-car commented on May 29, 2024

Also having this problem; it's very unclear how parameters are supposed to be formatted.

from python-dialogflow.

lordrubenbp avatar lordrubenbp commented on May 29, 2024

same problem here!, any know where i can found context id??

from python-dialogflow.

saiffero avatar saiffero commented on May 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.