0

I'm trying to create Google DialogFlow CX intent using python google-cloud-dialogflow-cxl library. Here is modified snippet code that took from the official documentation to create an intent. However, the did not work as expected. Once i called sample_create_intent() method it gives me an error says "<coroutine object sample_create_intent at 0x7fde18379040>" and the intent is not created. So, can anyone please help me to fix this code.

Thank You

import google.auth
from google.cloud import dialogflowcx_v3 as dialogflow


project_id = '{project_id}'
location_id = 'us-central1'
agent_id = 'agent_id
key_file = "/content/sample_data/service_account_key.json"

# Load credentials from the JSON key file
cred, project = google.auth.load_credentials_from_file(key_file)

async def sample_create_intent():
    # Create a client
    client = dialogflow.IntentsAsyncClient(credentials=cred)

    # Initialize request argument(s)
    intent = dialogflow.Intent()
    intent.display_name = "Test"

    request = dialogflow.CreateIntentRequest(
        parent="projects/{my_project_id}/locations/us-central1/agents/{my_agen_id}/",
        intent=intent,
    )

    # Make the request
    response = await client.create_intent(request=request)

    # Handle the response
    print(response)

sample_create_intent()
BinRip
  • 43
  • 2

1 Answers1

0

Try this code

import asyncio

import google.auth
from google.cloud import dialogflowcx_v3 as dialogflow
from google.api_core.client_options import ClientOptions

project_id = 'your project id'
location_id = 'your location id'
agent_id = 'your agent id'
key_file = "path to json key file"

# Load credentials from the JSON key file
cred, project = google.auth.load_credentials_from_file(key_file)


async def sample_create_intent():
    # Specify api-endpoint according to your agent location
    if location_id != "global":
       option = ClientOptions(api_endpoint=f"{location_id}-dialogflow.googleapis.com")
    else:
       option = ClientOptions(api_endpoint=f"dialogflow.googleapis.com")

    # Create a client
    client = dialogflow.IntentsAsyncClient(credentials=cred, client_options=option)

    # Initialize request argument(s)
    intent = dialogflow.Intent()
    intent.display_name = "Test"

    request = dialogflow.CreateIntentRequest(
       parent=f"projects/{project_id}/locations/{location_id}/agents/{agent_id}",
       intent=intent,
    )

    # Make the request
    response = await client.create_intent(request=request)

    # Handle the response
    print(response)

if __name__ == '__main__':
    asyncio.run(sample_create_intent())

You need to specify the api-endpoint according to your agent location.

It's look like your parent parameter at CreateIntentRequest was wrong. Don't give "/" at the end of your parent parameter.

To run a async function, you need to run in asyncio event loop. For example you can run using asyncio.run()

jon_17z
  • 91
  • 7