
Sending service messages to whatsapp via yowsup2 using the http-get method, including Bacula backup server reports
- Tutorial

So, let it be something more universal. For example, a separate server that can serve bacula, zabbix, syslog-server, website, or even Windows with macs.
UPD: I’ll make a reservation right away that the “easiest way to send an arbitrary message to the phone” task wasn’t because there is a jabber, SMS, and any mobile email client, finally. The task was: a) to adapt exactly whatsapp, since it is already on my phone and about 1 billion others, unlike the notorious telegram b) to make the gateway as universal as possible, from which you can quickly redirect the newsletter to wherever, and then duplicate.
Let's get started. I put on an "empty" Centos 7 amdx64 in an lxc container under proxmox ve 4.4.
The first thing I usually do is update, connect epel and install ssh, so that later it’s convenient to work
yum -y update
yum -y install epel-release openssh-server
systemctl enable sshd
systemctl start sshd
Next, put the dependencies and yowsup :
yum -y install unzip wget tar nano
yum -y install python python-dateutil python-argparse
yum -y install libtiff-devel libjpeg-devel libzip-devel freetype-devel lcms2-devel
yum -y install gcc
yum -y install protobuf pycrypto python-axolotl-curve25519
yum -y install python-devel python-pip python-imaging
pip install --upgrade pip
Download, unpack and install yowsup:
cd /usr/src/
wget https://github.com/tgalal/yowsup/archive/master.zip
unzip master
cd yowsup-master/
python setup.py install
Register your mobile number. Simka is inserted into the mobile phone (or gateway, in my case) to which SMS will come. The number should not be highlighted in whatsapp. First we request a registration code:
yowsup-cli registration --requestcode sms --phone 7xxxxxxxxxx --cc 7 --mcc 250 --mnc xx --env android
phone - your phone number starting with 7
cc - country code is the country code (for Russia it is 7)
mcc - mobile country code is another country code (for Russia it is 250)
mnc - mobile network code is the code of your operator. (01 - MTS, 02 - megaphone, 20 - tele2, 99 - beeline)
In response, you will receive an SMS with the code of the form XXX-XXX, which we use to confirm registration
yowsup-cli registration --register xxx-xxx --phone 7хххxxxxxxx --cc 7
The server will report a successful registration:
status: ok
kind: free
pw: X1isWwe + 25d / aOXJpcSduzTV7fg =
price: 33.00 rub.
price_expiration: 1495380655
currency: RUB
cost: 33.00
expiration: 4444444444.0
login: 7hhhhhhhhhh X1isWwe + 37d / aOXJpcSduzTV7fg =
type: new
We write the password from the pw variable to the config file
nano yowsup-cli.config
There are three lines in this file (country, number, password):
cc=7
phone=7хххxxxxxxx
password=X1isWwe+25d/aOXJpcSduzTV7fg=
Actually, that's all. Try
yowsup-cli demos -c yowsup-cli.config -s 7хххххххххх "alarm"
If the message has arrived, move on. To comfortably indulge in a live whatsapp chat from the terminal, you need Cyrillic. If it is not there, and instead of it an error of the form “ascii ... ordinal not in range (128)”, you need to put the locale and fix one file.
With
localectl
my the setup some problems, so:nano /etc/locale.conf
We write
LANG=ru_RU.UTF-8
, save and restart the session. The next step is to correct the encoding yowsup. We do as it is said here .
nano /usr/lib/python2.7/site-packages/yowsup2-2.5.2-py2.7.egg/yowsup/demos/cli/layer.py
After the line import sys add:
reload(sys)
sys.setdefaultencoding('utf8')
should get:
from .cli import Cli, clicmd
from yowsup.layers.interface import YowInterfaceLayer, ProtocolEntityCallback
from yowsup.layers.auth import YowAuthenticationProtocolLayer
from yowsup.layers import YowLayerEvent, EventCallback
from yowsup.layers.network import YowNetworkLayer
import sys
reload(sys)
sys.setdefaultencoding('utf8')
Now you can start a live chat and send-receive messages in Cyrillic. Have fun.
Having played enough, it’s time to adapt this wonderful tool to the needs of workers around the servers. To do this, it was decided to use the fairly simple and flexible Webhook tool github.com/adnanh/webhook , although there are many other ways. For example shell2http github.com/msoap/shell2http .
Download and unpack webhook into any directory. I put in / var / webhook for convenience
cd /usr/src
wget https://github.com/adnanh/webhook/releases/download/2.6.3/webhook-linux-amd64.tar.gz
tar -xvf webhook-linux-amd64.tar.gz -C /var
mv /var/webhook-linux-amd64 /var/webhook
And then, if desired, it would be more convenient to add all sorts of image-video-files - just copy yowsup-cli.config to / var / yowsup As they say, let everything be in one place.
mkdir /var/yowsup
cp /usr/src/yowsup-master/yowsup-cli.config /var/yowsup
Since Webhook allows you to run pre-prepared commands via http, create a file with the rules
hooks.json
.cd /var/webhook
nano hooks.json
In the file, we describe the hook that will receive commands at the address
http://адрес сервера:9000/hooks/wp-admin
, process and send messages to whatsapp admin. Inside the file, add a description of the conditions:[
{
"id": "wp-admin",
"execute-command": "/var/webhook/admin.sh",
"command-working-directory": "/var/webhook",
"pass-arguments-to-command":
[
{
"source": "url",
"name": "msg"
}
]
}
]
That is, everyone who calls url will
http://адрес сервера:9000/hooks/wp-admin?msg=привет
execute the script /var/webhook/admin.sh
, and that, in turn, will send hello to the admin on whatsupp, and only him. I decided not to send a phone-message pair every time, and if necessary to send other numbers to create additional hooks. For example, a hook sending a specific contact list or a universal phone-message pair and some kind of token for security. So, we check whether we wrote the hook correctly by running:
./webhook -hooks hooks.json -verbose
In response, we will see:
[webhook] 2017/04/26 05:12:48 version 2.6.3 starting
[webhook] 2017/04/26 05:12:48 setting up os signal watcher
[webhook] 2017/04/26 05:12:48 attempting to load hooks from hooks.json
[webhook] 2017/04/26 05:12:48 found 1 hook (s) in file
[webhook] 2017/04/26 05:12:48 loaded: wp-admin
[webhook] 2017 / 04/26 05:12:48 serving hooks on 0.0.0.0 : 9000 / hooks / {id}
Next, we create the send script /var/webhook/admin.sh, which will send messages to the administrator:
#! /bin/bash
msg="$(echo "$*" | tr ' ' ' ')"
if [[ ! -z "${msg/ //}" ]]; then
/usr/bin/yowsup-cli demos -c /var/yowsup/yowsup-cli.config -s 7xxxxxxxxxx "$msg"
fi
A check for emptiness and spaces was added so that with any call to the hook empty messages would not come.
We open in the browser
http://адрес сервера:9000/hooks/wp-admin?msg=привет
. Everything should work. Add a hook to startup. I did through systemd
.nano /etc/systemd/system/webhook.service
We describe the unit:
[Unit]
Description=Webhook
After=syslog.target
After=network.target
[Service]
Type=simple
PIDFile=/var/webhook/webhook-service.pid
WorkingDirectory=/var/webhook
User=root
Group=root
OOMScoreAdjust=-500
ExecStart=/var/webhook/webhook -hooks hooks.json -verbose
ExecStop=/usr/bin/pkill -f webhook
ExecReload=
TimeoutSec=300
[Install]
WantedBy=multi-user.target
Save. We start. We check.
systemctl enable webhook
systemctl start webhook
systemctl -l status webhook
Now you can make simple sending using curl from another machine
curl -G http://адрес сервера:9000/hooks/wp-admin?msg=привет
However, only boring messages without formatting similar to sms can be sent this way. To receive beautifully formatted messages, and even with emoji graphics, you must first encode the contents of msg in urlencode. Otherwise, esc sequences will get into the url. This is useful for further example with Bacula.
Сurl can do urlencode on its own, only the syntax will be different. For example, send a message with a line break:
export VAR="привет\nбуфет"
export MSG=$(echo -e $VAR)
curl -G http://адрес сервера:9000/hooks/wp-admin --data-urlencode msg="$MSG"
Now you can do bacula, i.e. by the fact that, in fact, led to the above dances, we go to the server where the bacula-director is installed, set bc and curl.
yum -y install bc curl
Then we take a beautiful script from the bacula.us/sending-notification-whatsapp article and tweak it a bit. Attention, if we want to use emoji emoticons, then the file must be in UTF-8.
From the Variables section, delete RECIPIENT_NUMBER and CONF, we will not need them. And we change the YOWSEXEC variable to a curl call to the admin sending hook.
# Variables
HOUR=$(date +%d/%m/%Y\ %H:%M:%S)
YOWSEXEC="curl -G http://ваш сервер:9000/hooks/wp-admin"
LOG="/var/log/bacula/whatsapp.log"
Enter your data to connect to MySQL
# MySQL config
DBUSER="bacula"
DBPASSWORD="bacula"
DBNAME="bacula"
And instead of the send line:
$YOWSEXEC demos --config $CONF --send $RECIPIENT_NUMBER "`echo -e "$MESSAGE${COUNT}"`" &>> $LOG
write:
FMSG=`echo -e "$MESSAGE${COUNT}"`
$YOWSEXEC --data-urlencode msg="$FMSG" &>> $LOG
We set execution rights:
chmod +x /etc/bacula/send_whatsapp.sh
Done. Now, as indicated in the article, you can embed in tasks
Command = "/etc/bacula/send_whatsapp.sh %i"
and receive beautiful reports on the phone:

Thank you all. I would be grateful for comments and corrections.
UPD (01/12/2018): Yousup was updated to 2.5.7, bugs were fixed. If it doesn’t work for anyone, update yowsup, use --env android during registration (corrected above).