Pruebas con WhatsApp

Pruebas con WhatsApp

Postby Antonio Linares » Wed Apr 17, 2024 3:57 am

Modesto Rocabado ha compartido con nosotros este código para enviar mensajes y ficheros usando WhatsApp.

Os agradecemos vuestras pruebas y feedback para afinarlo. Gracias!

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
 
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41901
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Pruebas con WhatsApp

Postby Armando » Wed Apr 17, 2024 4:42 am

Maestro Antonio L.

Copie la función y la llamo con el siguiente código

Code: Select all  Expand view

   SendToWhatsApp( "7293320729", "Hola !",)

o con
   SendToWhatsApp( "+557293320729", "Hola !",)
 


En mi PC se abre WhatsApp pero no llega el mensaje aunque la función retorna .T. ¿qué le cambio?

Saludos
SOI, s.a. de c.v.
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
User avatar
Armando
 
Posts: 3179
Joined: Fri Oct 07, 2005 8:20 pm
Location: Toluca, México

Re: Pruebas con WhatsApp

Postby Joaquim Ferrer » Wed Apr 17, 2024 4:10 pm

Mi experiencia con el codigo facilitado

El numero de telefono al que enviar mensaje tiene que ser exactamente como aparece en la información de contacto, es decir, (+)codigo de país + telefono

Añadir SysWait( 1 ) antes del ultimo oShell:SendKeys("~")

Si WhatsApp está abierto en el escritorio, se envia el mensaje de forma automática

Si no está abierto, lanza el programa pero no envia nada, a veces se queda el texto del mensaje a enviar en espera de darle al botón de envío

Seria interesante poder comunicar en modo 'silencioso' sin abrir el programa

Existen varios whatsapp-CLI pero para Python y Go
Fivewinner desde 1.9, programador PHP y Javascript, PWA & HTML5 evangelista
User avatar
Joaquim Ferrer
 
Posts: 105
Joined: Sat Jan 14, 2012 3:46 pm
Location: Barcelona

Re: Pruebas con WhatsApp

Postby paquitohm » Wed Apr 17, 2024 4:52 pm

IMHO,

Este tipo de soluciones me pareciera a mi que no se mantienen en el tiempo y además están sujetas a demasiadas casuísticas pre
Asi que como mejor alguna solución "estable" python, a través del API con python o un ejecutable python que haga el trabajo, después de recibir unos parámetros (*)

(*) Estaría bien que tuviéramos un .exe python con distintas funcionalidades al que se le pasa como parámetro el nombre de un json y devuelve otro con el mismo nombre y _respuesta de sufijo

Mis 2 eurocents
paquitohm
 
Posts: 211
Joined: Fri Jan 14, 2022 8:37 am

Re: Pruebas con WhatsApp

Postby karinha » Wed Apr 17, 2024 5:24 pm

Armando, no te falta el código de área?

Code: Select all  Expand view

   SendToWhatsApp( "+55(11)7293320729", "Hola !",) // +55 -> Brazil, (11) -> São Paulo? Cual ciudad en Brazil?
 



Regards, saludos.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
karinha
 
Posts: 7641
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: Pruebas con WhatsApp

Postby Antonio Linares » Wed Apr 17, 2024 6:08 pm

Usando python se puede hacer asi:

pip install pywhatkit

whatsapp.py
Code: Select all  Expand view
import pywhatkit as pw

# Número de teléfono (incluyendo código de país)
recipient = "+573123456789"

# Mensaje de texto
message = "Hola! Te envío este mensaje con Python."

# Lista de archivos adjuntos (opcionales)
attachments = [
    "/ruta/a/archivo1.pdf",
    "/ruta/a/archivo2.jpg"
]

# Enviar mensaje con o sin archivos adjuntos
if attachments:
    pw.send_whatsap_message(recipient, message, attachments=attachments)
else:
    pw.send_whatsap_message(recipient, message)

Para generar un EXE:
pyinstaller --onefile whatsapp.py
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41901
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Pruebas con WhatsApp

Postby Armando » Wed Apr 17, 2024 10:38 pm

Colegas:

Éxito obtenido, teniendo abierto wathsapp.

Saludos afectuosos
SOI, s.a. de c.v.
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
User avatar
Armando
 
Posts: 3179
Joined: Fri Oct 07, 2005 8:20 pm
Location: Toluca, México

Re: Pruebas con WhatsApp, funciona con condiciones

