How to send WhatsApp messages to phones using PHP

The recommended method of customizing your AppGini-generated application is through hooks. But sometimes you might need to add functionality not accessible through hooks. You can discuss this here.
Post Reply
User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

How to send WhatsApp messages to phones using PHP

Post by jsetzer » 2020-05-25 15:30

Hi AppGineers,

can anyone of you tell, how to send text notifications to WhatsApp phone numbers using PHP?

chrome_afCi9P5qnn.png
chrome_afCi9P5qnn.png (2.66 KiB) Viewed 13406 times

I'd like to notify users about certain tasks or status changes. I can store their phone numbers in AppGini and [x] check if they want to be informed or not. It is just about notification; there is no need for processing any responses.

I have read there is no WhatsApp API any longer and it seems we need to contract with an official gateway provider like TWILIO. Can anyone please share her/his experiences.

Thanks in advance!

Regards,
Jan
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

G Belgrado
Veteran Member
Posts: 61
Joined: 2017-03-12 09:24

Re: How to send WhatsApp messages to phones using PHP

Post by G Belgrado » 2020-05-25 16:13

Hi Jan

there would be this for whatsapp API

API WhatsApp Business

but it's not free

User avatar
D Oliveira
AppGini Super Hero
AppGini Super Hero
Posts: 347
Joined: 2018-03-04 09:30
Location: David

Re: How to send WhatsApp messages to phones using PHP

Post by D Oliveira » 2020-05-26 04:46

Hey Jan, I have something that could help you, mobile version will use native whatsapp and desktop version will use whatsapp web, hope it helps =)

Code: Select all


     <div>
        <button onclick="share();" style="background-color:rgba(0,0,0,0.4);border-top-left-radius: 5px;border-bottom-left-radius: 5px;border-top-right-radius: 5px;border-bottom-right-radius: 5px;color:white;font-size:20px;font-family:Gotham2;border:0.1px solid black;padding: 5px 10px 7px;">
      &nbsp;Share via Whatsapp&nbsp;</button>
     </div>
     
    <div>
   <img src="spacer.png?c=91" style="width:100%;height:50px;">        
    </div>
        
    
        
        
            <?php
            
             function isMobile() {
         return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
        }
            
            
            if(isMobile()){
            ?>
            <script>
            
            function share(){
            LinkTextToShare = 'https://api.whatsapp.com/send?text=' + encodeURIComponent(window.location.href) ;

            popup_window = window.open(LinkTextToShare,"_blank");
            
            //setInterval(function(){ popup_window.close (); }, 1000);
            
            popup_window.close ();

            }
            </script>
            
            <?php }else{
            ?>
           
           <script>
           function share(){
            LinkTextToShare = 'https://web.whatsapp.com/send?l=en&text=' + encodeURIComponent(window.location.href) ;

            popup_window = window.open(LinkTextToShare,"_blank");

           }
           </script>
            <?php }
            ?>
            
            
              <img src="spacer.png?c=91" style="width:100%;height:30px;">


User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: How to send WhatsApp messages to phones using PHP

Post by jsetzer » 2020-05-26 04:51

G Belgrado wrote:
2020-05-25 16:13
Hi Jan

there would be this for whatsapp API

API WhatsApp Business

but it's not free
Hi,

did you manage to use it according to the docs?

I have already seen that documentation page before. They are talking about a registration, but there is not even a "login" or "register" link on the page they are refering to.

Thanks anyway!
Jan
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: How to send WhatsApp messages to phones using PHP

Post by jsetzer » 2020-05-26 04:58

D Oliveira wrote:
2020-05-26 04:46
Hey Jan, I have something that could help you, mobile version will use native whatsapp and desktop version will use whatsapp web, hope it helps =)

Code: Select all


     <div>
        <button onclick="share();" style="background-color:rgba(0,0,0,0.4);border-top-left-radius: 5px;border-bottom-left-radius: 5px;border-top-right-radius: 5px;border-bottom-right-radius: 5px;color:white;font-size:20px;font-family:Gotham2;border:0.1px solid black;padding: 5px 10px 7px;">
      &nbsp;Share via Whatsapp&nbsp;</button>
     </div>
     
    <div>
   <img src="spacer.png?c=91" style="width:100%;height:50px;">        
    </div>
        
    
        
        
            <?php
            
             function isMobile() {
         return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
        }
            
            
            if(isMobile()){
            ?>
            <script>
            
            function share(){
            LinkTextToShare = 'https://api.whatsapp.com/send?text=' + encodeURIComponent(window.location.href) ;

            popup_window = window.open(LinkTextToShare,"_blank");
            
            //setInterval(function(){ popup_window.close (); }, 1000);
            
            popup_window.close ();

            }
            </script>
            
            <?php }else{
            ?>
           
           <script>
           function share(){
            LinkTextToShare = 'https://web.whatsapp.com/send?l=en&text=' + encodeURIComponent(window.location.href) ;

            popup_window = window.open(LinkTextToShare,"_blank");

           }
           </script>
            <?php }
            ?>
            
            
              <img src="spacer.png?c=91" style="width:100%;height:30px;">


