FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour WhatsApp tests
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
WhatsApp tests
Posted: Wed Apr 17, 2024 03:55 AM
Modesto Rocabado has shared with us this code to send messages and files using WhatsApp.

We do appreciate your tests and feedback to fine tune it. Many thanks!
Code (fw): Select all Collapse
FUNC SendToWhatsApp( cPhone, cMsg, aAttach )
LOCAL oShell, aFiles := {}, aOthers  := {}, lRet
 If (lRet := !Empty( cPhone ))
  If !Empty( aAttach )
   If( !HB_ISARRAY( aAttach ), aAttach := { aAttach }, )
   AEval( aAttach, {|cFile| If( HB_ISSTRING( cFile ) .AND. File( cFile ), AAdd( aFiles, cFile), AAdd( aOthers, cFile ) ) } )
  EndIf
  cMsg  := StrTran( cMsg, CRLF, "%0D%0A" )
  oShell := CreateObject( "WScript.Shell" )
  ShellExecute( 0, "Open", "whatsapp://send?phone="+cPhone+"&text="+cMsg)
  SysWait( 0.2 )
  If !Empty( aFiles )
   If FW_CopyToClipBoard( aFiles, 15 )
    SysWait( 2 )
    oShell:SendKeys( "^v" )
    SysWait( 1 )
   EndIf
  EndIf
  AEval( aOthers, <|u|
                      If FW_CopyToClipBoard( u, 2 ) .OR. FW_CopyToClipBoard( u )
                       SysWait( 1 )
                       oShell:SendKeys( "^v" )
                       SysWait( 1 )
                      EndIf
                      RETURN NIL
                  > )
   oShell:SendKeys("~")
 EndIf
RETURN lRet
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
Re: WhatsApp tests
Posted: Wed Apr 17, 2024 07:58 AM

Dear Antonio,

Thank you very much for the code and the suggestion with the key combination symbol ~ (which stands for "Enter").

I just did a test and it works. I only tried text. It seems that you need to have WhatsApp installed on the PC. On the server via RDP with the administrator, I get this info:

This app cannot be activated by the built-in administrator account.

Best regards,

Otto

Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: WhatsApp tests
Posted: Wed Apr 17, 2024 10:27 AM
Antonio,




I make a small test but I have allways errors

Error description: Error BASE/1003 Variable does not exist: AATTACH


My test
Code (fw): Select all Collapse
#include "fivewin.ch"
#include 'constant.ch'

Function test()
Local oDlg,oFont,oBold
local aGet:=array(10)
local oBtnSel
local cPhone:=space(14)
local cMsg:=space(255)
local aCooDlg := aParamDialog(6)
local nRow:= 40,nCol:= 10
local nInterlinea := 30


oFont := TFont():New( "TAHOMA", 0, 16,, )
oBold := TFont():New( "TAHOMA", 0, 14,,.t. )

DEFINE DIALOG oDlg SIZE aCooDlg[1], aCooDlg[2] PIXEL TRUEPIXEL;
       FONT oFont TITLE "test WhatsApp Message"  COLOR CLR_BLACK, nRgb( 245,244,234) TRANSPARENT;
       STYLE nOR( DS_MODALFRAME, WS_POPUP, WS_CAPTION, WS_SYSMENU, ;
                  WS_MINIMIZEBOX)


@ nRow,nCol Say "Phone :"
nRow-=2

@ nRow,nCol GET aGet[1] VAR cPhone SIZE 200,24 PIXEL of oDlg

@ nRow,nCol+200 BTNBMP oBtnSel FILE "PHONE.png" SIZE 30,23 PIXEL  FLAT NOROUND;
 ACTION ::ShowPopUp( Options( Self,cPhone,cMsg ) )
           oBtnSel:nClrBorder := rgb(210,210,205)



ACTIVATE DIALOG oDlg  CENTER   ;
ON INIT (oDlg:resize())
RETURN NIL
//---------------------------------------------------------------------//
Function Options (oBtn,cPhone,cMsg)
local oMenu

If Empty(cPhone)
    MsgAlert("Devi inserie un numero telefonico")
    return nil 
 Endif

MENU oMenu POPUP

MENUITEM "Messaggio con SMS"
SEPARATOR
MENUITEM "Messaggio con WhathApp"  NAME "whatsapp.png" ;
           ACTION   AskMsg(cMsg,cPhone)



