Ayuda - Clase p/envio de msg,file por whatsapp - GREENAPI

Ayuda - Clase p/envio de msg,file por whatsapp - GREENAPI

Postby carlos vargas » Sun Oct 29, 2023 4:22 pm

He estado trabajando, intentado hacer una clase para el envio de mensajes y archivos usando la api de GreenApi, la cual es de pago, ($12)
he tenido exito enviando mensajes y contactos, no asi con el enviod e archivos, he atacado el problema intentado enviar el archivo en base 64, el archivo se envia, pero nunca se decodifca a su binario correspondiente apareciendo como coarrupto, por lo que sospecho que elenvio es en modo binario, pero igualmente el archivo se envia, pero me parece que llega incompleto, si alguien logra hacerlo funcionar bien, le ruego lo comparta asi como yo lo hago con este trabajo. :-)
gracias y buena suerte.
https://console.green-api.com/app/api/sendFileByUpload
Codigo de el uso
Code: Select all  Expand view

   //Esto Funciona
   oGA := GreenApi():New( "7100068281", "1427d09058014f3cbb5033f3fccc8b0d0ed0ae8d44e946c8c2" )
   oGA:setLog( TRUE )
   IF !oGA:sendMessage( "50588888888","KDSOFT - Prueba de class!" + FINL + Time() )
      ?oGA:cError
   ENDIF
   IF !oGA:sendContact( "50588888888",  "50588888899", "Carlos", "Ivan", "Vargas", "KDSoft" )
      ?oGA:cError
   ENDIF
  //Esto no funciona
   IF !oGA:sendFileByUpload( "50588888888", "d:\81774383108888_payment.pdf" )
      ?oGA:cError
   ENDIF
   oGA:End()
 
Salu2
Carlos Vargas
Desde Managua, Nicaragua (CA)
User avatar
carlos vargas
 
Posts: 1720
Joined: Tue Oct 11, 2005 5:01 pm
Location: Nicaragua

Re: Ayuda - Clase p/envio de msg,file por whatsapp - GREENAPI

Postby carlos vargas » Sun Oct 29, 2023 4:24 pm

Code: Select all  Expand view

#define __NOBINARY__

/*----------------------------------------------------------------------------*/

//REEMPLAZAR POR LOS QUE LES INDIQUE AL REGISTRARSE EN GREENAPI, HAY UN MODO DEMO, DEBEN CREAR UNA CUENTA DESSARLLOADOR, CREAR UNA INSTANCIA Y VINCULAR UN DISPOSITIVO.
#define GREEN_IDINSTANCE  "7100068281"
#define GREEN_APITOKEN   "1427d09058014f3cbb5033f3fccc8b0d0ed0ae8d44e946c8c2"

/*----------------------------------------------------------------------------*/

CLASS GreenApi
   DATA cApiURL, cMediaURL
   DATA cIdInstance, cIdToken
   DATA oHttp
   DATA lLog
   DATA cError

   METHOD New( cIdInstance, cIdToken ) CONSTRUCTOR
   METHOD SetLog( lLog ) INLINE ::lLog := lLog
   METHOD SetIdInstance( cIdInstance ) INLINE ::cIdInstance := cIdInstance
   METHOD SetIdToken( cIdToken ) INLINE ::cIdToken := cIdToken
   METHOD SetURL( cMethod, lMedia )
   METHOD sendMessage( cPhone, cMessage )
   METHOD sendFileByUpload( cPhone, cFile, cCaption )
   METHOD sendContact( cPhone, hContact )
   METHOD End()

ENDCLASS

/*----------------------------------------------------------------------------*/

METHOD New( cIdInstance, cIdToken ) CLASS GreenApi
   LOCAL oErr

   DEFAULT cIdInstance TO GREEN_IDINSTANCE
   DEFAULT cIdToken TO GREEN_APITOKEN

   ::lLog   := FALSE

   ::SetIdInstance( cIdInstance )
   ::SetIdToken( cIdToken )

   ::cApiURL   := "https://api.green-api.com/waInstance"
   ::cMediaURL := "https://media.greenapi.com/waInstance"

   ::cError := ""

   TRY
      ::oHttp := CreateObject( "MSXML2.ServerXMLHTTP.6.0" )
   CATCH
      TRY
         ::oHttp := CreateObject( "MSXML2.XMLHTTP" )
      CATCH oErr
         ::cError := "Error en la creación de objeto HTTP: " + oErr:Description
      END
   END

RETURN Self

/*----------------------------------------------------------------------------*/

METHOD End() CLASS GreenApi
   IF hb_isObject( ::oHttp )
      ::oHttp := NIL
   ENDIF
RETURN

/*----------------------------------------------------------------------------*/

METHOD setURL( cMethod, lMedia ) CLASS GreenApi
   LOCAL cUrl := ::cApiURL + ::cIdInstance + "/" + cMethod + "/" + ::cIdToken

   DEFAULT lMedia TO FALSE

   IF lMedia
      cUrl := ::cMediaURL + ::cIdInstance + "/" + cMethod + "/" + ::cIdToken
   ENDIF

   IF ::lLog
      TraceLog( cUrl )
   ENDIF

RETURN cUrl

/*----------------------------------------------------------------------------*/

