How to Make Jarvis With Dialogflow and Python

How to Make Jarvis With Dialogflow and Python

Dialogflow is a powerful chatbot platform developed by Google that allows you to create chatbots without any coding experience. Many companies use Dialogflow to create their own chatbots. As the name suggests, Dialogflow specializes in controlling dialogues. If you want to have specific conversations with your customers about a particular product, service, or topic, then Dialogflow is the perfect tool for you.

Dialogflow uses a common structure for chatbots, with entities, contexts, and variables to create lifelike conversations. It uses INTENTS to try to understand the user’s intent and connect it with the relevant context. Overall, Dialogflow is a versatile and easy-to-use platform that can help you create chatbots that engage with your customers in a natural and effective way.

Jarvis, the virtual assistant from Iron Man, has always been a fascination for people. With the advancements in technology, it is now possible to create a virtual assistant like Jarvis. In this tutorial, we will use Dialogflow and Python to create our own virtual assistant that can understand natural language commands and respond appropriately.

1. Creating a Dialogflow Agent

Dialogflow is a natural language processing (NLP) platform that allows you to build conversational interfaces for your applications. To get started, you need to create a Dialogflow agent. Here are the steps to create a new agent:

Go to the Dialogflow console. Click on the Create Agent button.

1. Creating a Dialogflow Agent

2. Give your agent a name, select the default language, and select the Google project you want to use. Click on the Create button.

default language

Give your agent a name, select the default language, and select the Google project you want to use. Click on the Create button.

2. Creating Intents

An intent represents an action that your virtual assistant can perform. For example, if the user says What’s the weather like?, the intent would be to get the weather information. Here are the steps to create a new intent:

  1. In the Dialogflow console, click on the Intents menu item.
2. Creating Intents

2. Click on the Create Intent button.

2. Creating Intents

3. Give your intent a name, such as Welcome, and save it.

2. Creating Intents

4.  Enter some training phrases that the user might say, such as hi there or heya.

training phrases
  1. In the Action and parameters section, you can specify any parameters that the intent needs.
  2. In the Responses section, enter the response that your virtual assistant should give when this intent is triggered.
training phrases

Repeat these steps to create more intents for your virtual assistant. 

4. In the Fulfillment section, enable the option Enable webhook call for this intent as shown in the picture.

training phrases

3. Setting Up Authentication

Steps to set up authentication for Dialogflow in your Python code are presented as bullet points:

  • Click the Settings gear icon on the left side of the Dialogflow console and Select General from the options.
3. Setting Up Authentication

Scroll down to the Google Project section and Select the project associated with your agent. By clicking this Google Cloud Console will be open.

3. Setting Up Authentication
  • On the Google cloud dashboard Click the Go to APIs overview option.
  • Now Click on the Enable API button to enable the Dialogflow API (below it’s already enabled). 
3. Setting Up Authentication

Go to the Credentials tab. Create a new Service Account key

service account
  • Download the JSON file containing the service account key. Use the service account key in your Python code to authenticate your API calls
  • Use the Dialogflow API to send user input to your agent and receive the corresponding response

4. Integrate the chatbot into the website

We have successfully developed a basic Dialogflow Chatbot. As I do not have a website, I will be utilizing Ngrok to generate a URL for the chatbot. 

To use a Ngrok URL in a webhook in Dialogflow, follow these steps:

  1. Download Ngrok from its official website unzip the folder.
  2. Install and run Ngrok on your local machine.
  3. Start a session with Ngrok by running the following command in your terminal or command prompt:
ngrok http <port_number>

Replace <port_number> with the port number on which your webhook server is running. For example, if your webhook server is running on port 5000, you would run:

ngrok http 5000
port number

Ngrok will generate a public URL that is mapped to your local server. Look for the Forwarding section in the Ngrok terminal window and note the HTTPS URL that is displayed as shown below.

forwarding section

In the Dialogflow console, go to the Fulfillment section for your agent.

fulfilment

Enable webhook fulfillment by toggling the Webhook switch to the Enabled position.

webhook

Note: A webhook is a way for two systems to communicate with each other by sending messages back and forth. 

