This example shows how to handle different HTTP methods (POST and GET) as well as specific dependencies.
This example assumes you are familiar with how serverless functions work. If needed, you can check Scaleway's official documentation
This example uses the Scaleway Serverless Framework Plugin. Please set up your environment with the requirements stated in the Scaleway Serverless Framework Plugin before trying out the example.
Context: This example shows how to handle GET and POST methods. For this example, Chatterbot (a machine learning, conversational dialog engine) was used. The model used here has already been trained to communicate in English to speed up the installation. The results of the training are stored in an SQLite database (in app/english-corpus.sqlite3
). If you wish to train your model, you can follow the instructions given in Chatterbot documentation.
Explanation: When the function is triggered by a GET method, it renders an HTML file. When the function is triggered by a POST method with the parameter "message", a response is given by the trained chatbot. In all other cases (method not handled or parameter missing), an error is thrown.
In order to test your function locally before deployment in a serverless function, you can install our offline testing library with:
pip install -r requirements-dev.txt
Import your environment variables using:
export database_model="english-corpus.sqlite3"
Launch your function locally:
python app/chatbot.py
Test your local function using curl
:
curl -i -X POST localhost:8080 -d '{"message":"Hello"}'
Once your environment is set up, you can run:
# Install node dependencies
npm install
# Deploy
./bin/deploy.sh
Then, you can test your function by sending the following request:
# POST request
curl -i -X POST <function URL> -d '{"message":"Hello"}'
This will tell the chatbot "Hello". The expected answer should be something similar to "Hello" or "Hi".
The result of your function can also be checked through a browser. The expected behavior is to render the HTML file. And whenever a message is sent to the chatbot, a response should appear in the conversation.