Sample using Whatsapp from hb(free).

2,199 views
Skip to first unread message

Diego F

unread,
Nov 29, 2021, 6:33:09 AM11/29/21
to Harbour Users
Hi, all. I want to share for those who are interested in sending/receiving messages through wsap. This does not use the paid wsap api but is totally free and is maintained with a very large community. Until when will it work? Nobody knows. But it has been working since 2019. Basically I make a bridge between this library that works with node and hb, using mysql. This could be done in several ways but as an example and as a proof of concept it fulfills its mission. You will install and use it in 5min. The client example is a simple web gui, through hw_apache or via modharbour too, just making a small adjustment. But the same could be used from any program that uses mysql. Through querys. The received and sent messages are saved in two mysql db. I leave the repo with the sources for those interested. To send a message, just by creating a new record in the db (whatsapp_out) the node server will send it. Message that is received, this same server will save it in the db (whatsapp_in) It also supports sending / receiving multimedia files. The content of the message is encoded in base64. How to install it? read README.md https://github.com/diegofazio/hbwhatsapi
Diego.

nipes...@gmail.com

unread,
Nov 30, 2021, 12:23:09 PM11/30/21
to Harbour Users
Hello Diego 
when I compile with Harbour client.prg I have this error:

hbmk2: Error: Referenced, missing, but unrecognized Harbour function(s):
       HW_POSTPAIRS(), HW_WRITE(), VALTOCHAR(), PTRTOSTR()

Maurizio 

Diego F

unread,
Nov 30, 2021, 12:36:07 PM11/30/21
to Harbour Users
The client.prg sample was created to use it with hw_apache or modharbour making some changes). Please read
https://github.com/diegofazio/hbwhatsapi#readme

If you want you could still do it directly from a simple harbour app, by extracting the select/update/insert routines from client.prg and running it like a server and checking for new messages inside a loop.

Diego.

Maurício Faria

unread,
Dec 1, 2021, 6:16:14 AM12/1/21
to harbou...@googlegroups.com
Hi Diego.
 
Does this use the WhatsApp web via a browser extension ?
If so, when WhatsApp changes the web code does this goes down ?
 
[[]] Maurício

Diego F