Postby Willi Quintana » Thu Apr 18, 2024 6:23 pm

Estimados amigos....
El envío de mensajes por el WhatsApp funciona, siempre y cuando, al momento de enviar el mensaje desde FW, el usuario que envía, debe estar activo en el WHATSAPP de la pc....
Si estamos en revisando algún otro mensaje que o es el nuestro, el mensaje no llega...
User avatar
Willi Quintana
 
Posts: 1010
Joined: Sun Oct 09, 2005 10:41 pm
Location: Cusco - Perú

Re: Pruebas con WhatsApp

Postby Marcelo Roggeri » Mon Sep 02, 2024 12:44 pm

Hola buenos días grupo, una consulta para el envío de email, use el ejemplo de Antonio pero me da error en la línea que adjunto
En aFiles agregue el archivo que deseo enviar y luego con la ruta completa y da el mismo error.
Code: Select all  Expand view
If FW_CopyToClipBoard( aFiles, 15 )

Cualquier ayuda es bienvenida.
Gracias de antemano
Saludos
FWH - Harbour - BCC7 - PellesC
User avatar
Marcelo Roggeri
 
Posts: 342
Joined: Sat Jul 22, 2006 9:04 pm
Location: Venado Tuerto - Santa Fe -Argentina

Re: Pruebas con WhatsApp

Postby Antonio Linares » Mon Sep 02, 2024 3:13 pm

Marcelo,

aFiles debe tener este formato { ruta_a_mi_fichero, ruta_a_otro_fichero, etc. }

Lo estás haciendo asi ?
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41901
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Pruebas con WhatsApp

Postby Marcelo Roggeri » Mon Sep 02, 2024 5:13 pm

Si Antonio, correcto así lo estoy haciendo
Saludos
FWH - Harbour - BCC7 - PellesC
User avatar
Marcelo Roggeri
 
Posts: 342
Joined: Sat Jul 22, 2006 9:04 pm
Location: Venado Tuerto - Santa Fe -Argentina

Re: Pruebas con WhatsApp

Postby Antonio Linares » Mon Sep 02, 2024 5:27 pm

Y que error es el que te aparece ?
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41901
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Pruebas con WhatsApp

Postby Marcelo Roggeri » Mon Sep 02, 2024 5:49 pm

Time from start: 0 hours 0 mins 3 secs
Error occurred at: 09/02/24, 11:26:02
Error description: Error BASE/1066 Argument error: conditional
Args:
[ 1] = U

Stack Calls
===========
Called from: emailforo.prg => SENDTOWHATSAPP( 27 )
Called from: emailforo.prg => MAIN( 11 )

Code: Select all  Expand view
#include "fivewin.ch"

FUNCTION main()
         Local cPhone := "+5493462555555"
         Local cMsg   := "prueba"
         Local aAttach:= { curdrive()+":\"+curdir()+"\"+"warnings.log" }

 xbrowse(aAttach)


         SendToWhatsApp( cPhone, cMsg, aAttach )

RETURN nil

FUNCTION 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
 
FWH - Harbour - BCC7 - PellesC
User avatar
Marcelo Roggeri
 
Posts: 342
Joined: Sat Jul 22, 2006 9:04 pm
Location: Venado Tuerto - Santa Fe -Argentina

Re: Pruebas con WhatsApp

Postby Antonio Linares » Mon Sep 02, 2024 6:19 pm

Tu código funciona bien aqui

copia este código a tu ejemplo y usa varios MsgInfo()s para ver por donde pasa:

Code: Select all  Expand view
static 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
User avatar
Antonio Linares
Site Admin
 
Posts: 41901
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Pruebas con WhatsApp

Postby Marcelo Roggeri » Mon Sep 02, 2024 8:16 pm

Antonio, pego el codigo y te escribo algo,
Code: Select all  Expand view
        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 )

    ? lCopied

            else
               lCopied  := SetClipboardData( nFormat, uValue )
            endif
            CloseClipboard()
         endif
      endif
   endif

   if aBmp != nil
      PalBmpFree( aBmp )
   endif

return lCopied

 


Devuelve lCopied y es indefinido osea un blanco, osea no es .f. ni .t.
FWH - Harbour - BCC7 - PellesC
User avatar
Marcelo Roggeri
 
Posts: 342
Joined: Sat Jul 22, 2006 9:04 pm
Location: Venado Tuerto - Santa Fe -Argentina

Next

Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 39 guests