METHOD sendMessage( cPhone, cMessage ) CLASS GreenApi
   LOCAL hInfo
   LOCAL lSend := FALSE
   LOCAL oErr

   ::cError := ""

   IF !hb_isObject( ::oHttp )
      ::cError := "No se pudo crer objeto HTTP!"
      RETURN lSend
   ENDIF

   IF Empty( cPhone ) .or. Empty( cMessage )
      ::cError := "Datos insuficientes para enviar mensaje por Whatsapp!"
      RETURN lSend
   ENDIF

   hInfo := { "chatId" => cPhone + "@c.us", "message" => cMessage }

   TRY
      ::oHttp:Open( "POST", ::setURL( "sendMessage", FALSE ), FALSE )
      ::oHttp:SetRequestHeader( "Content-Type", "application/json" )
      ::oHttp:Send( hb_jsonEncode( hInfo ) )
      IF ::oHttp:Status == 200
         IF ::lLog
            TraceLog( ::oHttp:ResponseBody )
         ENDIF
         lSend := TRUE
         SysRefresh()
      ENDIF
   CATCH oErr
      ::cError := "Error en Objeto HTTP: " + oErr:Description
   END

RETURN lSend

/*----------------------------------------------------------------------------*/

METHOD sendContact( cPhone, cPhoneContact, cFirstName, cMiddelName, cLastName, cCompany ) CLASS GreenApi
   LOCAL hInfo, hContact
   LOCAL lSend := FALSE
   LOCAL oErr

   DEFAULT cMiddelName TO ""
   DEFAULT cCompany    TO ""

   IF !hb_isObject( ::oHttp )
      ::cError := "No se pudo crer objeto HTTP!"
      RETURN lSend
   ENDIF

   IF Empty( cPhone )        .or. ;
      Empty( cPhoneContact ) .or. ;
      Empty( cFirstName )    .or. ;
      Empty( cLastName )
      ::cError := "Datos insuficientes para enviar contacto por Whatsapp!"
      RETURN lSend
   ENDIF

   hContact := { "phoneContact" => cPhoneContact, ;
                 "firstName"    => cFirstName   , ;
                 "middleName"   => cMiddelName  , ;
                 "lastName"     => cLastName    , ;
                 "company"      => cCompany       }

   hInfo := { "chatId"       => cPhone + "@c.us", ;
              "contact"      => hContact          }

   TRY
      ::oHttp:Open( "POST", ::setURL( "sendContact", FALSE ), FALSE )
      ::oHttp:SetRequestHeader( "Content-Type", "application/json" )
      ::oHttp:Send( hb_jsonEncode( hInfo ) )
      IF ::oHttp:Status == 200
         IF ::lLog
            TraceLog( ::oHttp:ResponseBody )
         ENDIF
         lSend := TRUE
         SysRefresh()
      ENDIF
   CATCH oErr
      ::cError := "Error en Objeto HTTP: " + oErr:Description
   END

RETURN lSend

/*----------------------------------------------------------------------------*/

METHOD sendFileByUpload( cPhone, cFileName, cCaption ) CLASS GreenApi
   LOCAL lSend := FALSE
   LOCAL hInfo, oPost
   LOCAL oErr

   ::cError := ""

   IF !hb_isObject( ::oHttp )
      ::cError := "No se pudo crer objeto HTTP!"
      RETURN lSend
   ENDIF

   IF Empty( cPhone )
      ::cError := "Datos insuficientes para enviar mensaje por Whatsapp!"
      RETURN lSend
   ENDIF

   IF !File( cFileName )
      ::cError := "No existe el archivo indicado!"
      RETURN lSend
   ENDIF

   DEFAULT cCaption TO cFileName( cFileName )

   oPost := TPostHtml():New() //"utf-8"
   oPost:AddField( "chatId", cPhone + "@c.us" )
   oPost:AddField( "caption", cCaption )
   oPost:AddFile( "file", cCaption, cFileName )

   oPost:Save( ".\prueba.uue" )

   TRY
      ::oHttp:setTimeouts( 15000, 15000, 15000, 15000 )
      ::oHttp:Open( "POST", ::setURL( "sendFileByUpload", FALSE ), FALSE )
      ::oHttp:SetRequestHeader( "Content-Type", "multipart/form-data; boundary=" + oPost:cBoundaryBase )
      ::oHttp:Send( oPost:Read() )
      IF ::oHttp:Status == 200
         IF ::lLog
            TraceLog( ::oHttp:ResponseBody )
         ENDIF
         lSend := TRUE
         SysRefresh()
      ELSE
         ::cError := cValToChar( ::oHttp:Status )
      ENDIF
   CATCH oErr
     ::cError := "Error en Objeto HTTP: " + oErr:Description
   END

  oPost:End()

RETURN lSend

/*----------------------------------------------------------------------------*/
//EOF
/*----------------------------------------------------------------------------*/

#define adModeUnknown           0
#define adModeRead              1
#define adModeWrite             2
#define adModeReadWrite         3
#define adModeShareDenyRead     4
#define adModeShareDenyWrite    8
#define adModeShareExclusive   12
#define adModeShareDenyNone    16
#define adModeRecursive        0x400000

#define adTypeBinary           1
#define adTypeText             2

#define adReadAll             -1

/*----------------------------------------------------------------------------*/

