Whatsapp integrations

I developed two custom integrations using two different software that can send WhatsApp notifications straight from Home-assistant without the need to register to 3rd party systems or official WhatsApp cloud API accounts.

  1. The first one: https://github.com/t0mer/matterbridge-custom-notifier is using Matterbridge (Written in GO).

  2. The second one https://github.com/t0mer/wapi-custom-notifier is using https://github.com/chrishubert/whatsapp-api/.

Wapi is better than the matterbridge because it can also send messages to contacts while matterbridge can send only to groups.

7 Likes

Hello friend, congratulations on the project, I’ve been looking for something like this for free for some time, regarding wapi, I’ve been receiving a log with the phrase in my docker console,

whatsapp_web_api | Initialization error: Timed out after 30000 ms when trying to connect to the browser! Chrome only in revision r982053 it is guaranteed to work.

the QR Code was not even generated.

Thank you for this project, it helped solve a particular issue I was having.

Hi, @Tomer_Klein Before I found this topic I also was working to find a Whatsapp self-served solution in the HA Docker environment. I found my solution in the rest_command (and using https://github.com/chrishubert/whatsapp-api):

rest_command:
  send_whatsapp_web_api_string:
    url: "http://localhost:3000/client/sendMessage/HASS"
    method: post
    headers:
      content-type: application/json
    payload: >
      {
      "chatId": "<see_contacts>@g.us",
      "contentType": "string", 
      "content": "Keldervloer - *{{ states('binary_sensor.waterleak_water_leak') }}*"
      }

f.y.i Conditional send inside an automation

To be honest I had/have some problems to find the right syntax because examples are scares but luckily one issue mentioned the required URL sendMessage-string :grin: . And with some inspiration of this topic you can see that above example also sends the value of the sensor (perhaps a helpful addition).

I am still looking for the example how to retrieve an incoming message it would be most welcome. Think the method will be likely “get” but what is the URL??? If you (or someone else) already knows how to do this please share.

Hello,

@Tomer_Klein

I am trying to do the docker wapi installation, I guess this need to be done on a ma maching that has docker installed.

I created the yaml file and did the docker compose pull and up and getting some errors.

I am currently running Docker version 24.0.7, build afdd53b

when I initially copied the yaml configuration it complained about the version and asked me to use either 2.2 or 3.2

I changed it the 3.8 to 3.2, then after that change I am getting the following error:


Couldn't connect to Docker daemon at http+docker://localhost - is it running?

If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable.

Tried to add DOCER_HOST=FQDN of the docker server, didn’t make a difference, also remove the localhost on the basewebhook variable to the FQDN of the server didn’t make any difference.

I am not an expert in docker, and I would appreciate any help.

This is nice - got plain text messages working fine with the wapi components.

@Tomer_Klein Have you had any success sending images or video or othe rmedia, for exampe camera output etc? If so, are you able to share how? Thanks

@Tomer_Klein This looks very interesting, but I just cannot get it to work. I’m following your instructions to the point, the session will never link/connevt. At 5) in your instructions I just get the message:
{“success”:false,“error”:“session_not_connected”}

Since port 3000 is already in use on my docker host I had to map that to something else, and I used 3010. I did change the callback URL in the docker compose file as I figured it will still be valid internally in the container. Well I did try to change this to the actual host and the new/mapped port too but that made no difference. I still get the session_not_connected message above.

Any ideas?

Hi @chjohans
I’m also having the same issue with the whatsapp container and i opened an issue for that. Hopefully the developer wil soon find the solution.

In the meantime, you can use my new integration that work better:

In the next version i will add the ability to attach media.

Hello.
I try to use your green api and its working good. but i want to send a snapshot which i have on a url “http://192.168.1.22/snap.jpeg” how can i send it?

I don’t think you need to change the callback URL, as it would still be localhost:3000 IMHO, the api still responds internally to it. You did map the external port of the docker container.
I think I got it connected (but now am stuck somewhere else) by doing the following:

  • add “network_mode: bridge” to the docker-compose.yml of wapi
  • add " links: “whatsapp_web_api:whatsapp_web_api” " to your homeassistant container - I did it within my container manager, not in compose but it should be similar
  • change url in notify part of configuration.yaml to “url: http://whatsapp_web_api:3010/client/sendMessage”

It might be that linking via bridge might be “the old way” of handling it in docker, and can be done differently, but that’s how my changedetection and playwright are working flawlessly so I followed that example.

I think one of the issues is that “media_url” seems to be mandatory. If I try to send without, I get the following:

  File "/config/custom_components/wapi/notify.py", line 69, in send_message
    if data["media_url"]:
       ~~~~^^^^^^^^^^^^^
TypeError: 'NoneType' object is not subscriptable

So it requires “media_url:” in the data field, then it tries to send.

I am stuck now on

2024-06-29 23:00:23.819 ERROR (SyncWorker_1) [custom_components.wapi.notify] Error sending notification using wapi: HTTPConnectionPool(host='whatsapp_web_api', port=3123): Max retries exceeded with url: /client/sendMessage/<sessionidremoved> (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f10268c2240>: Failed to establish a new connection: [Errno 111] Connection refused'))

Hello Tomer, thank you for your work.
I choose matterbridge as i’m running a core install of hassio (no docker) .
I successfully installed matterbridge but cannot find the location of the matterbridge.taml nor where to ass the config

[general]
LogFile="/var/log/matterbridge.log" #Path to log file
IconURL="https://github.com/identicons/{NICK}.png" #Create avatar from github if user does not have one
PreserveThreading=true
ShowUserTyping=false
ShowJoinPart=false
NoSendJoinPart=false

[api.myapi]
BindAddress="0.0.0.0:4242" #Set API bind address and port
Buffer=1000 
RemoteNickFormat="{NICK} "
Token="Add you own token" #Optional, secure the api endpoint with strong token

[whatsapp.bridge]
# Number you will use as a relay bot. Tip: Get some disposable sim card, don't rely on your own number.
Number="+xxxxxxxxxxxx"
# First time that you login you will need to scan QR code, then credentials willl be saved in a session file
# If you won't set SessionFile then you will need to scan QR code on every restart
# optional (by default the session is stored only in memory, till restarting matterbridge)
SessionFile="session-xxxxxxxxxxxx.gob"
# If your terminal is white we need to invert QR code in order for it to be scanned properly
# optional (default false)
QrOnWhiteTerminal=false
# Messages will be seen by other WhatsApp contacts as coming from the bridge. Original nick will be part of the message.
#RemoteNickFormat="@{NICK}: "
RemoteNickFormat="{NICK}: "
# extra label that can be used in the RemoteNickFormat
# optional (default empty)
Label="Organization"


[[gateway]]
name="gateway1"
enable=true

    [[gateway.out]] #Set as out - send the message to this group
    account="whatsapp.bridge"
    channel="[email protected]"


    [[gateway.in]] #Set the API as in, receive message from the api 
    account="api.myapi"
    channel="api"

This (green-api) is amazing and I was able to set it up in 10 minutes! Thank you @Tomer_Klein

Just one note ALL messages will be sent as from your own Whatsapp account (or the one you bind) and this means that if you want to use it to send alerts to yourself… you’ll not see them :frowning:
I’m asking Green-Api if there is any way around it.