Skip to content

victorluissantos/whatsApp-Bot-API

Repository files navigation

English Português Español

WhatsApp Bot API - FastAPI

This project was migrated from Flask to FastAPI to improve performance and provide automatic API documentation. It now uses RabbitMQ for asynchronous message queueing, while MongoDB keeps message status history and webhook configuration.

Main Changes

  • Framework: Migrated from Flask to FastAPI
  • Main file: main.py (previously app.py)
  • Documentation: Automatic Swagger UI available at /docs
  • Validation: Pydantic models for data validation
  • Performance: Improved performance with FastAPI
  • Async messaging: RabbitMQ queue for async sends (/sendMessageAsync) with status persisted in MongoDB

Available Endpoints

GET /

  • Home page with API documentation

POST /sendText

  • Sends text message via WhatsApp
  • Parameters: phone (string, max 22 chars), text (string, max 800 chars)

GET /sendText

  • Sends text message via WhatsApp (GET method)
  • Parameters: phone (string, max 22 chars), text (string, max 800 chars)

POST /sendMultText

  • Sends message with URL via WhatsApp
  • Parameters: phone (string, max 22 chars), text (string, max 800 chars)

GET /sendMultText

  • Sends message with URL via WhatsApp (GET method)
  • Parameters: phone (string, max 22 chars), text (string, max 800 chars)

System Screens

Home Screen

Home Screen

Swagger Screen

Access the interactive API documentation (Swagger UI) at /docs:

Swagger UI

API Documentation

Access /docs to view the interactive API documentation (Swagger UI).

Execution

With Docker Compose

docker compose up --build

Locally

pip install -r IaC/flask/requirements.txt
uvicorn main:app --host 0.0.0.0 --port 8000

Visual Debug (VNC)

  • By default, Selenium runs hidden (headless) in main.py with:
    • WINDOW_SHOW_DEBUG = False
  • To show the browser window for debugging, change it to:
    • WINDOW_SHOW_DEBUG = True
  • Rebuild and restart containers after changing this value:
docker compose down
docker compose up -d --build

Verify the VNC port

The VNC server is exposed on host port 5914 (see 5914:5914 in docker-compose.yml). After the stack is up, confirm the port is listening before opening the viewer:

ss -tln | grep 5914

Or test TCP connectivity:

nc -zv 127.0.0.1 5914

With Docker Compose, you can also show the published mapping for the service port:

docker compose port fastapi 5914
  • Open the container desktop via VNC:
gvncviewer 127.0.0.1:5914
  • If you use TigerVNC:
vncviewer 127.0.0.1:5914
  • When the VNC client asks for a password, use:
    • V0oiye3R

Project Structure

├── main.py                 # Main FastAPI application
├── datasource/            # Data modules
├── static/               # Static files
├── templates/            # HTML Templates
├── IaC/flask/           # Docker configurations
│   ├── Dockerfile
│   ├── requirements.txt
│   └── entry_point.sh
└── docker-compose.yml

Environment Variables

Configure the following variables in the .env file:

  • MONGOUSER: MongoDB username
  • MONGOPASSWORD: MongoDB password
  • MONGODB: Database name
  • FASTAPIPORT: Application port (default: 8000)
  • FASTAPINAME: Container name (default: fastapi-app)
  • RABBITMQ_USER: RabbitMQ username
  • RABBITMQ_PASS: RabbitMQ password
  • TZ: Container timezone (IANA time zone name). Affects system time, Python, and the Chrome session used for WhatsApp Web. If unset, Compose defaults to UTC.

Timezone (Docker and WhatsApp Web)

The FastAPI service image includes tzdata and passes TZ from your .env (see docker-compose.yml). Without it, the container often runs in UTC, so message times in WhatsApp Web can look offset from your local time.

  1. Set TZ in .env to your region (examples below).
  2. Restart the stack: docker compose down && docker compose up -d --build (rebuild once after pulling changes that add tzdata).

Find your zone name

Region / use case TZ value (IANA)
Brazil (Brasília) America/Sao_Paulo
Portugal Europe/Lisbon
Spain (peninsula) Europe/Madrid
United Kingdom Europe/London
US Eastern America/New_York
US Pacific America/Los_Angeles
UTC UTC

Initial Setup

  1. Copy the example file:
cp env.example .env
  1. Edit the .env file with your settings:
nano .env
  1. Run the project:
docker compose up --build

Contact

Developed by Victor Luis Santos
LinkedIn

About

This project was migrated from Flask to FastAPI to improve performance and provide automatic API documentation.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors