Sultan Ahmed
Sultan Ahmed

Reputation: 2220

Python Code to Create follow up intent in Dialogflow

I have created a welcome intent and I want to create a follow-up intent of welcome-intent. I can create intent in Dialogflow using the following code:

            intents_client = dialogflow.IntentsClient()
            parent = dialogflow.AgentsClient.agent_path(project_id)
            training_phrases = []
            for training_phrases_part in training_phrases_parts:
                part = dialogflow.Intent.TrainingPhrase.Part(text=training_phrases_part)
                training_phrase = dialogflow.Intent.TrainingPhrase(parts=[part])
                training_phrases.append(training_phrase)

            text = dialogflow.Intent.Message.Text(text=intent_message_texts)
            message = dialogflow.Intent.Message(text=text)

            intent = dialogflow.Intent(
                display_name=intent_display_name, training_phrases=training_phrases, messages=[message]
            )

            response = intents_client.create_intent(
                request={"parent": parent, "intent": intent}
            )

Now I want to create a follow-up intent. I have searched a lot on the internet and got this link. In this they give the following code o create the follow up intent as given below:

followup_intent=dialogflow.types.Intent.FollowupIntentInfo(followup_intent_name="custom- followup")
 intent = dialogflow.types.Intent(
     display_name=display_name,
     training_phrases=training_phrases,
     messages=message,
     output_contexts=[
     dialogflow.types.Context(
     name=contexts_client.context_path("fir-chatbot-ccc2d", "-", "next-output"),lifespan_count=1)],
     input_context_names=["projects/project_id/agent/sessions/-/contexts/name"],
     webhook_state=dialogflow.enums.Intent.WebhookState.WEBHOOK_STATE_ENABLED,
     #is_fallback=True,
     parent_followup_intent_name="projects/project_id/agent/intents/id",
     followup_intent_info=[followup_intent]
)

But I could not get this code. Please help me to create follow-up intent in Dialogflow using Python code.

Upvotes: 0

Views: 450

Answers (1)

ewertonvsilva
ewertonvsilva

Reputation: 1955

Here is the function to create a followup intent. Remember to use the correct project_id and to edit the parent_intent variable. I have added some additional details in this link with more details of how test and execute it.

def create_intent(project_id, display_name, training_phrases_parts, message_texts):
    """Create an intent of the given intent type."""
    from google.cloud import dialogflow

    intents_client = dialogflow.IntentsClient()

    #TODO: EDIT WITH YOUR VALUES

    parent_intent="projects/project-id-<project id data>/agent/intents/792d58b1-30fc-49cd-be2b-<parent intent id>"
    parent = dialogflow.AgentsClient.agent_path(project_id)

    training_phrases = []
    for training_phrases_part in training_phrases_parts:
        part = dialogflow.Intent.TrainingPhrase.Part(text=training_phrases_part)
        # Here we create a new training phrase for each provided part.
        training_phrase = dialogflow.Intent.TrainingPhrase(parts=[part])
        training_phrases.append(training_phrase)

    text = dialogflow.Intent.Message.Text(text=message_texts)
    message = dialogflow.Intent.Message(text=text)

    intent = dialogflow.Intent(
        display_name=display_name,
        training_phrases=training_phrases,
        messages=[message],
        parent_followup_intent_name=parent_intent
    )

    
    response = intents_client.create_intent(
        request={"parent": parent, "intent": intent}
    )

    print("Intent created: {}".format(response))

Upvotes: 1

Related Questions