Whatsapp-bridge is a lightweight application written in python and FastAPI-MQTT. I created WhatsApp-bridge to have the ability to send WhatsApp notifications easily from any application or IoT platform using MQTT topics or HTTP calls and without needing to write an extension for every application or device in the network. To maintain the ability to combine MQTT and Web server, Im using the FastAPI-MQTT.
First youll need to follow the (instructions on this page)[https://developers.facebook.com/docs/whatsapp/cloud-api/get-started] to:
Once youve done that, go to your app and set up the WhatsApp product.
Youll be given a temporary access token and a Phone Number ID, note these down as youll need them later. Set up your own phone number as a recipient and you can have a go at sending yourself a test message:
In the test message above, you used the hello_world template. Youll need to set up your own template for your own purposes. If you go to Message Templates in the WhatsApp manager you can build your own templates.
In the following example, i created a template for my smat home. The template header if fixed and so is the footer. in the body i added variable for dynamic text:
Once you're done with the above ,you're ready to start send messages using this docker.
Before running the docker you should create the config file with the following parameters:
[Whatsapp]
api.token=
api.phone_id=
api.template_name=
api.template_language=
webhook.token=
webhook.base_url=
[MQTT]
mqtt.host=
mqtt.port=
mqtt.username=
mqtt.password=
mqtt.topic=
All fields are mandatory except webhook.token and webhook.base_url that are only needed for webhook validation and incoming messages (from WA to MQTT).
After config file is set, create the docker using the following dokcer-compose file:
version: "3.6"
services:
whatsapp-bridge:
image: techblog/whatsapp-bridge
container_name: whatsapp-bridge
restart: always
ports:
- 8081:8081
environment:
- LOG_LEVEL=DEBUG
volumes:
- ./wa-bridge:/app/config
You can send Whatsapp messages using rest notification by adding the following code to the configuration file.
notify:
- name: WhatsApp
platform: rest
resource: http://[whatsapp-bridge-ip:port]/send
data:
phone: #enter your phone number here
In the example above the message will be sent using the default template configured in the config file. if you want to send the notification using deferent template add the template name and language to the configuration section:
notify:
- name: WhatsApp
platform: rest
resource: http://[whatsapp-bridge-ip:port]/send
data:
phone: #enter your phone number here
template: #enter the temlate name here
language: #enter the template's language here
Now you can test the notification using services under developer tools
With whatsapp-bridge you can also send messages by publishing data to the topic whatsapp/send with the payload:
{
"message":"Whatsapp message",
"recipient":"1234567890"
}