unread,
Dec 1, 2021, 6:36:39 AM12/1/21
to Harbour Users
Hi Mauricio, the client.js emulates a chrome session via puppeteer( https://github.com/puppeteer/puppeteer ). The extension(  https://github.com/pedroslopez/whatsapp-web.js ), it's updated every time whatsapp makes any changes. It happened to me last week. As I mentioned, this library has a very large community. Currently I am using it in a client with which I have not had any problems so far. Until when will it work? Do not know. But what I do know is that it has been working since 2019 without serious problems, more than the falls that WhatsApp already had. Diego.

Jayadev

unread,
Dec 4, 2021, 9:11:10 AM12/4/21
to Harbour Users
Hi Diego,

Thanks a ton.  Have been looking for a solution like this.

Warm regards,

Jayadev

Diego F

unread,
Dec 4, 2021, 2:22:28 PM12/4/21
to Harbour Users
Great Jayadev, always keep an eye from the whastapp-web.js repo for updates. If there was one, use "npm update" and the library will be update.

Diego.

Jayadev

unread,
Dec 5, 2021, 1:42:08 AM12/5/21
to Harbour Users
Hi Diego,

I have just looked into client.prg and find reference to session.json which I could not find in the repository.

With your help I would like to convert the client.prg into native harbour calls (i.e. without using hw_apache or modharbour) and maybe you can then add that to your repository later.
In my personal opinion, I think that this is a very important development as lot of end users are asking for such an interface.  The paid interfaces available are not 100% working.

On what email ID should i correspond with you as I do not want to create noise here till we have a working sample.

Thanks again for your effort.

Warm regards,

Jayadev

Diego F

unread,
Dec 5, 2021, 9:36:37 AM12/5/21
to Harbour Users
Jayadev, the session.json file is created when the qr code is scanned. Once this is done, every time the node server is started, if this file exists, it uses the same session that was already created. This is used so you don't have to scan the qr every time. The client.html and client.prg provide for this. It is very simple to see it, it is all in the .prg. It is pure harbour. The logic would be as follows ... Check if the session.json file exists. If this does not exist index.js(node) create a qrcode.png. Client must look for this file. If not, the node server is down or hung. If qrcode.png exists, client.html shows it until the client scans it. This qrcode.png is updated every X sec until the session is started. When the client scans it, session.json is created and qrcode.png is removed. The server is ready to receive/send messages.  
If you close the node with Ctrol + C, the session.json file, if it exists, will be deleted, having to scan the qr again. If you close the server, such as turning off the machine or closing the window, you should implement something to verify this and warn the client in some way. This can be done in several ways, for example with another DB status, where you check a timestamp updated by the server every X time and check it from the client. If this time is exceeded it's because the server (node) is offline.
While the server is up, you could also too implement a check if whatsapp is working. Sending a ping message to the same phone number that the session is started. And then check that this message has been received. This can be done in the index.js without saving the sent / received message in the mysql DB. It's just for control. Every X time do a ping. If this ping is not received, do something ... Anyway ... there are several control things that can be done. As I said, the example is just a proof of concept and and only one of the many ways to do it Diego.

Maurício Faria

unread,
Dec 7, 2021, 1:16:33 PM12/7/21
to harbou...@googlegroups.com
Hi.

Em 05/12/2021 03:42, Jayadev escreveu:
> On what email ID should i correspond with you as I do not want to
> create noise here till we have a working sample.
>

Please don't.
I am interested in this too and would like to listen the "noise"...

Regards.

Pritpal Bedi

unread,
Dec 28, 2021, 7:14:08 PM12/28/21
to Harbour Users
Hi Diego

I have tried to implement this solution on Ubuntu 20.04 LTS server. I could configure nodejs alongwith npm, than could install  whatsapp-web.js qrcode mysql via npm. Than I tried to install MySQL but failed. I did this multiple times but could not succeed. My source of instructions of MySQL installation is - https://dev.mysql.com/doc/mysql-apt-repo-quick-guide/en/#repo-qg-apt-select-series . This is the first time I am playing with MySQL so for sure I may be making some mistake but have not succeeded to figure out what. Can you help ?

Tail lines of the process depicts the following, just in case you can forward some tips.

Setting up mysql-community-client (8.0.27-1ubuntu20.04) ...
Setting up mysql-client (8.0.27-1ubuntu20.04) ...
Setting up mysql-community-server (8.0.27-1ubuntu20.04) ...
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
/usr/share/mysql-8.0/mysql-helpers: line 73:  3394 Killed                  mysqld --defaults-group-suffix="$2" --user=mysql --init-file="$1" --socket="$tmpdir/mysqld.sock" --pid-file="$tmpdir/mysqld.pid" > /dev/null 2>&1
dpkg: error processing package mysql-community-server (--configure):
 installed mysql-community-server package post-installation script subprocess returned error exit status 137
dpkg: dependency problems prevent configuration of mysql-server:
 mysql-server depends on mysql-community-server (= 8.0.27-1ubuntu20.04); however:
  Package mysql-community-server is not configured yet.

dpkg: error processing package mysql-server (--configure):
 dependency problems - leaving unconfigured
Processing triggers for man-db (2.9.1-1) ...
No apport report written because the error message indicates its a followup error from a previous failure.
                                                                                                          Processing triggers for libc-bin (2.31-0ubuntu9) ...
Errors were encountered while processing:
 mysql-community-server
 mysql-server
E: Sub-process /usr/bin/dpkg returned an error code (1)


Regards
Pritpal Bedi
a student of software analysis & concepts

Pritpal Bedi

unread,
Dec 29, 2021, 1:05:47 AM12/29/21
to Harbour Users
Hi Diego

Please disregard previous message. I could install all components and finally could execute "node index.js". I tested if the node is up or not from my laptop chrome providing the IP and it showed up Bitnami's welcome page as this node server is nunning a bitnami node.js implementation.

Now I am at a loss how to call client.html from the laptop which could provide qrcode to be scanned by the computer.

I believe for a novice your tremendous effort is a bit short of proper steps to map all what has to be done. Can you provide where to place client.html which I can view on my laptop browser. I have tried to place it in the folder from where "node index.js" is executed ?

Thanks in advance

Pritpal Bedi

Diego F

unread,
Dec 29, 2021, 6:38:26 AM12/29/21
to Harbour Users
Hi Pritpal, client.prg and client.html were designed to be used with hw_apache / modharbour. Do you know them?
If not, here es the logic of how implement it.
The qrcode is created the first time the node index.js is started or when the session.json file does not exist. This indicates that there is no active wsap session. The qrcode.png file is created within the same folder that node is running. When this file is scanned by the phone from the wsap. The index.js creates a session.json file in which the information for that session is saved. And also remove the qrcode.png since it is no longer needed. Every time the node is started the index.js will try to reuse the session.json. If this session were down, a new qrcode.png would be created to be scanned again.
If everything works fine(session started) each message that is received, index.js is in charge of adding it to the db(whatsapp_in) and from hb you could implement a server that every X time verifies the existence of a new record(status == 0, then read status->0 ). To send a message....from hb you enter a new record in the db(whatsapp_out) and index.js will be in charge of sending it.  
You could do the all, the sending and receiving of messages from hb. You can guide yourself with the client.prg
Diego.

Pritpal Bedi

unread,
Dec 29, 2021, 11:30:25 AM12/29/21
to Harbour Users
Thanks Diego

It clarifies a lot. Now I understand the flow completely. 

But, the puzzling factor is that index.js does not create qrcode.png. I tried to put many console.log("Something"); at all points but code never reaches to this if ...
//
client1.on event will trigger this code
//
client1.on('qr', qr => {
   qrcode_file.toFile(QR_CODE, qr);
   console.log(QR_CODE);
});

As previous lines suggest,  
//
const client1 = new Client({
   session: sessionData,
});

and  as Client is up when 'whatsapp-web.js' is loaded and its .on event will be trigged
//
const { Client } = require('whatsapp-web.js');

does it mean that whatsapp-web.js is not loaded ? Correct ? And if yes, what should I look into ?


Pritpal Bedi

Diego F

unread,
Dec 29, 2021, 1:56:52 PM12/29/21
to Harbour Users
Pritpal, when you start "node index.js" on console, if any error occurred, in the same console you would see it. This is a normal situation... file.png
It should be a ./qrcode.png file. When you scan the qrcode from Whatsapp App to start a web session your console should be like this...

file.png

Here the file "qrcode.png" is automatically deleted . And a new file..."session.json" is created.

Diego.

Pritpal Bedi

unread,
Dec 29, 2021, 9:00:39 PM12/29/21
to Harbour Users
Hi Diego

Now I have better understanding.

So, seems node index.js is doing the job perfectly. Now the other part of the show is client.html and client.prg.

I did not study hw_apache / modharbour but seems modharbour has something which can load a .prg and execute it, like in client.html # postdata(). Apache is used as web server whch renders client.html and than qrcode, and other in/out messages are manipulated from whatsapp_out/in tables via MySQL. If I am not missing, this job can be delegated to Harbour's hbhttpd contrib (btw, which I use extensively for my other requirements) which can eliminate dependancy on couple of heavy softwares.

Than I tried to look into what interface client.html offers by opening it in chrom on Windows 10 laptop. It just displays a pink blank screen. I am not conversant with html/ccs code so could not figure out what has went wrong with interface elements.

My understanding is that client.html has to reside in the same folder where index.js is and web-server has to be configured to publish it. Am I correct ?

Thanks for all your help. At the end we can come up with mostly Harbour based solution.


Pritpal Bedi

Pritpal Bedi

unread,
Dec 30, 2021, 2:15:13 AM12/30/21
to Harbour Users
Hi Diego

I did the following:

Downloaded your binary zip containing two .so and placed them alonside client.prg in hbdocs.
Copies harbour/include/hbdyn.ch in and as  /usr/include/harbour/hbdyn.ch
#define SESSION_PATH "/home/bitnami/develop/whatsapi/" // the path as in index.js
Changed MySQL parameters with installed MySQL configuration.
Naviagted in the browser as http://100.??.??.???/client.html
Browser displayed the attached screen...but without any QrCode.

I am still confused. Who will create Qrcode.png - index.js or client.html ?


Pritpal Bedi
abcdef.png

Diego F

unread,
Dec 30, 2021, 5:37:35 AM12/30/21
to Harbour Users
1) index.js does: 
a) Create Qr if not session.json. Once session is started qrcode.png is deleted and Session.json created
b) if Session.json exist, use session information to reuse session.
c) Every X time read db( whatsapp_out), if new rec( with status == 0 ) send message. And set status->1
d) Check If new message is received and add it to db( whatsapp_in ) with status == 0