Hi,

thank you. Unfortunately this is clientside Javascript which requires a WhatsApp installation or an open WhatsAppWeb-session + additional user interaction.

I am looking for a server side PHP solution which I can use for mass sending WhatsApp messages to known phone numbers without user interaction per message.

Thank you both for taking the time!

Regards,
Jan
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
D Oliveira
AppGini Super Hero
AppGini Super Hero
Posts: 347
Joined: 2018-03-04 09:30
Location: David

Re: How to send WhatsApp messages to phones using PHP

Post by D Oliveira » 2020-05-26 22:25

jsetzer wrote:
2020-05-26 04:58
D Oliveira wrote:
2020-05-26 04:46
Hey Jan, I have something that could help you, mobile version will use native whatsapp and desktop version will use whatsapp web, hope it helps =)

Code: Select all


     <div>
        <button onclick="share();" style="background-color:rgba(0,0,0,0.4);border-top-left-radius: 5px;border-bottom-left-radius: 5px;border-top-right-radius: 5px;border-bottom-right-radius: 5px;color:white;font-size:20px;font-family:Gotham2;border:0.1px solid black;padding: 5px 10px 7px;">
      &nbsp;Share via Whatsapp&nbsp;</button>
     </div>
     
    <div>
   <img src="spacer.png?c=91" style="width:100%;height:50px;">        
    </div>
        
    
        
        
            <?php
            
             function isMobile() {
         return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
        }
            
            
            if(isMobile()){
            ?>
            <script>
            
            function share(){
            LinkTextToShare = 'https://api.whatsapp.com/send?text=' + encodeURIComponent(window.location.href) ;

            popup_window = window.open(LinkTextToShare,"_blank");
            
            //setInterval(function(){ popup_window.close (); }, 1000);
            
            popup_window.close ();

            }
            </script>
            
            <?php }else{
            ?>
           
           <script>
           function share(){
            LinkTextToShare = 'https://web.whatsapp.com/send?l=en&text=' + encodeURIComponent(window.location.href) ;

            popup_window = window.open(LinkTextToShare,"_blank");

           }
           </script>
            <?php }
            ?>
            
            
              <img src="spacer.png?c=91" style="width:100%;height:30px;">


Hi,

thank you. Unfortunately this is clientside Javascript which requires a WhatsApp installation or an open WhatsAppWeb-session + additional user interaction.

I am looking for a server side PHP solution which I can use for mass sending WhatsApp messages to known phone numbers without user interaction per message.

Thank you both for taking the time!

Regards,
Jan

sorry for the confusion, maybe those links can help:

https://www.phphive.info/285/how-to-sen ... using-php/

https://www.twilio.com/docs/whatsapp/tu ... atsapp-php

http://www.vetbossel.in/send-and-receiv ... using-php/

Cheers!

User avatar
a.gneady
Site Admin
Posts: 1281
Joined: 2012-09-27 14:46
Contact:

Re: How to send WhatsApp messages to phones using PHP

Post by a.gneady » 2021-02-04 11:00

I know this is an old thread, but I just posted a related recipe to the AppGini customization cookbook on GitHub: Share a record as a WhatsApp message

This is a client-side solution that opens the WhatsApp-web page, with a preselected contact and a pre-written message, ready for the user to click send. For sending automated server-side WhatsApp messages, I guess the only official way is to use the WhatsApp API -- I believe it's a commercial API rather than free.
:idea: AppGini plugins to add more power to your apps:
  • DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
  • Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
  • Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: How to send WhatsApp messages to phones using PHP

Post by jsetzer » 2021-02-04 12:07

Great, this works perfectly for clientside 1:1 communication if the sender has connected her/his smartphone to WhatsApp Web. Thanks for sharing!

Serverside (API) usage: I did not find any free alternative, yet.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: How to send WhatsApp messages to phones using PHP

Post by jsetzer » 2021-02-04 12:19

Tips for text-formatting
  • *bold*
  • _italic_
  • ~strike~
  • ```monospace```
  • you can use `\n` as newline-character
chrome_lwlw5D1lY8.png
chrome_lwlw5D1lY8.png (4.14 KiB) Viewed 11658 times
chrome_gJ2b5mTZjD.png
chrome_gJ2b5mTZjD.png (11.71 KiB) Viewed 11658 times
chrome_VnHwO9pv2n.png
chrome_VnHwO9pv2n.png (4.18 KiB) Viewed 11658 times

Question:
Does anyone know the meaning of app_absent=0 parameter? Is there a parameter documentation somewhere?
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: How to send WhatsApp messages to phones using PHP

Post by onoehring » 2021-02-04 12:52

Hi Jan,

just from curiosity: Why not simply send an email? That mail can be as short as a regular whatsapp message.

(Well, it's always good if we can - I agree)
Just a note from someone who might just be the only one here not using whatsapp ;-)