ENDMENU
return oMenu

Function AskMsg(cMsg,cPhone)
   If MsgGet("Messaggio da inviare","Messaggio :",cMsg )
      SendToWhatsApp( cPhone, cMsg, aAttach )
   endif
   return nil

//----------------------------------------------------------------------//
FUNC SendToWhatsApp( cPhone, cMsg, aAttach )
LOCAL oShell, aFiles := {}, aOthers  := {}, lRet

    DEFAULT aAttach := {}

 If (lRet := !Empty( cPhone ))
  If !Empty( aAttach )
   If( !HB_ISARRAY( aAttach ), aAttach := { aAttach }, )
   AEval( aAttach, {|cFile| If( HB_ISSTRING( cFile ) .AND. File( cFile ), AAdd( aFiles, cFile), AAdd( aOthers, cFile ) ) } )
  EndIf

  cMsg  := StrTran( cMsg, CRLF, "%0D%0A" )

  oShell := CreateObject( "WScript.Shell" )
  ShellExecute( 0, "Open", "whatsapp://send?phone="+cPhone+"&text="+cMsg)
  SysWait( 0.2 )
  If !Empty( aFiles )
   If FW_CopyToClipBoard( aFiles, 15 )
    SysWait( 2 )
    oShell:SendKeys( "^v" )
    SysWait( 1 )
   EndIf
  EndIf
  AEval( aOthers, <|u|
                      If FW_CopyToClipBoard( u, 2 ) .OR. FW_CopyToClipBoard( u )
                       SysWait( 1 )
                       oShell:SendKeys( "^v" )
                       SysWait( 1 )
                      EndIf
                      RETURN NIL
                  > )
   oShell:SendKeys("~")
 EndIf
RETURN lRet
 

Function aParamDialog(n)
     local  nBottom,nRight,nWd,nHt
     local aTmp :={0,0}
 Do Case
 Case n=6
           nBottom:= 12     //edit2
           nRight := 78
Endcase

        nHt       := nBottom * DLG_CHARPIX_H
        nWd       := Max( nRight * DLG_CHARPIX_W, 180 )
        aTmp[1]:=nWd
        aTmp[2]:=nHt
    return aTm

How can you remove the ability to have attachments or not?
then the function should check if the WhatsApp application is installed on the PC and load it directly from the Microsoft store but I don't know how to do it

the link to download is "https://apps.microsoft.com/detail/9nksqgp7f2nh?hl=it-it&gl=IT"
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Posts: 1283
Joined: Fri Feb 10, 2006 02:34 PM
Re: WhatsApp tests
Posted: Wed Apr 17, 2024 11:27 AM
Code (prg): Select all Collapse
Function AskMsg(cMsg,cPhone)
   If MsgGet("Messaggio da inviare","Messaggio :",cMsg )
      SendToWhatsApp( cPhone, cMsg )
   endif
   return nil
Salutacions, saludos, regards

"...programar es fácil, hacer programas es difícil..."

UT Page -> https://carles9000.github.io/
Forum UT -> https://discord.gg/bq8a9yGMWh
HIX -> https://github.com/carles9000/hix
Posts: 1344
Joined: Wed Nov 16, 2005 09:14 PM
Re: WhatsApp tests
Posted: Wed Apr 17, 2024 11:55 AM

Antonio, a partir de que version funciona?

Porque me da error en FW_CopyToClipBoard( aFiles, 15 )