2)  client.html
a) Call every X time to client.prg to check status of server and any new message
b) send data msg data to client.prg

3)  client.prg( When client.html post )
a) Check status of server. Any qrcode.png(Session not active) file? send it to client.html to be scanned 
b) Check status of server. Any Session.json(Session active) Let client.html send/receive message
c) If Session active, check new messages( whatsapp_in with status == 0 ) If new, send message to client.html
d) If received new msg action from client.html, add new record( whatsapp_out  with status == 0). At this point, index.js will detect it and send message from the api. 

The logic from the 2) and 3) can be done all from harbour.
If qrcode.png exist -> show to user to scan it. Wait for session.json. If exist session is started. Then make a server with a while to check db( whatsapp_in )( all rec with status == 0 ). Read it and set status -> 1.
A gui to get phone number and message data and then add new rec to whatsapp_out(With status == 0) Index.js when send it will set status -> 1

Index.js will do the rest. 

Diego.

Pritpal Bedi

unread,
Dec 30, 2021, 12:03:42 PM12/30/21
to Harbour Users
Hi Diego

This is exactly what I understood from the very begining but have failed at the very first point whech lead me to confusion.

Very first action of index.js is to create a qrcode.png if session.json is not present. It is not happening. So what could be the reason ? This needs to be resolved. Any tips ?


Pritpal Bedi

Diego F

unread,
Dec 30, 2021, 12:10:07 PM12/30/21
to Harbour Users
Pritpal, If the console after starting index.js says "connection established", without any error, the QR file must have been created. Maybe a permission issue.

Diego.

Pritpal Bedi

unread,
Dec 30, 2021, 12:39:50 PM12/30/21
to Harbour Users
Hi Diego

