B4J Library [BANanoGreenAPI] BANanoFetch REST API WhatsApp bridge to Send & Receive Messages, Manage Groups

Hi Fam

Download

Many thanks to @Mariano Ismael Castro

This is a wrap of the B4xGreenAPI for BANAno, internally it uses BANanoFetch to execute REST API calls to the GreenAPI to send & receive messages and manage groups on WhatsApp.

Requirements

1. A GreenAPI account. Developers get a free developer account.
2. A GreenAPI account instance. When you create an account, you will need to create an instance.
3. Extra - In each instance you can set a hook to receive incoming & outgoing messages & statuses like read, delivered etc. This can point to a b4j server controller or whatever back-end you use. That way you can store whatsapp messages on a back-end.

Things to note

Contact - a contact or chatID is the country code without the +, suffixed with @c.us, e.g. 27827366700@c.us (not my real number)
Group - a group ID is the country code without the +, suffixed with @g.us, (you get these when reading contacts as they are created by whatsapp)
Each message you receive / send is allocated a messageID

Available Methods

1. Account

1.1 GetSettings
1.2 SetSettings
1.3 GetStateInstance
1.4 Reboot
1.5 Logout
1.6 QR
1.7 GetAuthorizatioCode
1.8 SetProfilePicture
1.9 GetWaSettings

2. Sending
2.1 SendMessage
2.2 SendPoll
2.3 SendFileByUpload
2.4 SendFileByURL
2.5 UploadFile
2.6 SendLocation
2.7 SendContact
2.8 ForwardMessages

3. Receiving
3.1 ReceiveIncomingNotifications
3.2 ReceiveNotification
3.3 DeleteNotification
3.4 DownloadFile
3.5 ReadChat
3.6 ReadChats

4. Journals
4.1 GetChatHistory
4.2 GetMessage
4.3 LastIncomingMessages
4.4 LastOutgoingMessages

5. Que
5.1. ShowMessageQue
5.2 ClearMessagesQue

6. Groups
6.1. CreateGroup
6.2. UpdateGroupName
6.3. GetGroupData
6.4. AddGroupParticipant
6.5. RemoveGroupParticipant
6.6. SetGroupAdmin
6.7. RemoveAdmin
6.8. SetGroupPicture
6.9. LeaveGroup

7. Statuses (BETA)
7.1. SendTextStatus
7.2. SendVoiceStatus
7.3. SendMediaStatus
7.4. GetOutgoingStatuses
7.5. GetIncomingStatuses
7.6. GetStatusStatistic
7.7. DeleteStatus


8. Services
8.1 CheckWhatsApp
8.2 GetAvatar
8.3. GetContacts
8.4 GetContactInfo
8.5 DeleteMessage
8.6. EditMessage (BETA)
8.7 ArchiveChat
8.8 UnArchiveChat
8.9 SetDisappearingChat

GreenAPI RoadMap

  • As of 20 Jan 2025, there are 344 open issues on their GitHub repo and some pull requests.
  • There are some features that have been requested and are pending due to some limitations of some of the available methods in the REST API

Appreciation

If you find my posts useful, please consider supporting with a certificate of appreciation. This helps with research & development and ensures you have useful tools for developing your apps.


All the best
 

Mashiane

Expert
Licensed User
Longtime User
BANanoGreenAPI Usage

NB: You need your GreenAPI Instance ID and Instance Token


B4X:
Public GreenAPI As BANanoGreenAPI

Sub StartGreenAPI
    GreenAPI.Initialize(Me, "greenapi", Main.GreenAPI_Instance, Main.GreenAPI_Token)
    GreenAPI.ApiUrl = Main.GreenAPI_APIURL
    'this will return the phone number & deviceid
    BANano.Await(GreenAPI.GetWaSettings)
    'BANano.Await(GreenAPI.GetSettings)
    Main.AccountID = GreenAPI.PhoneNumber
    Main.AccountAvatar = GreenAPI.Avatar
    '
    'get the contact information for the account
    Dim thisAccount As ContactInfo = BANano.Await(GreenAPI.GetContactInfo($"${Main.AccountID}@c.us"$))
    If thisAccount.name = "" Then
        app.ShowToastWarning("Please re-login to your GreenAPI instance to establish the name of your account.")
    End If
    Dim suserName As String = $"${thisAccount.name} - ${thisAccount.chatId} - ${Main.UserName}"$
    BANano.Await(UpdateUserName(suserName))
    