CLASS TPostHTML

   DATA oStream
   DATA cBoundary, cBoundaryBase
   DATA cCharSet INIT "Windows-1252"
   DATA aExtraRequestHeader INIT {}
   DATA Status

   METHOD New( cCharSet ) CONSTRUCTOR
   METHOD AddField( cField, cValue )
   METHOD AddExtraRequest( cClave,cValor )
   METHOD AddFile( cFieldName, cFileName, cFilePath )
   METHOD Read()
   METHOD Save( cFileName )
   METHOD End() INLINE ( ::oStream:Close(), ::oStream := NIL )

ENDCLASS

/*----------------------------------------------------------------------------*/

METHOD New( cCharSet ) CLASS TPostHTML

   DEFAULT cCharSet TO ::cCharSet

   ::cBoundaryBase := "BorderLine-1234567890"
   ::cBoundary     := "--" + ::cBoundaryBase

   WITH OBJECT ( ::oStream := CreateObject( "ADODB.Stream" ) )
      :Charset := cCharSet
      :Mode    := adModeReadWrite
      :Type    := adTypeText
      :Open()
   END

RETURN Self

/*----------------------------------------------------------------------------*/

METHOD AddField( cField, cValue ) CLASS TPostHtml
   ::oStream:WriteText( ::cBoundary + FINL + 'Content-Disposition: form-data; name="' + cField + '"' + FINL + FINL + cValue + FINL )
RETURN NIL

/*----------------------------------------------------------------------------*/

METHOD AddExtraRequest( cClave,cValor ) CLASS TPostHtml
   AAdd( ::aExtraRequestHeader, { cClave, cValor } )
RETURN NIL

/*----------------------------------------------------------------------------*/

METHOD AddFile( cFieldName, cFileName, cFilePath ) CLASS TPostHtml
#ifdef __NOBINARY__
   LOCAL cFileStr, nFileLen

   cFileStr := hb_StrToUTF8( hb_Base64Encode( FileStr( cFilePath ) ) )
   //cFileStr := CharRem( CRLF, cFileStr )

   //hb_StrToUTF8(
   //cFileStr := FileStr( cFilePath )
   //nFileLen := Len( cFilePath )
   //cFileStr := hb_base64Encode( cFileStr, nFileLen )

   WITH OBJECT ::oStream
      :WriteText( ::cBoundary + FINL )
      :WriteText( 'Content-Disposition: form-data; name="'+ cFieldName + '"; filename="' + cFileName + '"' + FINL )    // + FINL
      :WriteText( 'Content-Type: ' + GetContentType( cFileName ) + FINL )
      :WriteText( 'Content-Transfer-Encoding: base64' +  FINL + FINL )
      :WriteText( cFileStr + FINL )
      :WriteText( ::cBoundary + "--" )
   END

   cFileStr := NIL
   nFileLen := 0

#else
   LOCAL oStream

   WITH OBJECT ( oStream := CreateObject( "ADODB.Stream" ) )
      :Charset := ::cCharSet
      :Type := adTypeBinary
      :Mode := adModeReadWrite
      :Open()
      :LoadFromFile( cFilePath )
   END

   WITH OBJECT ::oStream
      :WriteText( ::cBoundary + FINL )
      :WriteText( 'Content-Disposition: form-data; name="'+ cFieldName + '"; filename="' + cFileName + '"' + FINL + FINL )
      :WriteText( 'Content-Type: ' + GetContentType( cFileName ) + FINL )
      :WriteText( 'Content-Transfer-Encoding: binary' +  FINL + FINL )
      :Position := 0
      :Type     := adTypeBinary
      :Position := :Size()
      oStream:CopyTo( ::oStream )
      :Position := 0
      :Type     := adTypeText
      :Position := :Size()
      :WriteText( FINL + ::cBoundary + "--" )
      :Position := 0
      :Type     := adTypeBinary
   END

   oStream:Close()
#endif
RETURN NIL

/*----------------------------------------------------------------------------*/

METHOD Read() CLASS TPostHtml
   ::oStream:Position := 0
#ifdef __NOBINARY__
RETURN ::oStream:ReadText()
#else
   ::oStream:Position := 0
   ::oStream:Type     := adTypeBinary
RETURN ::oStream:Read()
#endif

/*----------------------------------------------------------------------------*/

METHOD Save( cFileName ) CLASS TPostHtml
   ::oStream:Position := 0
#ifdef __NOBINARY__
RETURN StrFile( ::oStream:ReadText(), cFileName)
#else
   ::oStream:Position := 0
   ::oStream:Type     := adTypeBinary
RETURN StrFile( ::oStream:Read( adReadAll ), cFileName )
#endif

/*----------------------------------------------------------------------------*/

STATIC FUNCTION FixFileName( cFileName )
RETURN CharRem( "[<(-)>]*&@:", cFileName )

/*----------------------------------------------------------------------------*/