'Connection established' is displayed but this belongs to mysql connection. 
//
con.connect((err) => { if (err) { console.log('Error connecting to Db'); process.exit(); } console.log('Connection established'); });
And Qrcode.png is created here inside "on" event which is never reached. I put a console.log("ON - qr'); which never displays
//
client1.on('qr', qr => { qrcode_file.toFile(QR_CODE, qr); });
It means client1.initialize() is never successful. Seems I do not have proper installation of npm somehow. 
Can you provide proper steps to install nodejs and npm on a fresh Ubuntu 20.04 server. I use AWS Lightsail server for this purpose. I believe I am missing some important piece of installation.

Pritpal Bedi

Diego F

unread,
Dec 30, 2021, 12:48:55 PM12/30/21
to Harbour Users
Pritpal: 

NodeJS v12 or higher is required
  1. Run inside folder created -> npm init -y
  2. Run inside folder created -> npm install whatsapp-web.js qrcode mysql

did you follow the steps?

Diego.

Pritpal Bedi

unread,
Dec 30, 2021, 1:06:45 PM12/30/21
to Harbour Users
Hi

I followed all what I googled only then node index.js could work. My knowledge about npm, node is only two days old so I might have erred. I did not find any link where instructions to install both components exist. If you have any links please forward. I will redo the whole process again on brand new server.


bitnami@ip-172-26-67-53:~/develop/whatsapi$ npm version
{
  whatsapi: '1.0.0',
  npm: '6.14.15',
  ares: '1.17.2',
  brotli: '1.0.9',
  cldr: '37.0',
  http_parser: '2.9.4',
  icu: '67.1',
  llhttp: '2.1.4',
  modules: '72',
  napi: '8',
  nghttp2: '1.41.0',
  node: '12.22.7',
  openssl: '1.1.1l',
  tz: '2019c',
  unicode: '13.0',
  uv: '1.40.0',
  v8: '7.8.279.23-node.56',
  zlib: '1.2.11'
}

bitnami@ip-172-26-67-53:~/develop/whatsapi$ node -v
v12.22.7
bitnami@ip-172-26-67-53:~/develop/whatsapi$ npx -v
6.14.15

Pritpal Bedi

Diego F

unread,
Jan 2, 2022, 5:24:59 PM1/2/22
to Harbour Users
Pritpal, this week I will try from linux and see what happend.

Diego

jb

unread,
Jan 3, 2022, 2:10:35 AM1/3/22
to Harbour Users
I have created Dockerfile for start and run whatsapp-web.js on Docker. Put attached files into new empty dir, change parameters in start.sh a run it. QRcode hould be generated and accesible in dir ./qrcode/
I am not using client.{html.prg} I read and write to tables whatsapp_(out,in) directly.

happy new year

Dne neděle 2. ledna 2022 v 23:24:59 UTC+1 uživatel Diego F napsal:
start.sh
Dockerfile

jb

unread,
Jan 3, 2022, 3:08:40 AM1/3/22
to Harbour Users
corrected file start.sh

Dne pondělí 3. ledna 2022 v 8:10:35 UTC+1 uživatel jb napsal:
start.sh

Diego F

unread,
Jan 3, 2022, 5:24:00 AM1/3/22
to Harbour Users
Great! 

Diego.

Pritpal Bedi

unread,
Jan 3, 2022, 3:55:21 PM1/3/22
to Harbour Users
Hi JB

You have opened another path to explore, Dockers. Is there an equivalent for Ububtu machine? I will try to explore your scripts, which I believe is what is complete it in itself on Docker on AWS Lightsail instance and will see if I succeed. I believe some package is missing on my Ubuntu installation. 

Pritpal Bedi
a student of software analysis & concepts

jb

unread,
Jan 4, 2022, 5:01:18 AM1/4/22
to Harbour Users
HI,
It is tested on docker on local machine with Manjaro and CentOS7 and it should run on Ubuntu as well

JB


Dne pondělí 3. ledna 2022 v 21:55:21 UTC+1 uživatel bedipr...@gmail.com napsal:

Pritpal Bedi

unread,
Jan 4, 2022, 12:10:40 PM1/4/22
to Harbour Users
Hi JB

I did the following.

1 Initiated an AWS Lightsail instance with Ubuntu 20.04 image and logged with "ubuntu" user as this is the only method to access the instance via putty and landed in /home/ubuntu (~) folder.
2 Applied these commands - sudo apt update and sudo apt upgrade.
3 Installed docker as per these instructions - https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04 - and succeeded all aprompts.
4 Installed MySQL as per these instructions - https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-20-04 - and succeeded.
5 Copied your scripts as i-env and Dockerfile in ubuntu (user) home, i.e., ~ or /home/ubuntu/.
6 First I did not change anything in your scripts except mysql#admin user/pass and executed - sudo ./i-env 
7 It cloned hbwhatapp git repository, unzipped imported db.zip, setup database in mysql properly.
8 It wanrned that docker image bot_wa image is not present for docker commands which is correct at this stage.
9 The problem arose in docker buid operation in Dockerfile - completes every step but at the end says :

Successfully built dcb5135760b2
Successfully tagged bot_wa:latest
Starting container
4e6113905760eb13b8825bc1232d14e9c3a8dcce150a171559e3d04f5e514655
Error: No such container: bot_wa
Error: No such container: bot_wa

And when I explore I do not find any /home/bot_wa folder or anything such folder im /home/ubuntu/.

ubuntu@ip-172-26-76-252:~$
ubuntu@ip-172-26-76-252:~$ ls /home
ubuntu
ubuntu@ip-172-26-76-252:~$
ubuntu@ip-172-26-76-252:~$ ls /home -last
total 12
4 drwxr-xr-x 10 ubuntu ubuntu 4096 Jan  4 16:45 ubuntu
4 drwxr-xr-x  3 root   root   4096 Dec 31 00:50 .
4 drwxr-xr-x 19 root   root   4096 Dec 31 00:50 ..
ubuntu@ip-172-26-76-252:~$
ubuntu@ip-172-26-76-252:~$
ubuntu@ip-172-26-76-252:~$ ls
Dockerfile  d-whatsapp  develop  hbwhatsapi  nodesource_setup.sh  qrcode  start.sh
ubuntu@ip-172-26-76-252:~$
ubuntu@ip-172-26-76-252:~$

Log file is attached. Looking for tips what is missing.

Thanks for everything...

Pritpal Bedi
a student of software analysis & concepts



bot_wa_logs.txt

Pritpal Bedi

unread,
Jan 4, 2022, 12:13:20 PM1/4/22
to Harbour Users
Correction 

5 Copied your scripts as i-env and Dockerfile in ubuntu (user) home, i.e., ~ or /home/ubuntu/.
=>
5 Copied your start.sh, chmod +x start.sh, and run as # sudo ./start.sh while staying in ~.

Pritpal Bedi

Pritpal Bedi

unread,
Jan 4, 2022, 12:59:16 PM1/4/22
to Harbour Users
Then I run start.sh ( after chmod -x start.sh ) as 
# sudo /bin/sh start.sh
But with the same results as before with only one difference 

Starting container id was replaced by new one.
4e6113905760eb13b8825bc1232d14e9c3a8dcce150a171559e3d04f5e514655
=>
5eaebe5ead1bc0e41734469aaae113c0e847c1d17a96bd69e6de773128c30621

Pritpal Bedi

jb

unread,
Jan 4, 2022, 1:16:03 PM1/4/22
to Harbour Users
it looks promising, but I don't use cloud servers, so I can't advise specifically. Im using linux on local machine/servers. For running locally you need runig docker and mysql

remove set old_passwords=0; from start.sh ( regarding Unknown system variable 'old_passwords' from your logfile)

It looks like docker image is build (docker build . --no-cache -t bot_wa) successfully and containers starts (docker run --name bot_wa --user bot_wa --rm -v `pwd`/qrcode:/home/bot_wa/app/qrcode -d bot_wa) successfuly too, its name should be bot_wa
your log:
Successfully built dcb5135760b2
Successfully tagged bot_wa:latest
Starting container 4e6113905760eb13b8825bc1232d14e9c3a8dcce150a171559e3d04f5e514655

In script start.sh is informative messages from running contaner ;sleep 2;docker logs bot_wa;sleep 2;docker logs bot_wa and and output is
Error: No such container: bot_wa (from your log) I do not know why, maybe is not started yet or has another name. You can try with container id from your log
docker logs 4e6113905760eb13b8825bc1232d14e9c3a8dcce150a171559e3d04f5e514655


About /home/bot_wa it is in directory in running container noy your shell. If is container running you can try log into it by
docker exec -it  contaner/nameOrID bash
docker exec -it bot_wa bash   or
docker exec -it  4e6113905760eb13b8825bc1232d14e9c3a8dcce150a171559e3d04f5e514655 bash

you can try start container from image created
docker run --name bot_wa --user bot_wa -v `pwd`/qrcode:/home/bot_wa/app/qrcode -d bot_wa


if container start successfully, you should see qrcode.png in directory ./qrcode, so what do you see in it:
ls qrcode

Dne úterý 4. ledna 2022 v 18:59:16 UTC+1 uživatel bedipr...@gmail.com napsal:

Pritpal Bedi

unread,
Jan 4, 2022, 1:34:56 PM1/4/22
to Harbour Users
Seems run command executed, not sure though, since it only showed another big hash:

ubuntu@ip-172-26-76-252:~$ sudo docker run --name bot_wa --user bot_wa -v `pwd`./qrcode:/home/bot_wa/app/qrcode -d bot_wa
79acc28272f60bc88b16df162ed5660cdd3d5d1169b91a6d7f64457185005242
ubuntu@ip-172-26-76-252:~$
ubuntu@ip-172-26-76-252:~$ ls qrcode
ubuntu@ip-172-26-76-252:~$

I had put a . before 'pwd'/qrcode in the above command  as 'pwd'./qrcode so seems qrcode.png did not show up here.
Then I run the container again without the '.' and saw it

ubuntu@ip-172-26-76-252:~$ sudo docker run --name bot_wa --user bot_wa -v `pwd`/qrcode:/home/bot_wa/app/qrcode -d bot_wa
docker: Error response from daemon: Conflict. The container name "/bot_wa" is already in use by container "79acc28272f60bc88b16df162ed5660cdd3d5d1169b91a6d7f64457185005242". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
ubuntu@ip-172-26-76-252:~$

So seems bot_wa is in use and running. How to stop and run it again with corrected command without 'added dot?

jb

unread,
Jan 4, 2022, 1:39:35 PM1/4/22
to Harbour Users
docker stop bot_wa;docker rm bot_wa;
and then againg build new image and run it again with  start.sh (there are thouse commads too - docker stop bot_wa;docker rm bot_wa)
and remove --rm in start.sh in (command docker run --name bot_wa --user bot_wa --rm -v `pwd`/qrcode:/home/bot_wa/app/qrcode -d bot_wa) so you can inspect log for dead contamnier too (is not destroyed)

Dne úterý 4. ledna 2022 v 19:34:56 UTC+1 uživatel bedipr...@gmail.com napsal:

jb

unread,
Jan 4, 2022, 1:45:43 PM1/4/22
to Harbour Users
you can replace `pwd` with your starting directory - maybe /home/ubuntu - (`pwd` shold be expanded to it)

Dne úterý 4. ledna 2022 v 19:39:35 UTC+1 uživatel jb napsal:

jb

unread,
Jan 4, 2022, 1:58:28 PM1/4/22
to Harbour Users
for repeated image generation it is advisable to remove remove --no-cache from command
docker build . --no-cache -t bot_wa



Dne úterý 4. ledna 2022 v 19:45:43 UTC+1 uživatel jb napsal:

Pritpal Bedi

unread,
Jan 4, 2022, 1:59:00 PM1/4/22
to Harbour Users
Rebuit last lines there...


Removing intermediate container 04bccbaa8815
 ---> 420341fe0387
Successfully built 420341fe0387

Successfully tagged bot_wa:latest
Starting container
a1dec0b4bde749a562d61677dd2594397ccb10730f22485ebfff737bcf922770

Error: No such container: bot_wa
Error: No such container: bot_wa
ubuntu@ip-172-26-76-252:~$

ubuntu@ip-172-26-76-252:~$ sudo docker run --name bot_wa --user bot_wa -v `pwd`/qrcode:/home/bot_wa/app/qrcode -d bot_wa
7a031dcab2187d7b117212f6254d177b0b1c483d36b0cedead7317556341791c

ubuntu@ip-172-26-76-252:~$
ubuntu@ip-172-26-76-252:~$ ls qrcode
ubuntu@ip-172-26-76-252:~$
ubuntu@ip-172-26-76-252:~$


qrcode folder contains nothing.

Pritpal Bedi

unread,
Jan 4, 2022, 2:08:11 PM1/4/22
to Harbour Users
And again 


ubuntu@ip-172-26-76-252:~$ sudo docker stop bot_wa
bot_wa
ubuntu@ip-172-26-76-252:~$ sudo docker rm bot_wa
bot_wa
ubuntu@ip-172-26-76-252:~$ sudo docker run --name bot_wa --user bot_wa -v /home/ubuntu/qrcode:/home/bot_wa/app/qrcode -d bot_wa
78c3b164e141b255f5d3fb24f06bd365e2c93a8c136da6ddee493087bec778e5

ubuntu@ip-172-26-76-252:~$
ubuntu@ip-172-26-76-252:~$ ls qrcode
ubuntu@ip-172-26-76-252:~$
Message has been deleted

jb

unread,
Jan 4, 2022, 2:25:26 PM1/4/22
to Harbour Users
after changes in Dockerfile or dependend files you should build image again
docker build . -t bot_wa
and run container from thi image
docker run --name bot_wa --user bot_wa --rm -v ./qrcode:/home/bot_wa/app/qrcode -d bot_wa

and what shows
docker logs 78c3b164e141b255f5d3fb24f06bd365e2c93a8c136da6ddee493087bec778e5
?

parameters -v `pwd`/qrcode:/home/bot_wa/app/qrcodeyour hist directory
staring dir/qrcode to contanuners dir :/home/bot_wa/app/qrcode





Dne úterý 4. ledna 2022 v 20:08:11 UTC+1 uživatel bedipr...@gmail.com napsal:

Pritpal Bedi

unread,
Jan 4, 2022, 2:44:34 PM1/4/22
to Harbour Users
After commenting out clone hbwhatapi  and changing process:exit() in index.js I get the following where 'Connection established' though not in real-time is correct and app is waiting for event 'qr' which never arrives.

ubuntu@ip-172-26-76-252:~$ sudo docker run --name bot_wa --user bot_wa -v /home/ubuntu/qrcode:/home/bot_wa/app/qrcode -d bot_wa
e8924a188825927faa0f4d3733eade14905dc9c19239041c2507215a3b252f10
ubuntu@ip-172-26-76-252:~$
ubuntu@ip-172-26-76-252:~$ sudo docker logs e8924a188825927faa0f4d3733eade14905dc9c19239041c2507215a3b252f10
Error connecting to Db
Connection established
ubuntu@ip-172-26-76-252:~$

Attached is the htop image which shows that application is up and waiting in loop.

bot_wa_app.png

Pritpal Bedi

unread,
Jan 4, 2022, 2:48:09 PM1/4/22
to Harbour Users
Wow, I got qrcode.png

ubuntu@ip-172-26-76-252:~$ ls qrcode
qrcode.png
ubuntu@ip-172-26-76-252:~$

So error was in mysql connection. Will look forward to fix it. Now I have a feel of docker also. Thanks JB for this entire session.

Can you brief how you are displaying qrcode.png so that phone can scan it ?


Regards
Pritpal Bedi
a student of software analysis and concepts

Angel Pais

unread,
Jan 4, 2022, 3:57:04 PM1/4/22
to harbou...@googlegroups.com
You can use whatever program: Paint, Chrome etc to display it and scan from your phone.

--
--
You received this message because you are subscribed to the Google
Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: http://groups.google.com/group/harbour-users

---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/harbour-users/f8d2300f-bc17-4b19-8a3e-b880f6e13880n%40googlegroups.com.

Pritpal Bedi

unread,
Jan 4, 2022, 4:19:48 PM1/4/22
to Harbour Users
Come on Angel.
I am asking how it can be viewed from docker instance remotely.

Pritpal Bedi

Angel Pais

unread,
Jan 4, 2022, 4:29:36 PM1/4/22
to harbou...@googlegroups.com
Come on Pritpal !!!!
Have you ever used web whatsapp ?
If so. What is the purpose of the QR code?
Validate you mobile phone. So it needs to be scanned from a mobile phone.
How do you display it ? i
ndex.js has nothing to do with the validation of your device-. It only maintains 
the session and interacts with the mysql logs.

Angel Pais

unread,
Jan 4, 2022, 5:18:01 PM1/4/22
to harbou...@googlegroups.com
Let me be more clear.
To expose the .png to your mobile phone you need a web server  ( we suggested apache + hw_apache ) or to copy it manually to elsewhere.
Once you scan the qrcode, it remains valid as far as index.js session is alive.

Pritpal Bedi

unread,
Jan 4, 2022, 5:48:53 PM1/4/22
to Harbour Users
Yah, there are many forms to display. I used to download via scp and then dispayed it just by clicking as windows does usually. But I was wondering how things can be transparent to your clients. Me, personally, will not be using this feature in my commercial apps but the end user. I was wondering there is another procol to achieve this as I will be interactinging with a container service.

BTW, I displayed it on windows and scanning through whatsapp#link device option did the job. It recognized and linked the device. 

@ JB - your solution worked alsonwith Diago's lib. Thanks to both of you. Rest is onto the developers how they build the solution for their end-users.


Pritpal Bedi
a student of software analysis & concepts


Angel Pais

unread,
Jan 4, 2022, 5:57:28 PM1/4/22
to harbou...@googlegroups.com
IMHO the best is a web server + a simple page.

jb

unread,
Jan 5, 2022, 12:37:31 AM1/5/22
to Harbour Users
I improved start.sh and Dockerfile, now it saves the qrcode and also shows (docker logs bot_wa)

Dne pondělí 3. ledna 2022 v 9:08:40 UTC+1 uživatel jb napsal:
Dockerfile
start.sh

jb

unread,
Jan 5, 2022, 12:46:14 AM1/5/22
to Harbour Users
the error was probably caused by the command
set old_passwords=0 the rest of the commands on this line were not executed

regarding qrcode display: qrcode.png is exposed from docker in the host directory, so it is accessible. it is on my local machine, so I can only view it in the browser using the url file:///PathToQRCode/qrcode.png
above I have sent new files (start.sh and Dockerfile) that display the qrcode using the qrcode-terminal. the qrcode is probably generated every few seconds, so after displaying the qrcode (docker logs bot_wa) it should be scanned as soon as possible.

Dne úterý 4. ledna 2022 v 20:48:09 UTC+1 uživatel bedipr...@gmail.com napsal:

Diego F

unread,
Jan 5, 2022, 5:44:10 AM1/5/22
to Harbour Users
Hi,  Another way to read qr using a simple way is to create another db (status) with a single record which contains the qr code. Index.js if the qr exists, it upload to db, if not, it would leave it empty. Another useful field would be a "status". From hb it would do a ping type and put stat -> 1. Then index.js would be in charge of verifying if star == 1. If so, it would put stat -> 0. This would indicate that index.js is active. It's just an idea....No hw_apache/modharbour. Just harbour with mysql.... :)

Diego.

Pritpal Bedi

unread,
Jan 6, 2022, 1:47:13 AM1/6/22
to Harbour Users
Hi JB

I have made these changes in start.sh which creates the user, assign the privileges etc without errors. The syntax of various versions of MySQL is different somehow. At least on MySQL 8.0.27 for Ubuntu respect the following.

echo "------ Creating user ------"
echo "drop user '${USER}'@'localhost';" |mysql -p${DBADMIN_PASSWORD} -u${DBADMIN_USER} -h${HOST}
echo "create user '${USER}'@'localhost' identified by '${PASSWORD}';" |mysql -p${DBADMIN_PASSWORD} -u${DBADMIN_USER} -h${HOST}
echo "------ Granting all privileges ------"
echo "grant all privileges on ${DB}.* to '${USER}'@'localhost';" |mysql -p${DBADMIN_PASSWORD} -u${DBADMIN_USER} -h${HOST}
#echo "grant all privileges on ${DB}.* to ${USER} identified by '${PASSWORD}';" |mysql -p${DBADMIN_PASSWORD} -u${DBADMIN_USER} -h${HOST}
echo "------ Flushing privileges ------"
echo "flush privileges;" |mysql -p${DBADMIN_PASSWORD} -u${DBADMIN_USER} -h${HOST}

After removing --no-cache option building a container and starting is extremely fast, thanks for new start.sh.

Now the last hurdle - caoonot connect to database from index.js

Successfully tagged bot_wa:latest
------ Starting container ------
e6cf84546a9cd3f46cc794bb494171651e765212b6882367cd4f0e61854ceb87
Error connecting to Db
exit

Still unable to resolve this issue. Maybe HOST=localhost is not recognized from within container, not sure though.
On the machine I can connect to mysql via same user/pass which is in index.js.

Any tips?

Pritpal Bedi
a student of software analysis & concepts


Message has been deleted

jb

unread,
Jan 6, 2022, 3:14:41 AM1/6/22
to Harbour Users
Hi,

yes, localhost is not correct, try using the host ip address. ( You can get the ip of the containers using docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{{end}}' )

You can jump into the container using docker exec -it bot_wa bash and try to ping the host ip to verify the connection

JB

Dne čtvrtek 6. ledna 2022 v 7:47:13 UTC+1 uživatel bedipr...@gmail.com napsal:

Pritpal Bedi

unread,
Jan 6, 2022, 2:53:19 PM1/6/22
to Harbour Users
Hi JB

I changed index.js as to not exit the app:

con.connect((err) => {
   if (err) {
      console.log('Error connecting to Db',err);
      //process.exit();

   }
   console.log('Connection established');
});

which resulted to continue execution of index.js than after wait of 20 seconds I got these results.

index-js-run.png

qrcode was received and dispayed on the console as you modified the start.sh but then exception occured as below and index.js terminated.

exit
node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

[Error: ENOENT: no such file or directory, open './qrcode/qrcode.png'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: './qrcode/qrcode.png'
}

Node.js v17.3.0
ubuntu@ip-172-26-76-252:~$



Seems saving the displayed qrcode as disk .png failed.


Pritpal Bedi

Pritpal Bedi

unread,
Jan 6, 2022, 3:28:47 PM1/6/22
to Harbour Users
Then I followed your suggession and pinged the localhost and servers private ip and it succeeded.


ubuntu@ip-172-26-76-252:~$ sudo docker exec -it bot_wa bash
bash-4.2$
bash-4.2$
bash-4.2$ ping localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.035 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.039 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.040 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.040 ms
64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.039 ms
64 bytes from localhost (127.0.0.1): icmp_seq=6 ttl=64 time=0.039 ms
^C
--- localhost ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5097ms
rtt min/avg/max/mdev = 0.035/0.038/0.040/0.007 ms
bash-4.2$
bash-4.2$ ping 172.26.76.252
PING 172.26.76.252 (172.26.76.252) 56(84) bytes of data.
64 bytes from 172.26.76.252: icmp_seq=1 ttl=64 time=0.046 ms
64 bytes from 172.26.76.252: icmp_seq=2 ttl=64 time=0.073 ms
64 bytes from 172.26.76.252: icmp_seq=3 ttl=64 time=0.064 ms
^C
--- 172.26.76.252 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2035ms
rtt min/avg/max/mdev = 0.046/0.061/0.073/0.011 ms
bash-4.2$

Pritpal Bedi

unread,
Jan 6, 2022, 3:54:01 PM1/6/22
to Harbour Users
Then I tried like this :

docker stop bot_wa
docker rm bot_wa

echo "------ Bulding docker image ------"

docker build . -t bot_wa

echo "------ Starting container ------"
docker run --name bot_wa --user bot_wa --network="host"  -v `pwd`/qrcode:/home/ubuntu/bot_wa/app/qrcode -d bot_wa
                                                                        ^^^^^^^^^^^^^^^^^ https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach
docker logs bot_wa
sleep 20
#should display qrcode
docker logs bot_wa


And received this error for database connection:

ubuntu@ip-172-26-76-252:~$ sudo docker logs bot_wa
Error connecting to Db Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
    at Handshake.Sequence._packetToError (/home/bot_wa/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)
    at Handshake.ErrorPacket (/home/bot_wa/app/node_modules/mysql/lib/protocol/sequences/Handshake.js:123:18)
    at Protocol._parsePacket (/home/bot_wa/app/node_modules/mysql/lib/protocol/Protocol.js:291:23)
    at Parser._parsePacket (/home/bot_wa/app/node_modules/mysql/lib/protocol/Parser.js:433:10)
    at Parser.write (/home/bot_wa/app/node_modules/mysql/lib/protocol/Parser.js:43:10)
    at Protocol.write (/home/bot_wa/app/node_modules/mysql/lib/protocol/Protocol.js:38:16)
    at Socket.<anonymous> (/home/bot_wa/app/node_modules/mysql/lib/Connection.js:88:28)
    at Socket.<anonymous> (/home/bot_wa/app/node_modules/mysql/lib/Connection.js:526:10)
    at Socket.emit (node:events:390:28)
    at addChunk (node:internal/streams/readable:324:12)
    --------------------
    at Protocol._enqueue (/home/bot_wa/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/home/bot_wa/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at Connection.connect (/home/bot_wa/app/node_modules/mysql/lib/Connection.js:116:18)
    at Object.<anonymous> (/home/bot_wa/app/index.js:11:5)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'ER_NOT_SUPPORTED_AUTH_MODE',
  errno: 1251,
  sqlMessage: 'Client does not support authentication protocol requested by server; consider upgrading MySQL client',
  sqlState: '08004',
  fatal: true
}


But I could scan the qtcode displayed on the terminal and received session.json but which could not been saved like :

index-js-run-2.png

Pritpal Bedi

unread,
Jan 6, 2022, 3:59:03 PM1/6/22
to Harbour Users
Now there remain two issues:

1. Connection to database is failing - latest error as above ( after --network="host" option in docker run ) suggests that MySQL on host needs to be compatible through older versions comaptible mode - looking at.

2. ./qrcode/ folder seems inaccessible for write operatopns - could not save qrcode.png as well as received session.json.


Pritpal Bedi
a student of software analysis & concepts


Pritpal Bedi

unread,
Jan 6, 2022, 5:57:33 PM1/6/22
to Harbour Users
#1 issue os resolved by changing this line in start.sh:

echo "create user '${USER}'@'localhost' identified with mysql_native_password by '${PASSWORD}';" |mysql -p${DBADMIN_PASSWORD} -u${DBADMIN_USER} -h${HOST}
                                                                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

#2 issue is strange since initially we were able to save in the folder. Working on ....

Pritpal Bedi

Pritpal Bedi

unread,
Jan 6, 2022, 7:41:09 PM1/6/22
to Harbour Users
SOLVED - #2 issue -  I was making a mistake

`pwd`/qrcode:/home/bot_wa/app/qrcode - should have been and is correct - the original JB publisj=hed.
`pwd`/qrcode:/home/ubuntu/bot_wa/app/qrcode - what I was playing with and is wrong

Now another issue. I am saving session.json and provising on another run in session = require(SESSION_FILE_PATH); but still qrcode is received which conceptually must not as session.json is supplied. Any tip why it fails ?


Pritpal Bedi

jb

unread,
Jan 7, 2022, 1:27:44 AM1/7/22
to Harbour Users
Hi,
in container started without --network="host" you should use your host ip ( it is probably 172.26.76.252 as can be seen from prompt ip-172-26-76-252 ):
HOST=172.26.76.252

restoring session:
there is a bug in sed in start.sh, line session: sessionData, is lost, correct version:
#workaround no usable sandbox
sed -i "s/session: sessionData,/session: sessionData,\n puppeteer: {\n headless: true,\n args: [\n \"--no-sandbox\",\n \"--disable-setuid-sandbox\",\n \"--unhandled-rejections=strict\"\n ]\n },/" index.js

JB



Dne pátek 7. ledna 2022 v 1:41:09 UTC+1 uživatel bedipr...@gmail.com napsal:

Pritpal Bedi

unread,
Jan 7, 2022, 1:37:13 PM1/7/22
to Harbour Users
Thanks JB

I already figured it out and corrected sed command. I will try with HOST=InternalIPofServer and will publish the results.

Thanks a lot for all this trouble but worth it for Harbour community.

Pritpal Bedi
a student of software analysis & concepts

Pritpal Bedi

unread,
Jan 7, 2022, 5:11:06 PM1/7/22
to Harbour Users
Hi All

Few tweaks in the JB provided start.sh which now builds and runs docker image to consume Diago's hbwhatsapi implementation without issues. I believe this protocol can be extended to multiple clients being managed by single instance which I will be persuing in near future. 

I have also wrote this function which, I believe, should be in index.js as it leverages to test the implementation in easy way and from the command-line, without first depositing in mysql.
// Add these lines at the end of index.js

const MSG_FILE_PATH = cPath + 'msg.json';

function checkDiskMessage() {
   if (fs.existsSync(MSG_FILE_PATH)) {

      var data = require( MSG_FILE_PATH );
      if (data){
         if (connected) {
            connected = false;
            client1.sendMessage(data.to + "@c.us", data.msg)
               .then(response => {
                  if (response.id.fromMe) {
                     console.log('Mensaje enviado:', data.msg);
                  }
            });
            console.log('Sent - ', data.to + '  ' + data.msg);
            fs.unlink(MSG_FILE_PATH, function (err) {
               console.log(MSG_FILE_PATH, 'Could not been removed' );
            });
            connected = true;
         };
      };
   };
};

intervalid = setInterval(checkDiskMessage, 31000);

Than  store this msg.json in ./qrcode folder:
{ "to": "client_phone_number_withou_+_sign", "msg": "From Harbour Whatsapp Bot - Complete Implementation !" }
          ^^^^  919876554321 ^^^^^^^^^^^^^^^^^^

@JB 
Host IP does not work and it should not be used. --networ="host" is considered unsecure but the protocol above works without any additional configurations. 

Also instead of MySQL deposited messages, we can use pure text file for the purpose further eliminating the dependancies. But if you managing every client separately the above method is more than sufficient. Once session.json is received it should be stored somewhere persistently and used for next connection. Index.js should not attempt to delete them otherwise every time same phone is used it will need to be scan the qrcode again.


Thanks Diego and JB for your works.

Pritpal Bedi
a student of software analysis & concepts

hb-start.sh
Reply all
Reply to author
Forward
0 new messages