Page 1 of 1

WhatsApp tests

PostPosted: Wed Apr 17, 2024 3:55 am
by Antonio Linares
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: Select all  Expand view
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
 

Re: WhatsApp tests

PostPosted: Wed Apr 17, 2024 7:58 am
by Otto
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

Re: WhatsApp tests

PostPosted: Wed Apr 17, 2024 10:27 am
by Silvio.Falconi
Antonio,

Image


I make a small test but I have allways errors

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


My test
Code: Select all  Expand view
#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"

Re: WhatsApp tests

PostPosted: Wed Apr 17, 2024 11:27 am
by Carles
Code: Select all  Expand view
Function AskMsg(cMsg,cPhone)
   If MsgGet("Messaggio da inviare","Messaggio :",cMsg )
      SendToWhatsApp( cPhone, cMsg )
   endif
   return nil
 

Re: WhatsApp tests

PostPosted: Wed Apr 17, 2024 11:55 am
by cmsoft
Antonio, a partir de que version funciona?
Porque me da error en FW_CopyToClipBoard( aFiles, 15 )
Error description: Error BASE/1066 Argument error: conditional

Re: WhatsApp tests

PostPosted: Wed Apr 17, 2024 1:31 pm
by Antonio Linares
César,

Code: Select all  Expand view
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

Re: WhatsApp tests

PostPosted: Wed Apr 17, 2024 3:23 pm
by cmsoft
SetClipboardData me devuelve NIL y por eso me falla la funcion

Re: WhatsApp tests

PostPosted: Wed Apr 17, 2024 8:11 pm
by cmsoft
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.

Re: WhatsApp tests

PostPosted: Wed Apr 17, 2024 9:53 pm
by Antonio Linares
César,

muchas gracias por tus pruebas! :-)

Re: WhatsApp tests

PostPosted: Thu Apr 18, 2024 6:30 am
by Silvio.Falconi
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