STATIC FUNCTION GetContentType( cFileName )
   LOCAL cExt := cFileExt( cFileName )
   LOCAL aItem, cTipo :=  "text/plain"
   LOCAL aTipeFile

   aTipeFile := { ;
             { "php"  , "application/x-php"},;
             { "vbs"  , "application/x-vbs"},;
             { "jpe"  , "image/jpeg"},;
             { "jpg"  , "image/jpeg"},;
             { "jpeg" , "image/jpeg"},;
             { "gif"  , "image/gif"},;
             { "png"  , "image/png"},;
             { "bmp"  , "image/bmp"},;
             { "ico"  , "image/x-icon"},;
             { "svg"  , "image/svg+xml"},;
             { "svgz" , "image/svg+xml"},;
             { "tif"  , "image/tiff"},;
             { "tiff" , "image/tiff"},;
             { "pct"  , "image/x-pict"},;
             { "psd"  , "image/vnd.adobe.photoshop"},;
             { "aac"  , "audio/x-aac"},;
             { "aif"  , "audio/x-aiff"},;
             { "flac" , "audio/x-flac"},;
             { "m4a"  , "audio/x-m4a"},;
             { "m4b"  , "audio/x-m4b"},;
             { "mid"  , "audio/midi"},;
             { "midi" , "audio/midi"},;
             { "mp3"  , "audio/mpeg"},;
             { "mpa"  , "audio/mpeg"},;
             { "mpc"  , "audio/x-musepack"},;
             { "oga"  , "audio/ogg"},;
             { "ogg"  , "audio/ogg"},;
             { "ra"   , "audio/vnd.rn-realaudio"},;
             { "ram"  , "audio/vnd.rn-realaudio"},;
             { "snd"  , "audio/x-snd"},;
             { "wav"  , "audio/x-wav"},;
             { "wma"  , "audio/x-ms-wma"},;
             { "avi"  , "video/x-msvideo"},;
             { "divx" , "video/divx"},;
             { "flv"  , "video/x-flv"},;
             { "m4v"  , "video/mp4"},;
             { "mkv"  , "video/x-matroska"},;
             { "mov"  , "video/quicktime"},;
             { "mp4"  , "video/mp4"},;
             { "mpeg" , "video/mpeg"},;
             { "mpg"  , "video/mpeg"},;
             { "ogm"  , "application/ogg"},;
             { "ogv"  , "video/ogg"},;
             { "rm"   , "application/vnd.rn-realmedia"},;
             { "rmvb" , "application/vnd.rn-realmedia-vbr"},;
             { "smil" , "application/x-smil"},;
             { "webm" , "video/webm"},;
             { "wmv"  , "video/x-ms-wmv"},;
             { "xvid" , "video/x-msvideo"},;
             { "js"   , "application/javascript"},;
             { "xml"  , "text/xml"},;
             { "html" , "text/html"},;
             { "css"  , "text/css"},;
             { "txt"  , "text/plain"},;
             { "py"   , "text/x-python"},;
             { "pdf"  , "application/pdf"},;
             { "xhtml", "application/xhtml+xml"},;
             { "zip"  , "application/x-zip-compressed, application/zip"},;
             { "rar"  , "application/x-rar-compressed"},;
             { "cmd"  , "application/cmd"},;
             { "bat"  , "application/x-bat, application/x-msdos-program"},;
             { "exe"  , "application/exe, application/x-ms-dos-executable"},;
             { "msi"  , "application/x-msi"},;
             { "bin"  , "application/x-binary"},;
             { "crt"  , "application/x-x509-ca-cert"},;
             { "crl"  , "application/x-pkcs7-crl"},;
             { "pfx"  , "application/x-pkcs12"},;
             { "p12"  , "application/x-pkcs12"},;
             { "odc"  , "application/vnd.oasis.opendocument.chart"},;
             { "odf"  , "application/vnd.oasis.opendocument.formula"},;
             { "odb"  , "application/vnd.oasis.opendocument.database"},;
             { "odg"  , "application/vnd.oasis.opendocument.graphics"},;
             { "odi"  , "application/vnd.oasis.opendocument.image"},;
             { "odp"  , "application/vnd.oasis.opendocument.presentation"},;
             { "ods"  , "application/vnd.oasis.opendocument.spreadsheet"},;
             { "odt"  , "application/vnd.oasis.opendocument.tex"},;
             { "docx" , "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},;
             { "dotx" , "application/vnd.openxmlformats-officedocument.wordprocessingml.template"},;
             { "potx" , "application/vnd.openxmlformats-officedocument.presentationml.template"},;
             { "ppsx" , "application/vnd.openxmlformats-officedocument.presentationml.slideshow"},;
             { "pptx" , "application/vnd.openxmlformats-officedocument.presentationml.presentation"},;
             { "xlsx" , "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},;
             { "xltx" , "application/vnd.openxmlformats-officedocument.spreadsheetml.template"},;
             { "ppam" , "application/vnd.ms-powerpoint.addin.macroEnabled.12"},;
             { "ppa"  , "application/vnd.ms-powerpoint"},;
             { "potm" , "application/vnd.ms-powerpoint.template.macroEnabled.12"},;
             { "ppsm" , "application/vnd.ms-powerpoint.slideshow.macroEnabled.12"},;
             { "xlsm" , "application/vnd.ms-excel.sheet.macroEnabled.12"},;
             { "pptm" , "application/vnd.ms-powerpoint.presentation.macroEnabled.12"},;
             { "dotm" , "application/vnd.ms-word.template.macroEnabled.12"},;
             { "docm" , "application/vnd.ms-word.document.macroEnabled.12"},;
             { "doc"  , "application/msword"},;
             { "dot"  , "application/msword"},;
             { "pps"  , "application/mspowerpoint"},;
             { "ppt"  , "application/mspowerpoint,application/powerpoint,application/vnd.ms-powerpoint,application/x-mspowerpoint"},;
             { "xls"  , "application/vnd.ms-excel"},;
             { "xlt" , "application/vnd.ms-excel" } }

   FOR EACH aItem IN aTipeFile
      IF aItem[ 1 ] == cExt
         cTipo := aItem[ 2 ]
      ENDIF
   NEXT

RETURN cTipo

/*----------------------------------------------------------------------------*/
//EOF
/*----------------------------------------------------------------------------*/

 
Salu2
Carlos Vargas
Desde Managua, Nicaragua (CA)
User avatar
carlos vargas
 
Posts: 1720
Joined: Tue Oct 11, 2005 5:01 pm
Location: Nicaragua

Re: Ayuda - Clase p/envio de msg,file por whatsapp - GREENAPI

Postby carlos vargas » Sun Oct 29, 2023 6:50 pm

El archivo llega, si es texto bien, si no, parece corrupto.
Code: Select all  Expand view
CLASS GreenApi
   DATA cApiURL, cMediaURL
   DATA cIdInstance, cIdToken
   DATA oHttp
   DATA lLog, cError

   METHOD New( cIdInstance, cIdToken ) CONSTRUCTOR
   METHOD SetLog( lLog ) INLINE ::lLog := lLog
   METHOD SetIdInstance( cIdInstance ) INLINE ::cIdInstance := cIdInstance
   METHOD SetIdToken( cIdToken ) INLINE ::cIdToken := cIdToken
   METHOD SetURL( cMethod, lMedia )
   METHOD sendMessage( cPhone, cMessage )
   METHOD sendContact( cPhone, hContact )
   METHOD sendFileByUpload( cPhone, cFile, cCaption )
   METHOD getLastErro() INLINE ::cError
   METHOD End()

ENDCLASS

/*----------------------------------------------------------------------------*/

METHOD New( cIdInstance, cIdToken ) CLASS GreenApi
   LOCAL oErr

   Default cIdInstance TO GREENAPI_IDINSTANCE
   Default cIdToken TO GREENAPI_TOKEN

   ::lLog   := FALSE

   ::SetIdInstance( cIdInstance )
   ::SetIdToken( cIdToken )

   ::cApiURL   := "https://api.green-api.com/waInstance"
   ::cMediaURL := "https://media.greenapi.com/waInstance"

   ::cError := ""

   TRY
      ::oHttp := CreateObject( "MSXML2.ServerXMLHTTP.6.0" )
   CATCH
      TRY
         ::oHttp := CreateObject( "MSXML2.XMLHTTP" )
      CATCH oErr
         ::cError := "Error en la creación de objeto HTTP: " + oErr:Description
      END
   END

RETURN Self

/*----------------------------------------------------------------------------*/

METHOD End() CLASS GreenApi
   IF hb_isObject( ::oHttp )
      ::oHttp := NIL
   ENDIF
RETURN

/*----------------------------------------------------------------------------*/

METHOD setURL( cMethod, lMedia ) CLASS GreenApi
   LOCAL cUrl := ::cApiURL + ::cIdInstance + "/" + cMethod + "/" + ::cIdToken

   DEFAULT lMedia TO FALSE

   IF lMedia
      cUrl := ::cMediaURL + ::cIdInstance + "/" + cMethod + "/" + ::cIdToken
   ENDIF

   IF ::lLog
      TraceLog( cUrl )
   ENDIF

RETURN cUrl

/*----------------------------------------------------------------------------*/

METHOD sendMessage( cPhone, cMessage ) CLASS GreenApi
   LOCAL hInfo
   LOCAL lSend := FALSE
   LOCAL oErr

   ::cError := ""

   IF !hb_isObject( ::oHttp )
      ::cError := "No se pudo crer objeto HTTP!"
      RETURN lSend
   ENDIF

   IF Empty( cPhone ) .or. Empty( cMessage )
      ::cError := "Datos insuficientes para enviar mensaje por Whatsapp!"
      RETURN lSend
   ENDIF

   hInfo := { "chatId" => cPhone + "@c.us", "message" => cMessage }

   TRY
      ::oHttp:Open( "POST", ::setURL( "sendMessage", FALSE ), FALSE )
      ::oHttp:SetRequestHeader( "Content-Type", "application/json" )
      ::oHttp:Send( hb_jsonEncode( hInfo ) )
      IF ::oHttp:Status == 200
         IF ::lLog
            TraceLog( ::oHttp:ResponseBody )
         ENDIF
         lSend := TRUE
         SysRefresh()
      ENDIF
   CATCH oErr
      ::cError := "Error en Objeto HTTP: " + oErr:Description
   END

RETURN lSend

/*----------------------------------------------------------------------------*/

METHOD sendContact( cPhone, cPhoneContact, cFirstName, cMiddelName, cLastName, cCompany ) CLASS GreenApi
   LOCAL hInfo, hContact
   LOCAL lSend := FALSE
   LOCAL oErr

   DEFAULT cMiddelName TO ""
   DEFAULT cCompany    TO ""

   IF !hb_isObject( ::oHttp )
      ::cError := "No se pudo crer objeto HTTP!"
      RETURN lSend
   ENDIF

   IF Empty( cPhone )        .or. ;
      Empty( cPhoneContact ) .or. ;
      Empty( cFirstName )    .or. ;
      Empty( cLastName )
      ::cError := "Datos insuficientes para enviar contacto por Whatsapp!"
      RETURN lSend
   ENDIF

   hContact := { "phoneContact" => cPhoneContact, ;
                 "firstName"    => cFirstName   , ;
                 "middleName"   => cMiddelName  , ;
                 "lastName"     => cLastName    , ;
                 "company"      => cCompany       }

   hInfo := { "chatId"       => cPhone + "@c.us", ;
              "contact"      => hContact          }

   TRY
      ::oHttp:Open( "POST", ::setURL( "sendContact", FALSE ), FALSE )
      ::oHttp:SetRequestHeader( "Content-Type", "application/json" )
      ::oHttp:Send( hb_jsonEncode( hInfo ) )
      IF ::oHttp:Status == 200
         IF ::lLog
            TraceLog( ::oHttp:ResponseBody )
         ENDIF
         lSend := TRUE
         SysRefresh()
      ENDIF
   CATCH oErr
      ::cError := "Error en Objeto HTTP: " + oErr:Description
   END

RETURN lSend

/*----------------------------------------------------------------------------*/

METHOD sendFileByUpload( cPhone, cFileName, cCaption ) CLASS GreenApi
   LOCAL lSend := FALSE
   LOCAL hInfo, oPost
   LOCAL oErr

   ::cError := ""

   IF !hb_isObject( ::oHttp )
      ::cError := "No se pudo crer objeto HTTP!"
      RETURN lSend
   ENDIF

   IF Empty( cPhone ) .or. Empty( cFileName )
      ::cError := "Datos insuficientes para enviar mensaje por Whatsapp!"
      RETURN lSend
   ENDIF

   IF !File( cFileName )
      ::cError := "No existe el archivo indicado a enviar!"
      RETURN lSend
   ENDIF

   DEFAULT cCaption TO cFileName( cFileName )

   oPost := TPostHtml():New() //"utf-8"
   oPost:AddField( "chatId", cPhone + "@c.us" )
   oPost:AddField( "caption", cCaption )
   oPost:AddFile( "file", cCaption, cFileName )

   oPost:Save( ".\prueba.uue" )

   TRY
      ::oHttp:setTimeouts( 15000, 15000, 15000, 15000 )
      ::oHttp:Open( "POST", ::setURL( "sendFileByUpload", FALSE ), FALSE )
      ::oHttp:SetRequestHeader( "Content-Type", "multipart/form-data; boundary=" + oPost:cBoundaryBase + FINL )
      ::oHttp:Send( oPost:Read() )
      IF ::oHttp:Status == 200
         IF ::lLog
            TraceLog( ::oHttp:ResponseBody )
         ENDIF
         lSend := TRUE
         SysRefresh()
      ELSE
         ::cError := ">"+cValToChar( ::oHttp:Status )+"<"
      ENDIF
   CATCH oErr
     ::cError := "Error en Objeto HTTP: " + oErr:Description
   END

  oPost:End()

RETURN lSend

/*----------------------------------------------------------------------------*/
//EOF
/*----------------------------------------------------------------------------*/

#define adModeUnknown           0
#define adModeRead              1
#define adModeWrite             2
#define adModeReadWrite         3
#define adModeShareDenyRead     4
#define adModeShareDenyWrite    8
#define adModeShareExclusive   12
#define adModeShareDenyNone    16
#define adModeRecursive        0x400000

#define adTypeBinary           1
#define adTypeText             2

#define adReadAll             -1

/*----------------------------------------------------------------------------*/

CLASS TPostHTML

   DATA oStream
   DATA cBoundary, cBoundaryBase
   DATA cCharSet INIT "Windows-1252"
   DATA aExtraRequestHeader INIT {}
   DATA Status

   METHOD New( cCharSet ) CONSTRUCTOR
   METHOD AddField( cField, cValue )
   METHOD AddExtraRequest( cClave,cValor )
   METHOD AddFile( cFieldName, cFileName, cFilePath )
   METHOD Read()
   METHOD ReadText()
   METHOD Save( cFileName )
   METHOD End() INLINE ( ::oStream:Close(), ::oStream := NIL )

ENDCLASS

/*----------------------------------------------------------------------------*/

METHOD New( cCharSet ) CLASS TPostHTML

   DEFAULT cCharSet TO ::cCharSet

   ::cBoundaryBase := "MY_BorderLine"
   ::cBoundary     := "--" + ::cBoundaryBase

   WITH OBJECT ( ::oStream := CreateObject( "ADODB.Stream" ) )
      :Charset := cCharSet
      :Mode    := adModeReadWrite
      :Type    := adTypeText
      :Open()
   END

RETURN Self

/*----------------------------------------------------------------------------*/

METHOD AddField( cField, cValue ) CLASS TPostHtml
   ::oStream:WriteText( ::cBoundary + FINL + 'Content-Disposition: form-data; name="' + cField + '"' + FINL + FINL + cValue + FINL )
RETURN NIL

/*----------------------------------------------------------------------------*/

METHOD AddExtraRequest( cClave,cValor ) CLASS TPostHtml
   AAdd( ::aExtraRequestHeader, { cClave, cValor } )
RETURN NIL

/*----------------------------------------------------------------------------*/

METHOD AddFile( cFieldName, cFileName, cFilePath ) CLASS TPostHtml
   LOCAL oStream
   LOCAL cInfo

   cInfo := ::cBoundary + FINL
   cInfo += 'Content-Disposition: form-data; name="'+ cFieldName + '"; filename="' + cFileName + '"' + FINL
   cInfo += 'Content-Type: ' + GetContentType( cFileName ) + FINL
   cInfo += 'Content-Transfer-Encoding: binary' + FINL + FINL
   cInfo += FileStr( cFilePath ) + FINL
   cInfo += FINL + ::cBoundary + "--"

   WITH OBJECT ::oStream
      :Position := 0
      :Type := adTypeText
      :Position := :Size()
      :WriteText( cInfo )
   END

RETURN NIL

/*----------------------------------------------------------------------------*/

METHOD Read() CLASS TPostHtml
   WITH OBJECT ::oStream
      :Position := 0
      :Type     := adTypeBinary
   END
RETURN ::oStream:Read()

/*----------------------------------------------------------------------------*/

METHOD ReadText() CLASS TPostHtml
   WITH OBJECT ::oStream
      :Position := 0
      :Type     := adTypeText
   END
RETURN ::oStream:ReadText()

/*----------------------------------------------------------------------------*/

METHOD Save( cFileName ) CLASS TPostHtml
RETURN StrFile( ::ReadText(), cFileName )

/*----------------------------------------------------------------------------*/

STATIC FUNCTION GetContentType( cFileName )
   LOCAL cExt := cFileExt( cFileName )
   LOCAL aItem, cTipo :=  "text/plain"
   LOCAL aTipeFile

   aTipeFile := { ;
             { "php"  , "application/x-php"},;
             { "vbs"  , "application/x-vbs"},;
             { "jpe"  , "image/jpeg"},;
             { "jpg"  , "image/jpeg"},;
             { "jpeg" , "image/jpeg"},;
             { "gif"  , "image/gif"},;
             { "png"  , "image/png"},;
             { "bmp"  , "image/bmp"},;
             { "ico"  , "image/x-icon"},;
             { "svg"  , "image/svg+xml"},;
             { "svgz" , "image/svg+xml"},;
             { "tif"  , "image/tiff"},;
             { "tiff" , "image/tiff"},;
             { "pct"  , "image/x-pict"},;
             { "psd"  , "image/vnd.adobe.photoshop"},;
             { "aac"  , "audio/x-aac"},;
             { "aif"  , "audio/x-aiff"},;
             { "flac" , "audio/x-flac"},;
             { "m4a"  , "audio/x-m4a"},;
             { "m4b"  , "audio/x-m4b"},;
             { "mid"  , "audio/midi"},;
             { "midi" , "audio/midi"},;
             { "mp3"  , "audio/mpeg"},;
             { "mpa"  , "audio/mpeg"},;
             { "mpc"  , "audio/x-musepack"},;
             { "oga"  , "audio/ogg"},;
             { "ogg"  , "audio/ogg"},;
             { "ra"   , "audio/vnd.rn-realaudio"},;
             { "ram"  , "audio/vnd.rn-realaudio"},;
             { "snd"  , "audio/x-snd"},;
             { "wav"  , "audio/x-wav"},;
             { "wma"  , "audio/x-ms-wma"},;
             { "avi"  , "video/x-msvideo"},;
             { "divx" , "video/divx"},;
             { "flv"  , "video/x-flv"},;
             { "m4v"  , "video/mp4"},;
             { "mkv"  , "video/x-matroska"},;
             { "mov"  , "video/quicktime"},;
             { "mp4"  , "video/mp4"},;
             { "mpeg" , "video/mpeg"},;
             { "mpg"  , "video/mpeg"},;
             { "ogm"  , "application/ogg"},;
             { "ogv"  , "video/ogg"},;
             { "rm"   , "application/vnd.rn-realmedia"},;
             { "rmvb" , "application/vnd.rn-realmedia-vbr"},;
             { "smil" , "application/x-smil"},;
             { "webm" , "video/webm"},;
             { "wmv"  , "video/x-ms-wmv"},;
             { "xvid" , "video/x-msvideo"},;
             { "js"   , "application/javascript"},;
             { "xml"  , "text/xml"},;
             { "html" , "text/html"},;
             { "css"  , "text/css"},;
             { "txt"  , "text/plain"},;
             { "py"   , "text/x-python"},;
             { "pdf"  , "application/pdf"},;
             { "xhtml", "application/xhtml+xml"},;
             { "zip"  , "application/x-zip-compressed, application/zip"},;
             { "rar"  , "application/x-rar-compressed"},;
             { "cmd"  , "application/cmd"},;
             { "bat"  , "application/x-bat, application/x-msdos-program"},;
             { "exe"  , "application/exe, application/x-ms-dos-executable"},;
             { "msi"  , "application/x-msi"},;
             { "bin"  , "application/x-binary"},;
             { "crt"  , "application/x-x509-ca-cert"},;
             { "crl"  , "application/x-pkcs7-crl"},;
             { "pfx"  , "application/x-pkcs12"},;
             { "p12"  , "application/x-pkcs12"},;
             { "odc"  , "application/vnd.oasis.opendocument.chart"},;
             { "odf"  , "application/vnd.oasis.opendocument.formula"},;
             { "odb"  , "application/vnd.oasis.opendocument.database"},;
             { "odg"  , "application/vnd.oasis.opendocument.graphics"},;
             { "odi"  , "application/vnd.oasis.opendocument.image"},;
             { "odp"  , "application/vnd.oasis.opendocument.presentation"},;
             { "ods"  , "application/vnd.oasis.opendocument.spreadsheet"},;
             { "odt"  , "application/vnd.oasis.opendocument.tex"},;
             { "docx" , "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},;
             { "dotx" , "application/vnd.openxmlformats-officedocument.wordprocessingml.template"},;
             { "potx" , "application/vnd.openxmlformats-officedocument.presentationml.template"},;
             { "ppsx" , "application/vnd.openxmlformats-officedocument.presentationml.slideshow"},;
             { "pptx" , "application/vnd.openxmlformats-officedocument.presentationml.presentation"},;
             { "xlsx" , "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},;
             { "xltx" , "application/vnd.openxmlformats-officedocument.spreadsheetml.template"},;
             { "ppam" , "application/vnd.ms-powerpoint.addin.macroEnabled.12"},;
             { "ppa"  , "application/vnd.ms-powerpoint"},;
             { "potm" , "application/vnd.ms-powerpoint.template.macroEnabled.12"},;
             { "ppsm" , "application/vnd.ms-powerpoint.slideshow.macroEnabled.12"},;
             { "xlsm" , "application/vnd.ms-excel.sheet.macroEnabled.12"},;
             { "pptm" , "application/vnd.ms-powerpoint.presentation.macroEnabled.12"},;
             { "dotm" , "application/vnd.ms-word.template.macroEnabled.12"},;
             { "docm" , "application/vnd.ms-word.document.macroEnabled.12"},;
             { "doc"  , "application/msword"},;
             { "dot"  , "application/msword"},;
             { "pps"  , "application/mspowerpoint"},;
             { "ppt"  , "application/mspowerpoint,application/powerpoint,application/vnd.ms-powerpoint,application/x-mspowerpoint"},;
             { "xls"  , "application/vnd.ms-excel"},;
             { "xlt" , "application/vnd.ms-excel" } }

   FOR EACH aItem IN aTipeFile
      IF aItem[ 1 ] == cExt
         cTipo := aItem[ 2 ]
      ENDIF
   NEXT

RETURN cTipo

/*----------------------------------------------------------------------------*/
//EOF
/*----------------------------------------------------------------------------*/
 
Salu2
Carlos Vargas
Desde Managua, Nicaragua (CA)
User avatar
carlos vargas
 
Posts: 1720
Joined: Tue Oct 11, 2005 5:01 pm
Location: Nicaragua

Re: Ayuda - Clase p/envio de msg,file por whatsapp - GREENAPI

Postby nageswaragunupudi » Sun Oct 29, 2023 8:02 pm

viewtopic.php?f=3&t=43841&p=264851&sid=e24041a61111c5a2cb7ce52e4e99150c#p264851

I tested sending Exe files as attachments
This is working perfectly.
The receiver is able to execute the exe correctly.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10633
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Ayuda - Clase p/envio de msg,file por whatsapp - GREENAPI

Postby carlos vargas » Sun Oct 29, 2023 10:24 pm

Yes rao, others using whastapp app, i try using api Green, other option. :-)
Salu2
Carlos Vargas
Desde Managua, Nicaragua (CA)
User avatar
carlos vargas
 
Posts: 1720
Joined: Tue Oct 11, 2005 5:01 pm
Location: Nicaragua

Re: Ayuda - Clase p/envio de msg,file por whatsapp - GREENAPI

Postby Erick Almanza » Mon Oct 30, 2023 12:24 am

Buena noche
Carlos

Me parece que si usas el metodo SendFilebyUrl. Primero debes subir a tu servidor luego desde alli lo envias. La otra forma es por carga

Saludos

Erick Almanza
Erick Almanza
 
Posts: 85
Joined: Mon Sep 11, 2006 3:52 pm

Re: Ayuda - Clase p/envio de msg,file por whatsapp - GREENAPI

Postby carlos vargas » Mon Oct 30, 2023 4:23 am

Esa es la que deseo usar..
Salu2
Carlos Vargas
Desde Managua, Nicaragua (CA)
User avatar
carlos vargas
 
Posts: 1720
Joined: Tue Oct 11, 2005 5:01 pm
Location: Nicaragua

Re: Ayuda - Clase p/envio de msg,file por whatsapp - GREENAPI

Postby Erick Almanza » Mon Oct 30, 2023 2:45 pm

Buen dia Carlos

Consideraste usar el hb_memoread() para que el archivo no llegue corrupto.

manda['body']:= "data:application/pdf;base64,"+ hb_base64encode( hb_memoread(formato))


Saludos

Erick Almanza
Panama
Erick Almanza
 
Posts: 85
Joined: Mon Sep 11, 2006 3:52 pm

Re: Ayuda - Clase p/envio de msg,file por whatsapp - GREENAPI

Postby carlos vargas » Mon Oct 30, 2023 9:38 pm

Si, usar hb_momoread o Filestr es similar, he probado en base 64 y si bien llega completo no se e regresa a su condición anterior.
Salu2
Carlos Vargas
Desde Managua, Nicaragua (CA)
User avatar
carlos vargas
 
Posts: 1720
Joined: Tue Oct 11, 2005 5:01 pm
Location: Nicaragua


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 37 guests