In the context of Dialogflow, a webhook is used to send user input from Dialogflow to a custom backend system, which processes the input and sends a response back to Dialogflow.

  1. Enter the Ngrok URL as the webhook URL. Use the HTTPS URL that Ngrok generated in step 3, the webhook URL in Dialogflow would be:
https://abcdefg.ngrok.io
  1. Save the webhook configuration.

Now, when a user interacts with your Dialogflow agent, Dialogflow will send the webhook requests to the Ngrok URL, which will forward them to your local server for processing.

5. Write the Python Code

Dialogflow_Bot.py

​from flask import Flask, request, jsonify
​
app = Flask(__name__)
​
@app.route('/', methods=['POST'])
def webhook():
  if request.method == "POST":
      payload= request.json
      user_response = (payload['queryResult']['quertText'])
      bot_response = (payload['queryResult']['fulfillmentText'])
      if user_response or bot_response != "":
          print("User: "+ user_response)
          print("Bot: "+ bot_response)
      return "Message received."
  else:
      print(request.data)
      return "200"
​
       
if __name__ == '__main__':
  app.run(debug=True, use_reloader=False)

The purpose of this code is to create a webhook for a Dialogflow agent using the Flask web framework. The webhook receives POST requests from Dialogflow with user input and responds with an acknowledgment message.

More specifically, this code does the following:

  1. Imports the Flask web framework and the necessary modules for handling HTTP requests and JSON data.
  2. Creates a new Flask application instance.
  3. Defines a function named webhook that will handle incoming requests.
  4. Uses a decorator to specify that the webhook function should handle POST requests to the root URL (“/”).
  5. Parses the incoming JSON payload from the request body using request.json.
  6. Extracts the user’s input text and the webhook’s response text from the Dialogflow payload using the queryResult field.
  7. Prints the user’s input and the webhook’s response to the console.
  8. Returns a message indicating that the request was successfully received.
  9. If the incoming request is not a POST request, print the request data to the console and returns a HTTP status code 200 to indicate that the request was successfully processed.
  10. Starts the Flask application in debug mode.

In summary, this code creates a simple webhook that receives POST requests from Dialogflow and prints the user’s input and webhook’s response to the console. This code can be extended to include more complex logic for processing user input and generating webhook responses, depending on the requirements of your Dialogflow agent.

Let’s run this above code:

python Dialogflow_Bot.py
dialogflow

Note: We have Ngrok running, as we typed its URL in dialogue flow and now we are going to run this program.

Let’s type Hi in Dialogflow run it field and see how Chatbot responds: 

intent

We can see our chatbot respond Hi, What is your name? in the answer of Hi.

what is your name

Note: this is the same response we wrote in Intent Response Section above.

Next, we will click on the diagnostic info option as displayed in the image above. This will allow us to view the raw API response. As we inspect the response, we can see that the value for queryText is Hi, which was input by the user. Additionally, the value for fulfillmentText is Hi, What is your name? which is the response provided by the chatbot.

As we look back at our Python code, we can see that we have called the API using the payload variable and have retrieved the values of the user input and chatbot response from the API response.

coding

Upon running the Python code, we can observe that it is successfully capturing the POST 200 request. The output displayed in the console indicates that the user message and the response of the chatbot have been recorded. This is achieved by extracting the JSON payload from the received request and storing it in the payload variable. The user response and the chatbot response are then assigned to their respective variables within the payload.

coding

By utilizing Flask and Dialogflow, we have successfully established a connection between our chatbot and the user. 

Conclusion: 

In conclusion, we have created a basic chatbot using Dialogflow and Flask. We have utilized the features of Dialogflow to train our chatbot to understand user input and provide relevant responses. Additionally, we have implemented Flask to receive requests from Dialogflow and extract user input and chatbot responses from the JSON payload.

Overall, this project has provided us with a foundational understanding of chatbot development and the technologies involved in the process. With further exploration and experimentation, we can expand the functionality of our chatbot and create more advanced applications.

Read More:

Fix: Ray Out Of Memory Error Python

Fix – TypeError: tuple indices must be integers or slices not str

How to convert RGB to Hex and Hex to RGB code in Python 

How do I reinstall Anaconda Python

Coding Spell Python Journey | About Us 

Latest posts by Muhammad Usman Shafique (see all)