'    'trap incoming messages & files (this can be done once via the instance in web interface)
'    Log("Apply settings...")
'    GreenAPI.WebhookUrl = Main.GreenAPI_WebHook
'    GreenAPI.OutgoingWebhook = "no"
'    GreenAPI.OutgoingAPIMessageWebhook = "no"
'    GreenAPI.OutgoingMessageWebhook = "no"
'    GreenAPI.IncomingWebhook = "yes"
'    GreenAPI.OutgoingWebhook = "no"
'    GreenAPI.pollMessageWebhook = "no"
'    GreenAPI.StateWebhook = "no"
'    GreenAPI.StatusInstanceWebhook = "no"
'    Dim bSettings As Boolean = BANano.Await(GreenAPI.SetSettings)
'    Log(bSettings)
    
'    Log(GreenAPI.deviceid)
'    Log(GreenAPI.PhoneNumber)
'    Log(GreenAPI.Avatar)
    'update the user logo in pgIndex
    'UpdateUserName(GreenAPI.PhoneNumber)
    'UpdateUserAvatar(GreenAPI.Avatar)
    'log out first
    'BANano.Await(GreenAPI.LogOut)
    'get QR Code
    'Dim qr As String = BANano.Await(GreenAPI.GetQRCode)
    'Log(qr)
    
    'authorize
    'Dim acode As String = BANano.Await(GreenAPI.GetAuthorizationCode)
    'Log(acode)
    
    'greenAPI.MediaUrl = "https://7103.media.greenapi.com"
    'BANano.Await(GreenAPI.getAccountSettings)
    'Dim accountStatus As String = BANano.Await(GreenAPI.GetAccountState)
    'Log(accountStatus)
    'Dim bResponse As Boolean = BANano.Await(GreenAPI.ReBoot)
    'Log(bResponse)
    '*****
    'send a message to a contact
'    Dim sAndile As String = "27xxx@c.us"
'    Dim sSiba As String = "27xxx@c.us"
'    Dim sMySelf As String = "27xxx@c.us"
'    Dim sDean As String = "27xxx@c.us"
    
'    Dim sResp As String = BANano.Await(greenAPI.SendPoll(sMySelf, "Please choose the color", Array("green", "red", "blue"), False, ""))
'    Log(sResp)
    
    
    'Dim idMessage As String = BANano.Await(greenAPI.SendMessage(sSiba, "Da is testing the GreenAPI to send & receive WhatsApp messages for b4x. Please confirm receipt!","",True))
    'Log(idMessage)
    '
