📜 ⬆️ ⬇️

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

I've seen quite a lot of examples of using whatsapp notifications with zabbix, and other monitoring systems, but this article inspired me for my own experiments with whatsapp. However, if everything is clear with the monitoring systems, then in the presence of an excellent “innate” system of email alerts, making a garden for one bacula was frankly lazy. And suddenly, then you want to put zabbix or something else to send? Every bot on vatsapu?
So, let it be something more universal. For example, a separate server that can serve a bacula, zabbix, syslog server, site, or even windows with macs.


UPD: At once I’ll make a reservation that the tasks “the easiest way to send an arbitrary message to the phone” did not stand, as there is a jabber, sms, any mobile email client, finally. The task was: a) to adapt exactly whatsapp, since it already exists in my phone and about 1 billion others, unlike the same notorious telegram b) to make the gateway as universal as possible, from which you can quickly redirect the distribution to wherever then duplicate.

Let's get started I put it 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 it is convenient to work later

yum -y update yum -y install epel-release openssh-server systemctl enable sshd systemctl start sshd 

Next, set 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 

We register mobile number. Simka is inserted into a mobile phone (or gateway, in my case) to which SMS will come. The number should not be illuminated in whatsapp. First, we request the 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 a different country code (for Russia it is 250)
mnc - mobile network code - this is the code of your operator. (01 - MTS, 02 - megaphone, 20 - tele2, 99 - beeline)

In response, you will receive an SMS with the code type XXX-XXX, which we use to confirm the registration

 yowsup-cli registration --register xxx-xxx --phone 7xxxxxxx --cc 7 

The server will report 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: 7xxxxxxxxxxx X1isWwe + 37d / aOXJpcSduzTV7fg =
type: new

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=7xxxxxxx password=X1isWwe+25d/aOXJpcSduzTV7fg= 

Actually, everything. We try

 yowsup-cli demos -c yowsup-cli.config -s 7 "alarm" 

If the message has arrived, move on. To comfortably play around with a live whatsapp chat from the terminal - you need Cyrillic. If it is not there, and instead of it errors like “ascii ... ordinal not in range (128)”, you need to set the locale and fix one file.

With localectl there are some problems in my setup, so:

 nano /etc/locale.conf 

write LANG=ru_RU.UTF-8 , save and restart session

The next step is to correct the encoding yowsup. We do, as stated here .

 nano /usr/lib/python2.7/site-packages/yowsup2-2.5.2-py2.7.egg/yowsup/demos/cli/layer.py 

After the import sys line add:

 reload(sys) sys.setdefaultencoding('utf8') 

should be:

 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 run live chat and send-receive messages in Cyrillic. Have fun.

Having played enough, it's time to adapt this wonderful tool for the needs of workers around servers. For this, it was decided to use a fairly simple and flexible tool Webhook github.com/adnanh/webhook , although there are a lot of other ways. For example, shell2http github.com/msoap/shell2http .

Download and unpack the webhook in any directory. For convenience, I put in / var / webhook

 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 so that later, if desired, it was more convenient to add all sorts of files, pictures, videos - just copied yowsup-cli.config into / 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 previously prepared commands via http, we will create a file with hooks.json rules.

 cd /var/webhook​​​​​ nano hooks.json 

We will describe a hook in the file that will receive commands at http:// :9000/hooks/wp-admin , process and send messages to the admins whatsapp. 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, anyone who calls the url http:// :9000/hooks/wp-admin?msg= - /var/webhook/admin.sh script, and he, in turn, sends his regards to admin on whatsupp , and only him. I decided not to transmit a couple of phone messages each time, and if necessary, send to other numbers to create additional hooks. For example, a hook with sending to a specific contact list or universal with a pair of telephone message and some kind of token for greater security.

So, we check if 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, create a send script /var/webhook/admin.sh, which will send messages to the admin:

 #! /bin/bash msg="$(echo "$*" | tr ' ' ' ')" if [[ ! -z "${msg/ //}" ]]; then /usr/bin/yowsup-cli demos -c /var/yowsup/yowsup-cli.config -s 7xxxxxxxxxx "$msg" fi 

Added a check for emptiness and spaces, so that any call to the hook will not receive empty messages.

Open in the browser http:// :9000/hooks/wp-admin?msg= . Everything should work. Add a hook to autoload. 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 

We save. We start. We are checking.

  systemctl enable webhook systemctl start webhook systemctl -l status webhook 

Now you can make a simple sending using curl from another machine

 curl -G http:// :9000/hooks/wp-admin?msg= 

However, this way you can send only boring messages without formatting, similar to sms. To receive beautifully formatted messages, and even with emoji-graphics, you must first encode the contents of msg in urlencode. Otherwise, esc-sequences will be in the url. This is useful for further examples with Bacula.

Surl can do urlencode independently, 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. so that, in fact, inspired the above described dances. We go to the server where the bacula-director is installed, set bc and curl.

 yum -y install bc curl 

Then we take an excellent script from the article bacula.us/sending-notification-whatsapp and tweak a little. Attention, if you want to use emoji emoticons, the file must be in UTF-8.

From the Variables section we delete RECIPIENT_NUMBER and CONF, we will not need them. And the variable YOWSEXEC is changed to the curl call of the send hook to the admin.

 # 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 put the right to perform:

 chmod +x /etc/bacula/send_whatsapp.sh 

Is done. Now, as stated in the article, you can insert into tasks

 Command = "/etc/bacula/send_whatsapp.sh %i" 

and get beautiful reports on your phone:



Thanks to all. I would be grateful for comments and corrections.

UPD (01/12/2018): Yousup updated to 2.5.7, bugs fixed. If someone does not work, update yowsup, use --env android (corrected above) when registering.

Source: https://habr.com/ru/post/327404/


All Articles