Olaf

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: How to send WhatsApp messages to phones using PHP

Post by onoehring » 2021-02-04 12:52

Hi,

forgot, maybe, depending on usergroup you could use an instant messenger, based on email: https://delta.chat/

Olaf

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: How to send WhatsApp messages to phones using PHP

Post by jsetzer » 2021-02-04 13:13

@Olaf: In my case (see beginning of this thread) a customer of mine had dozens of workers on construction sites. They are used to receive all organizational information in there specific WhatsApp team-group. So for that use case it is not an option to completely change their usual information-flow. Thanks for the link. I will definately have a look at this.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: How to send WhatsApp messages to phones using PHP

Post by jsetzer » 2021-02-04 13:28

By the way:
Thanks to Ahmed's WhatsApp recipe (see above) I have just included a WhatsApp link in one of my login pages (just as an idea).
chrome_5uYBgEPaih.png
chrome_5uYBgEPaih.png (116.2 KiB) Viewed 11646 times

It allows users to send the URL of that specific website via WhatsApp to others or to themselves. It is a little bit faster and more conveniant than copy&paste.

Other elements inside the "Connect" box:
  • The "paperplane" button opens up your mail client and creates a new mail, already containing the URL as link. So you can send the URL by email with very few clicks.
  • The QR code can be scanned with your smartphone (if it has QR Code scanner integrated or as an installed app) and directly opens the same page.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: How to send WhatsApp messages to phones using PHP

Post by jsetzer » 2021-02-06 11:46

@Olaf:
Once again thank you for sharing the DeltaChat tip for automated sending messages to Smartphones which have installed DeltaChat client.

Unfortunately, I did not get it running, yet, and I would appreciate your help and experiences.

Status quo
  1. As you have suggested, I have installed DeltaChat on my PC and on my Smartphone
  2. I have setup a blank new email account just for testing purposes
  3. I have connected both clients (Desktop + Smartphone) with that email account

    That's fine: I can use those DeltaChat clients on different devices for sending and receiving messages.

Problem

I did not manage to send an e-mail (for example using PHP or by a standard e-mail client) to a DeltaChat client (Desktop or smartphone).

Analysis

When watching the email inbox while sending a message using one of the DeltaChat clients I have seen that DeltaChat uses attachments for data-transfer. They are using PGP (pretty good privacy).

As you can see from the following screenshot, such a message does NOT contain any contents but two attachments:

chrome_cmxdRb6uIQ.png
chrome_cmxdRb6uIQ.png (15.02 KiB) Viewed 11557 times

The .asc file contains PGP-encrypted data.

The .pgp file just contains three lines (two of them are empty):

Code_xsgSrR55HC.png
Code_xsgSrR55HC.png (1.09 KiB) Viewed 11557 times

Right now I have no idea how to encrypt my plain-text or HTML-messages using a PGP key of a recipient if I only have the recipient's email address.

So my question is:
How can I send a message to a DeltaChat client without using a DeltaChat client itself?

Thanks again for your recommendation and support!
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: How to send WhatsApp messages to phones using PHP

Post by onoehring » 2021-02-08 10:01

Hi Jan,

oh well, I did not try this before myself, maybe I am not of a big help here but. I thought as DeltaChat is essentially using email it should be able to receive standard mail....

Looking at the headers I find

Code: Select all

Chat-Version: 1.0
Autocrypt: [email protected]; prefer-encrypt=mutual;
Maybe you need to send the headers along, so that DeltaChat recognizes, that the mail is intended for it. For the second line see specifications here: https://autocrypt.org/examples.html .

Hope this helps .. a little.

Olaf

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: How to send WhatsApp messages to phones using PHP

Post by onoehring » 2021-02-08 10:05

Hi Jan,
maybe write yourself a message from DeltaChat (to your regular email account) and look at this message. Can you reply to that and receive it on DeltaChat? If yes, you should probably be able to take the message apart and send directly (without anyone initiating the "chat" first).
Just a thought.

Olaf

eagle
Veteran Member
Posts: 39
Joined: 2013-01-09 15:38

Re: How to send WhatsApp messages to phones using PHP

Post by eagle » 2021-02-08 15:44

Just wanted to chime in with this related tip.

Would it be possible to convince the customer to use Telegram instead of (or in addition to WhatsApp)? Telegram has a super, free, API that makes it very easy to send messages from PHP. At work we use it to monitor server uptime, mail queues etc and I get a Telegram message whenever anything needs attention. We also have connected it to our CRM system to "ring the bell" whenever new sales contracts are closed.

Eagle.

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: How to send WhatsApp messages to phones using PHP

Post by jsetzer » 2021-02-08 19:18

Thank you for your idea with Telegram API. I will keep it in mind for future projects. In this specific project, we will not be able to completely convert their staff to the use of telegram. WhatsApp has been used there for years in various process-steps, and as a manufacturer of a partial solution I cannot change the process information flow.

Anyway, thanks for the idea. I think it is very valuable that you have already gained experience with this.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

Post Reply