Build a Webhook for a Chatbot Using Python
- How to create a Python endpoint in SAP BTP, using Flask
- How to read POST data from a chatbot
- How to send back data to a chatbot
- How to deploy a Python script to SAP BTP (using Cloud Foundry CLI)
- How to connect the webhook to the chatbot
Prerequisites
- A SAP Business Technology Platform trial account. If you create a new trial account you should have the necessary entitlements, but check the tutorial Manage Entitlements on SAP BTP Trial, if needed.
- You understand the basics of creating a chatbot, as described in the tutorial Build Your First Chatbot with SAP Conversational AI.
- Python
- VS Code with the Python extension by Microsoft, though you can use any Python development environment. See Getting Started with Python in VS Code.
- Flask and requests packages for Python
- Cloud Foundry CLI
You will create a very simple chatbot that asks the user to pick an animal, and then have the chatbot call a webhook, which will then call an API to retrieve a “fun fact” about the animal via the cat-facts
API. The webhook will also update the memory variable that keeps track of how many times the user requested a fun fact.
The point of the tutorial is to show you how the webhook reads the request data from the chatbot, and to show you the format of the data that must be returned to the chatbot.
As an added bonus, we will show how to deploy a Python script to SAP BTP. Special thanks to Yohei Fukuhara for his blog Create simple Flask REST API using Cloud Foundry.
IMPORTANT: The focus of this tutorial is the response an application (API) must return in order to work with SAP Conversational AI. The requirements for Python on SAP BTP changes from time to time – e.g., quota, runtime – so you are encouraged to check the documentation for how to deploy Python scripts: Developing Python in the Cloud Foundry Environment