Sending WhatsApp msgs from our apps

832 views
Skip to first unread message

Antonio Linares

unread,
Aug 12, 2012, 10:25:12 PM8/12/12
to harbou...@googlegroups.com
I have started reviewing the code published in:

I am using Harbour's sockets so it may work from all operating systems. Here I share the first steps
for those of you that may be interested about it and may want to try it and help:

#include "hbsocket.ch"

function Main()

   local pSocket := hb_socketOpen()   
   local cIPAddr := hb_socketGetHosts( "bin-short.whatsapp.net" )[ 1 ]
   local cBuffer := Space( 1024 )
   
   ? hb_socketConnect( pSocket, { HB_SOCKET_AF_INET, cIPAddr, 5222 } )

   ? hb_socketSend( pSocket, "WA" + "\x01\x00\x00\x19\xf8\x05\x01\xa0\x8a\x84\xfc\x11" + ;
                    "iPhone-2.6.9-5222" + ;
                            "\x00\x08\xf8\x02\x96\xf8\x01\xf8\x01\x7e\x00\x07\xf8\x05\x0f\x5a\x2a\xbd\xa7" )
   ? hb_socketRecv( pSocket, @cBuffer )  // we should get something here

   HB_SocketShutDown( pSocket )
   HB_SocketClose( pSocket )

return nil

We should get some data from hb_socketRecv() but it is returning zero bytes.

I am also opening this same thread at FiveTech forums:

Antonio

Antonio Linares

unread,
Aug 12, 2012, 10:40:39 PM8/12/12
to harbou...@googlegroups.com
Fixed, now we get data :-)

#include "hbsocket.ch"

function Main()

   local pSocket := hb_socketOpen()   
   local cIPAddr := hb_socketGetHosts( "bin-short.whatsapp.net" )[ 1 ]
   local cBuffer := Space( 1024 )
   
   ? hb_socketConnect( pSocket, { HB_SOCKET_AF_INET, cIPAddr, 5222 } )

   ? hb_socketSend( pSocket, "WA" + Chr( 0x01 ) + Chr( 0 ) + Chr( 0 ) + ;
                    Chr( 0x19 ) + Chr( 0xF8 ) + Chr( 0x05 ) + Chr( 0x01 ) + ;
                    Chr( 0xA0 ) + Chr( 0x8A ) + Chr( 0x84 ) + Chr( 0xFC ) + ;
                    Chr( 0x11 ) + "iPhone-2.6.9-5222" + ;
                            Chr( 0 ) + Chr( 0x08 ) + Chr( 0xF8 ) + Chr( 0x02 ) + ;
                            Chr( 0x96 ) + Chr( 0xF8 ) + Chr( 0x01 ) + Chr( 0xF8 ) + ;
                            Chr( 0x01 ) + Chr( 0x7E ) + Chr( 0 ) + Chr( 0x07 ) + Chr( 0xF8 ) + ;
                            Chr( 0x05 ) + Chr( 0x0F ) + Chr( 0x5A ) + Chr( 0x2A ) + ;
                            Chr( 0xBD ) + Chr( 0xA7 ) )
   ? hb_socketRecv( pSocket, @cBuffer ) 

   HB_SocketShutDown( pSocket )
   HB_SocketClose( pSocket )

return nil

Antonio

Antonio Linares

unread,
Aug 13, 2012, 5:27:02 AM8/13/12
to harbou...@googlegroups.com
#include "hbsocket.ch"

function Main()

   local oWA := HB_WhatsApp():New()

   ? oWA:Connect()
   oWA:Login()

return nil

CLASS HB_WhatsApp

   DATA  pSocket
   DATA  cHost  INIT "bin-short.whatsapp.net"
   DATA  nPort  INIT 5222
   DATA  cIP
   DATA  aResArray
   DATA  cMsg
   DATA  _Incomplete_message

   METHOD New()
   METHOD Connect()
   METHOD Login()

   METHOD _Identify( cStr )
   METHOD _Is_Full_Msg( cStr )

   DESTRUCTOR Destroy()

ENDCLASS

METHOD New() CLASS HB_WhatsApp

   ::cIP = hb_socketGetHosts( ::cHost )[ 1 ]
   ::pSocket = hb_socketOpen()

return self

METHOD Connect() CLASS HB_WhatsApp

return hb_socketConnect( ::pSocket, { HB_SOCKET_AF_INET, ::cIP, ::nPort } )

METHOD Login() CLASS HB_WhatsApp

   local cBuffer := Space( 1024 ), nLen, cV


   ? hb_socketSend( ::pSocket, "WA" + Chr( 0x01 ) + Chr( 0 ) + Chr( 0 ) + ;
                  Chr( 0x19 ) + Chr( 0xF8 ) + Chr( 0x05 ) + Chr( 0x01 ) + ;
                  Chr( 0xA0 ) + Chr( 0x8A ) + Chr( 0x84 ) + Chr( 0xFC ) + ;
                  Chr( 0x11 ) + "iPhone-2.6.9-5222" + ;
                  Chr( 0 ) + Chr( 0x08 ) + Chr( 0xF8 ) + Chr( 0x02 ) + ;
                  Chr( 0x96 ) + Chr( 0xF8 ) + Chr( 0x01 ) + Chr( 0xF8 ) + ;
                  Chr( 0x01 ) + Chr( 0x7E ) + Chr( 0 ) + Chr( 0x07 ) + Chr( 0xF8 ) + ;
                  Chr( 0x05 ) + Chr( 0x0F ) + Chr( 0x5A ) + Chr( 0x2A ) + ;
                  Chr( 0xBD ) + Chr( 0xA7 ) )

   ? nLen := hb_socketRecv( ::pSocket, @cBuffer )

   cBuffer = SubStr( cBuffer, 1, nLen )
   ::aResArray = HB_ATokens( cBuffer, Chr( 0 ) )

   for each cV in ::aResArray
      cRcvdType = ::_Identify( cV )

      ? cRcvdType

      do case
         case cRcvdType == "incomplete_msg"
              ::_incomplete_message = cV

         case cRcvdType == "msg"
              ::cMsg = ::parse_received_message( cV )
        
         case cRcvdType == "account_info"
              ::accinfo = ::parse_account_info( cV )

         case cRcvdType == "last_seen"
              ::lastseen = ::parse_last_seen( cV )
      endcase
   next