'    Dim fileURL As String = "https://drive.google.com/uc?export=download&id=1-G3jNqwkweu4qtN3tntYFjmHtUF5X6FZxxx"
'    Dim idMessage As String = BANano.Await(greenAPI.SendFileByUrl(sMySelf, fileURL, "BANanoVuetify 3 - Mock.Compile.Publish.pdf", "BANanoVuetify 3 - Mock.Compile.Publish", ""))
'    Log(idMessage)
    
    'Dim mypos As BANanoGeoPosition = BANano.Await(app.GetMyLocation)
    'Log(mypos)
    '
    'Dim idMessage As String = BANano.Await(greenAPI.SendLocation(sAndile, mypos.Latitude, mypos.Longitude, "Home via GreenAPI", "<Address>", ""))
    'Log(idMessage)
    
    '
    'Dim idMessage As String = BANano.Await(greenAPI.SendContact(sAndile, "Anele", "Mashy", "Mbanga", "27xxx", "Sithaso Holdings BANanoGreenAPI", ""))
    'Log(idMessage)
    
    'Dim whatsAppExists As Boolean = BANano.Await(greenAPI.CheckWhatsApp("27xxx"))
    'Log(whatsAppExists)
    '
    'Dim myAvatar As String = BANano.Await(greenAPI.GetAvatarFor(sAndile))
    'Log(myAvatar)
    
    'Dim contacts As List = BANano.Await(greenAPI.GetContacts(False))
    'Log(contacts)
    '
    'Dim contact As ContactInfo = BANano.Await(greenAPI.GetContactInfo(sDean))
    'Log(contact)
    '
    'Dim contacts As List
    'contacts.Initialize 
    'contacts.Add(sDean)
    'contacts.Add(sSiba)
    'contacts.Add(sMySelf)
    'Dim grp As Map = BANano.Await(greenAPI.CreateGroup("Group created by BANanoGreenAPI", contacts))
    'Log(grp)
    '120363347003942330@g.us
    'Dim nGroupID As String = "120xxx@g.us"
    'Dim bRenameGroup As Boolean = BANano.Await(greenAPI.UpdateGroupName(nGroupID, "Sithaso BANanoGreenAPI"))
    'Log(bRenameGroup)
    '
    'Dim gd As GroupData = BANano.Await(greenAPI.GetGroupData(nGroupID))
    'Log(gd)
    
    'Dim bAdded As Boolean = BANano.Await(greenAPI.AddGroupParticipant(nGroupID, sAndile))
    'Log(bAdded)
    '
    'Dim bRemoved As Boolean = BANano.Await(greenAPI.RemoveGroupParticipant(nGroupID, sAndile))
    'Log(bRemoved)
    '
    'Dim bGroupAdmin As Boolean = BANano.Await(greenAPI.SetGroupAdmin(nGroupID, sDean))
    'Log(bGroupAdmin)
    '
    'Dim bGroupAdmin As Boolean = BANano.Await(greenAPI.SetGroupAdmin(nGroupID, sSiba))
    'Log(bGroupAdmin)
    '
    'Dim bRemoveGroupAdmin As Boolean = BANano.Await(greenAPI.RemoveGroupAdmin(nGroupID, sDean))
    'Log(bRemoveGroupAdmin)
    
    'Dim bRemoveGroupAdmin As Boolean = BANano.Await(greenAPI.RemoveGroupAdmin(nGroupID, sSiba))
    'Log(bRemoveGroupAdmin)
    
    '*** send file by upload
'    Dim assetFile As BANanoObject = BANano.Await(SDUIShared.FetchAsFile("./assets", "mashy.jpg"))
'    Dim fileURL As String = BANano.Await(greenAPI.UploadFile(assetFile))
'    Log(fileURL)
    '
'    Dim response As Map = BANano.Await(greenAPI.SendFileByUpload(sMySelf, "Mashy", assetFile, "mashy.jpg", ""))
'    If response.ContainsKey("urlFile") Then
'        Dim surlFile As String = response.Get("urlFile")
'        Dim idMessage As String = BANano.Await(greenAPI.SendFileByUrl(sMySelf, surlFile, "mashy.jpg", "Mashy", ""))
'        Log(idMessage)
'    End If
    
    'get a file from the assets folder

'    Dim bRemoveGroupAdmin As Boolean = BANano.Await(greenAPI.SetGroupPicture(nGroupID, assetFile))
'    Log(bRemoveGroupAdmin)
    '
'    Dim bLeaveGroup As Boolean = BANano.Await(greenAPI.LeaveGroup(nGroupID))
'    Log(bLeaveGroup)

    'Dim chats As List = BANano.Await(greenAPI.GetChatHistory(sMySelf))
    'Log(chats)
    '    
    'Dim chat As Message = BANano.Await(greenAPI.GetMessage("27xxx@c.us", "BAxxx"))
    'Log(chat)
    
    'Dim forwards As List = BANano.Await(greenAPI.ForwardMessages(sSiba, sMySelf, Array("BAxxx", "BAxxx")))
    'Log(forwards)
    
    'Dim lLastIncomingMessages As List = BANano.Await(greenAPI.LastIncomingMessages(1440))
    'Log(lLastIncomingMessages)
    '
    'Dim lLastOutgoingMessages As List = BANano.Await(greenAPI.LastOutgoingMessages(1440))
    'Log(lLastOutgoingMessages)
    
    '***** works for notifications
'    Try
'        app.PagePause
'        Dim message As MessageNotification
'        message.Initialize
'        Dim messageExists As Boolean = BANano.IsNull(message) 
'        Do While messageExists = False
'            'trap receive notifications
'            message = BANano.Await(greenAPI.ReceiveNotification(20))
'            Log(message)
'            BANano.Await(greenAPI.DeleteNotification(message.receiptId))
'            messageExists = BANano.IsNull(message)
'        Loop
'        app.pageresume
'    Catch
'        Log(LastException)
'        app.pageresume
'    End Try    
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…