Building a chatbot is a complex task that requires integrating different components to achieve the desired functionality. Custom actions play a crucial role in creating an engaging and interactive chatbot experience. In this tutorial, we will explore how to develop custom actions for build your own chatbot using Python and Dialog flow.
Setting Up the Environment
Before starting, ensure that you have the necessary tools installed on your machine. You will need Python 3.6 or above, the Dialog flow Python Client, and a code editor. Once you have these set up, you can create a new Python file and import the required modules:
Next, you need to set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of your Dialog flow service account JSON file.
import dialogflow_v2 as dialogflow
from google.api_core.exceptions import InvalidArgument
import os
os.environ[“GOOGLE_APPLICATION_CREDENTIALS”] = “path/to/service_account.json”
Defining The Custom Action
A custom action is a piece of code that performs a specific task, such as sending an email or querying a database. In this example, we will create a custom action that fetches the weather forecast for a particular location using an external API.
def get_weather(location):
# code to fetch weather data for location
return weather_data
Handling the Intent
To use the custom action in your chatbot, you need to define an intent that triggers it. In Dialog flow, an intent is a mapping between what a user says and what action should be taken.
def weather_intent_handler(request):
intent_name = request[“queryResult”][“intent”][“displayName”]
if intent_name == “GetWeather”:
location = request[“queryResult”][“parameters”][“location”]
weather_data = get_weather(location)
response = f”The weather in {location} is {weather_data}.”
return response
else:
return “Unknown intent”
In the code above, we define a function weather_intent_handler that takes a request object as input and checks if the intent name is GetWeather. If it is, we extract the location parameter from the request and call the get_weather function to fetch the weather data. Finally, we return a response that includes the weather data.
Creating A Fulfillment
To connect the custom action to the Dialog flow intent, you need to create a fulfillment that calls the weather_intent_handler function when the intent is triggered.
def webhook(request):
req = request.get_json(force=True)
fulfillment_text = weather_intent_handler(req)
response = {“fulfillmentText”: fulfillment_text}
return response
In the code above, we define a function webhook that takes a request object as input and extracts the JSON payload. We then call the weather_intent_handler function with the request object and create a response object that includes the fulfillment text.
Deploying The Custom Action
Once you have defined your custom action, you need to deploy it to the cloud so that it can be accessed by your chatbot. In this example, we will deploy the custom action to Google Cloud Functions.
def main(request):
headers = {
“Access-Control-Allow-Origin”: “*”,
“Access-Control-Allow-Methods”: “POST”,
“Access-Control-Allow-Headers”: “Content-Type”,
}
if request.method == “OPTIONS”:
return “”, 204, headers
if request.method == “POST”:
response = webhook(request) return response, 200, headers
In the code above, we define a function main that handles HTTP requests. We set the required headers for CORS and check if the request method is OPTIONS. If it is, we return an empty response with status code 204. If the request method is POST, we call the webhook function and return the response with status code 200.
To deploy the custom action to Google Cloud Functions, you need to create a new function and upload your Python file. Once your function is deployed, you can test it using the Dialog flow console or by sending a POST request to the function’s URL.
Testing The Custom Action
To test the custom action, you can create a new intent in the Dialog flow console and add a sample user expression that triggers the intent. You can then use the inline editor to add the fulfillment URL and deploy the intent.
Once the intent is deployed, you can test it using the chatbot simulator in the Dialog flow console. Enter a sample user expression and check if the chatbot returns the expected response.
Final Words:
In conclusion, custom actions play a critical role in creating engaging and interactive chatbot experiences. By following the steps outlined in this tutorial, you can create custom actions using Python and Dialog flow and deploy them to the cloud for use in your chatbot. Remember to test your custom actions thoroughly to ensure that they function correctly and provide the desired user experience.