B4J Library {WIP} Telegram Bot API (REST based)

I published these wrappers a while ago

Telegram Bots API
B4A : https://www.b4x.com/android/forum/threads/telegram-bots-api.94438/
B4J : https://www.b4x.com/android/forum/threads/telegram-bots-api.66458/

but they suffered some problems especially the B4A wrapper

Also , @DonManfred has done another great wrapper here https://www.b4x.com/android/forum/threads/beta-jtelegrambot-create-your-own-telegram-bot.103821/


What I am sharing now is at the very beginning and still needs a lot of improvements and work BUT the most attempting point in here is that it should be easily used in any of our great B4X platforms (B4A,B4J and B4i )

The sample is in B4J but it should work for B4A and B4i (not tested though )

If you improved any part of this project , please share :)

The code of the sample (B4J)

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim bot As TelegramBot
    Private domain As String=""
    Private token As String=""
    Private tgtoken As String =""
    Dim time As Timer
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    bot.Initialize(tgtoken,Me,"tg")
'    bot.GetMe
'    bot.DeleteWebhook(domain)
    bot.getUpdates(0,0,10000,"" )
'    time.Initialize("UpdateIp",1000*60*10)
'    time.Enabled=True
'    UpdateIp_Tick
'    bot.GetWebhookInfo
End Sub
Public Sub tg_updates(updates As List)

    For Each up As Update In updates
        Dim msg As Message
        msg.Initialize(up.message)

        If msg.document.IsInitialized Then
            Dim d As Document
            d.Initialize(msg.document)
            bot.GetFile(d.file_id)
            
            Log(d.file_name)
            Log(d.file_size)
            If d.thumb.IsInitialized Then
                Dim photothumb As PhotoSize
                photothumb.Initialize( d.thumb)
                Log(photothumb.file_id)
                Log(photothumb.file_size)
                Log(photothumb.height)
                Log(photothumb.width)
            End If
'            Log(d.thumb)
                        
        End If
        Dim photos  As List =msg.photo
        If photos.IsInitialized Then
            Log(photos.Size)
            Dim phot As PhotoSize
            phot.Initialize(photos.Get(2))
            Log(phot.file_id)
            bot.GetFile(phot.file_id)
            
        End If
'        Log(msg.text)

    Next
End Sub
Public Sub tg_file(tfile As TGFile)
    Log(bot.getFullFilePath(tfile.file_path))
End Sub
Public Sub GetFile(file_id As String)
    
End Sub
Public Sub tg_WebhookInfo(whi As WebhookInfo)
    Log(whi.url)
    Log(whi.has_custom_certificate)
    Log(whi.pending_update_count)
    
    
End Sub
Public Sub tg_me(meuser As User)
    Log(meuser.id)
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub
Sub UpdateIp_Tick
    Dim j As HttpJob
    j.Initialize("j", Me)
    j.Tag=Null
    Try
        j.Download2("http://www.duckdns.org/update", Array As String("domains", domain, "token", token, _
        "ip", ""))
    Catch
        Log(LastException)
    End Try
    
End Sub
 

Attachments

  • TelegramBotApi.zip
    22.5 KB · Views: 408
Top