Kotlin
WhatsApp API + Kotlin examples.
We’ve created a few examples on how to interact with WAHA WhatsApp API with Kotlin (ktor).
You can find the examples in examples folder on GitHub with detailed instructions in README.md ->
Installation
We assume that you have installed software:
- Kotlin + JVM
- Docker
Download and start image
First of all, you must run WhatsApp API locally (which under the hood it runs real WhatsApp Web instance and expose HTTP API for interaction).
Here are the steps from Quick Start:
Download and start WhatsApp API docker container
# Download the image
docker pull devlikeapro/waha
# Run the docker container
docker run -it --rm --network=host -e WHATSAPP_HOOK_URL=http://localhost:5000/bot -e "WHATSAPP_HOOK_EVENTS=*" --name waha devlikeapro/waha
# It prints logs and the last line must be
# WhatsApp API is running on: http://[::1]:3000
Download image - ARM
If you’re using ARM (like Apple Silicon, Apple M1, etc.) - use following commands to download the image
For Core version the command is
# Download the image
docker pull devlikeapro/waha:arm
# Rename it, so you can use devlikeapro/waha image in other place
docker tag devlikeapro/waha:arm devlikeapro/waha
# Run the docker container
docker run -it --rm --network=host -e WHATSAPP_HOOK_URL=http://localhost:5000/bot -e "WHATSAPP_HOOK_EVENTS=*" --name waha devlikeapro/waha
# It prints logs and the last line must be
# WhatsApp API is running on: http://[::1]:3000
Start session and scan QR
- Open Swagger API in the browser http://localhost:3000/
- Start session and scan QR code in swagger
- Find
POST /api/session
request press Try it out and Execute it withdefault
session name - Find
GET /api/screenshot
and execute it - it must show QR code - Scan QR code on your mobile WhatsApp application (that installed on your phone)
- Execute
GET /api/screenshot
once again - it must show the screenshot from WhatsApp Web.
- Find
- Send test text message - find
POST /api/sendText
and execute it with payload (change12132132130
in thechatId
to phone number that is registered in WhatsApp).
{
"chatId": "12132132130@c.us",
"text": "Hi there!",
"session": "default"
}
If you see Hi there! message then you’re ready to run bots!
WhatsApp Echo Bot
The WhatsApp Echo Bot is a sample ktor webhook server application that echoes back to you whatever you send it. It can serve as a basic reference for how to set up webhooks and reply to incoming messages.
# Clone the git repository with example
git clone https://github.com/devlikeapro/waha-kotlin.git
# Build and run the app
./gradlew build
./gradlew run
Open http://127.0.0.1:5000/bot - if you see WhatsApp Echo Bot is ready! then the bot is ready to receive message!
Send message to the WhatsApp (that you used to scan QR code) and it’ll echo text back to you!