diff --git a/.gitignore b/.gitignore index ad41aa1c..369f4b8b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ tokens files +venv # compiled output /dist diff --git a/README.md b/README.md index 20a76ea2..558d2669 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,43 @@ export PHONE=79776772457 curl -d "{\"chatId\": \"${PHONE}@c.us\", \"text\": \"Hello from WhatsApp HTTP API Free\" }" -H "Content-Type: application/json" -X POST http://localhost:3000/api/sendText ``` +## Echo + +To show how to receive messages we'll create a simple "echo" server with two functions: + +1. When we receive a text message - just send the text back +2. When we receive a message with a file (an image, a voice message) - download it and send the path back + +In order to send you messages we use webhooks and configure them via environments variables. So what you need to +create "echo" server is HTTP server that will receive JSON POST request and then call back WhatsApp HTTP API +via `POST /api/sendText` endpoint with JSON body. + +### Python echo server + +We use Python. Feel free to create your favorite language example and contribute to the project! + +Run "echo" server in one terminal and leave it working: + +```bash +# if you haven't already +git clone https://github.com/allburov/whatsapp-http-api.git +cd whatsapp-http-api +python -mpip install -r examples/requirements.txt +export FLASK_APP=examples/echo.py +flask run +``` + +Visit http://localhost:5000 and check that we are good to go further. + +Let's start WhatsApp HTTP API and configure the "on message" webhook and point it on our "http://localhost:5000/message" +endpoint: + +```bash +docker run -it -v `pwd`/tokens:/app/tokens --network=host -e WHATSAPP_HOOK_ONMESSAGE=http://localhost:5000/message allburov/whatsapp-http-api +``` + +Now go ahead, open the second whatsapp and send to our WhatsApp HTTP API a text message! It must reply the same text. + # Environment variables ## Common diff --git a/examples/echo.py b/examples/echo.py new file mode 100644 index 00000000..ca195dc3 --- /dev/null +++ b/examples/echo.py @@ -0,0 +1,29 @@ +from pprint import pprint + +import requests +from flask import Flask +from flask import request + +app = Flask(__name__) + + +@app.route("/") +def whatsapp_echo(): + return "WhatsApp Echo server is ready!" + + +@app.route("/message", methods=["GET", "POST"]) +def whatsapp_webhook(): + data = request.get_json() + pprint(data) + # The text + body = data['body'] + # Number in format 791111111@c.us + from_ = data['from'] + # Send a text back via WhatsApp HTTP API + response = requests.post('http://localhost:3000/api/sendText', json={ + "chatId": from_, + "text": body + }) + response.raise_for_status() + return "OK" diff --git a/examples/requirements.txt b/examples/requirements.txt new file mode 100644 index 00000000..30692b76 --- /dev/null +++ b/examples/requirements.txt @@ -0,0 +1,2 @@ +flask +requests