return nil

static function StartsWith( cStr, cCompare, nPos )

return SubStr( cStr, nPos, Len( cCompare ) ) == cCompare

static function EndsWith( cStr, cCompare )

return Right( cStr, Len( cCompare ) ) == cCompare

METHOD _Identify( cStr ) CLASS HB_WhatsApp

   local cMsg_identifier := Chr( 0x5D ) + Chr( 0x38 ) + Chr( 0xFA ) + Chr( 0xFC )
   local cServer_delivery_identifier := Chr( 0x8C )   
   local cClient_delivery_identifier := Chr( 0x7F ) + Chr( 0xBD ) + Chr( 0xAD )
   local cAcc_info_iden := Chr( 0x99 ) + Chr( 0xBD ) + Chr( 0xA7 ) + Chr( 0x94 )   
   local cLast_seen_ident := Chr( 0x48 ) + Chr( 0x38 ) + Chr( 0xFA ) + Chr( 0xFC )
   local cLast_seen_ident2 := Chr( 0x7B ) + Chr( 0xBD ) + Chr( 0x4C ) + Chr( 0x8B )

   if ! ::_is_full_msg( cStr )
      return "incomplete_msg"

   elseif StartsWith( cStr, cMsg_identifier, 3 )

      if EndsWith( cStr, cServer_delivery_identifier )
         return "server_delivery_report"

      elseif EndsWith( cStr, cClient_delivery_identifier )
         return "client_delivery_report"

      else
         return "msg"

      endif

   elseif StartsWith( cStr, cAcc_info_iden, 3 )
      return "account_info"

   elseif StartsWith( cStr, cLast_seen_ident, 3 ) .and. cLast_seen_ident2 $ cStr
      return "last_seen"
  
   else
      return "other"

   endif

return nil

METHOD _Is_Full_Msg( cStr ) CLASS HB_WhatsApp

return Len( cStr ) == Asc( Left( cStr, 1 ) ) + 1

METHOD Destroy() CLASS HB_WhatsApp


   HB_SocketShutDown( ::pSocket )
   HB_SocketClose( ::pSocket )

   ::pSocket = nil

return nil

regards

Antonio

Antonio Linares

unread,
Aug 13, 2012, 5:21:50 PM8/13/12
to harbou...@googlegroups.com
More recent version at: (we already get some readable info)

I will publish it there to avoid too much posts here

Antonio

Antonio Linares

unread,
Aug 17, 2012, 7:09:36 AM8/17/12
to harbou...@googlegroups.com
It is working !!!! wooooot!!! :-D

Most recent version (full source code included)  and screenshots:

Harbour power!!! :-)

Antonio

Massimo Belgrano

unread,
Aug 23, 2012, 6:04:04 AM8/23/12
to harbou...@googlegroups.com
i read
Remember that you have to use a phone number (to send a msg) from a WhatsApp registered on an Android device! 

2012/8/23 Cha <charo...@gmail.com>
Hi,

I try with my Iphone but no success.

Any tip?.



--
Massimo Belgrano

Antonio Linares

unread,
Aug 23, 2012, 6:17:27 AM8/23/12
to harbou...@googlegroups.com
We have just ported the WhatsAPI from php code to PRG (Harbour) code.

For WhatsAPI tech support please visit the WhatsAPI project and forks at GitHub

Antonio


El jueves, 23 de agosto de 2012 11:14:28 UTC+2, Cha escribió:
Hi,

I try with my Iphone but no success.

Any tip?.

El lunes, 13 de agosto de 2012 04:25:12 UTC+2, Antonio Linares escribió:

Francis

unread,
Aug 25, 2012, 1:27:42 PM8/25/12
to harbou...@googlegroups.com
Antonio, it dows not work with Iphone or Blackberry.

Here, all the contributions have a very high quality except yours. See, for example, last chat by Giovanni. It works quite good and the source is

properly tested.


Please, test your ideas before making here noise and wasting people´s time, powe of the PC and bandwith when downloading your messages about unfinished trash.

If you are bored you may finish all your propotypes: Visual FW, IDE, Fiveform, Fiveweb... even the sole Fivewin requieres you fix her bugs and you give a fresh look to the interface, just indicating some examples.

What do you intend with those "trash prototypes"?.

vszakats

unread,
Nov 14, 2012, 6:46:55 PM11/14/12
to harbou...@googlegroups.com
Latest from forum doesn't work.

Error BASE/1132  Bound error: array access (Quit)
Error BASE/1132  Bound error: array access
Called from HB_WHATSAPP:LOGIN(121)
Called from MAIN(16)

Line 121:
      hAuthData[ aResData[ 1 ] ] = StrTran( aResData[ 2 ], '"', "" )

This is STRREV in pure .prg code: (so it can be run as script 
after switching back to EN CP)
---
STATIC FUNCTION StrRev( cString )

   LOCAL cRes := ""
   LOCAL c

   FOR EACH c in cString DESCEND
      cRes += c
   NEXT

   RETURN cRes
---

-- Viktor

Reply all
Reply to author
Forward
0 new messages