Error description: Error BASE/1066 Argument error: conditional

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: WhatsApp tests
Posted: Wed Apr 17, 2024 01:31 PM
César,
Code (fw): Select all Collapse
function FW_CopyToClipBoard( uValue, nFormat )

   local lCopied  := .f.
   local aBmp

   if HB_ISPOINTER( uValue ) // assumes GDI+ Image object
      GDIPLUSImageToClipBoard( uValue, GetDesktopWindow() )
      return .t.
   endif

   if uValue != nil

      if nFormat == 15
         if !HB_ISARRAY( uValue ); uValue := { uValue }; endif
         _fwhtmp_ := uValue
         uValue   := {}
         AEval( _fwhtmp_, { |c| If( File( c ), AAdd( uValue, TrueName( c ) ), nil ) } )
         _fwhtmp_ := nil
         if Empty( uValue )
            return .f.
         endif
      endif

      if Empty( nFormat ) .and. HB_ISARRAY( uValue ) .and. ;
         AScan( uValue, { |u| !( ValType( u ) == "C" .and. File( u ) ) } ) == 0
            AEval( uValue, { |c,i| uValue[ i ] := TrueName( c ) } )
            nFormat  := 15
      endif

      if nFormat == 2 .and. HB_ISSTRING( uValue )
         aBmp  := FW_ReadImage( nil, uValue, , .f. )
         if !Empty( aBmp[ 1 ] )
            uValue := aBmp[ 1 ]
         else
            return .f.
         endif
      endif

      if Empty( nFormat )
         if HB_ISSTRING( uValue )
            if IsBinaryData( uValue )
               if Left( MemoryBufferType( uValue ), 4 ) == "IMG."
                  aBmp := FW_ReadImage( nil, uValue, , .f. )
                  if !Empty( aBmp[ 1 ] )
                     uValue   := aBmp[ 1 ]
                     nFormat  := 2
                  endif
               endif
               if Empty( nFormat )
                  return .f.
               endif
            elseif Left( uValue, 4 ) == "http"
               aBmp := FW_ReadImage( nil, uValue, , .f. )
               if !Empty( aBmp[ 1 ] )
                  uValue   := aBmp[ 1 ]
                  nFormat  := 2
               else
                  nFormat  := 1
               endif
            else
               nFormat  := 1
            endif
         elseif HB_ISNUMERIC( uValue )
            if ISHBITMAP( uValue )
               nFormat := 2
            elseif ISENHMETA( uValue )
               nFormat  := 14
            endif
         endif
      endif

      if Empty( nFormat )
         if ValType( uValue ) $ "AH"
            uValue   := FW_ValToExp( uValue )
         elseif ValType( uValue ) $ "DT"
            uValue   := CharRem( "'", FW_DateToSQL( uValue ) )
         else
            uValue   := cValToChar( uValue )
         endif
         nFormat  := 1
      endif

      if nFormat != nil
         if nFormat == 2
            if HB_ISNUMERIC( uValue ) .and. ISHBITMAP( uValue )
               GDIPLUSHBITMAPTOCLIPBOARD( uValue, GetDeskTopWindow() )
               lCopied  := .t.
            endif
         elseif OpenClipBoard( GetDeskTopWindow() )
            EmptyClipboard()
            if nFormat == 1 .and. IsUtf8( uValue )
               uValue    := utf8toutf16( uValue )
               if !( Right( uValue, 2 ) == Chr( 0 ) + Chr( 0 ) )
                  uValue    += ( Chr( 0 ) + Chr( 0 ) )
               endif
               lCopied  := SetClipboardData( 13, uValue )
            elseif nFormat == 15
               lCopied  := SetClipboardData( 15, uValue )
            else
               lCopied  := SetClipboardData( nFormat, uValue )
            endif
            CloseClipboard()
         endif
      endif
   endif

   if aBmp != nil
      PalBmpFree( aBmp )
   endif

return lCopied
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1344
Joined: Wed Nov 16, 2005 09:14 PM
Re: WhatsApp tests
Posted: Wed Apr 17, 2024 03:23 PM

SetClipboardData me devuelve NIL y por eso me falla la funcion

Posts: 1344
Joined: Wed Nov 16, 2005 09:14 PM
Re: WhatsApp tests
Posted: Wed Apr 17, 2024 08:11 PM

Funcionó bien!

Depende de donde estemos parado (si en el texto para enviar, en buscar, etc) del Whatsapp para que funcione correctamente.

Pero si arrancamos poniendo el cursor en donde se debe escribir el mensaje, funciona

Igualmente sigo haciendo pruebas.

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: WhatsApp tests
Posted: Wed Apr 17, 2024 09:53 PM
César,

muchas gracias por tus pruebas! :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: WhatsApp tests
Posted: Thu Apr 18, 2024 06:30 AM

on my computer it simply opens the WhatsApp application but in cases where it is not installed,

the procedure should check if it is installed and if it is not, go and download the application from the Microsoft store

However it doesn't work for me, I tried to send it to a friend and nothing arrived

opens the whatsapp application but then doesn't send anything

if it finds the number on whatsapp it goes into the chat of the called number but then does not send anything, while if you enter an incorrect number the number not found information message arrives

Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com

Continue the discussion