B4J Library Telegram Bots API

Hello all ,

I started wrapping this project https://github.com/pengrad/java-telegram-bot-api a while ago after failing to make a multipart post that Telegram needs to send a photo :D
the project seems to be huge so I wrapped only features I needed at the current time .
I will try to keep this library updated with most requested features if any :D




Even newer :1.12

A new version with some features is released
Thank you kostefar :)



NEW : 1.06 :
I got busy so I almost stopped working on this library but for my own needs I updated it partially :D I don't remember my updates but If you wanted some feature that was missing , you might want to try this version .
If the feature you are looking for is still missing , You can motivate me with a donation to implement it :)




NEW : 1.05 :
Two new modifications :
1- Initialize method modified to accommodate timeout setting tele.Initialize(Token,EventName,connectTimeout,writeTimeout,readTimeout)
an attempt to solve issue reported by
u2005k
here https://www.b4x.com/android/forum/threads/telegram-bots-api.66458/#post-432967

2- error event
B4X:
Sub tele_error(error As String)
    Log("FROM B4J: "&error)
End Sub
not sure if it would work as it should :D


NEW : Version 1.03 : Just re wrapping a newer (not the latest) version of original library.
I get timeout error but it might be due to partial block on Telegram here .
Can someone please test?




SMMTeleBot
Author:
SMM
Version: 1.01
  • Audio
    Methods:
    • Initialize
    • IsInitialized As Boolean
    • duration As Integer
    • fileId As String
    • fileSize As Integer
    • mimeType As String
    • performer As String
    • title As String
  • Chat
    Methods:
    • Initialize
    • IsInitialized As Boolean
    • firstName As String
    • id As Long
    • lastName As String
    • title As String
    • type As String
    • username As String
  • Contact
    Methods:
    • Initialize
    • IsInitialized As Boolean
    • firstName As String
    • lastName As String
    • phoneNumber As String
    • userId As Integer
  • Document
    Methods:
    • Initialize
    • IsInitialized As Boolean
    • fileId As String
    • fileName As String
    • fileSize As Integer
    • mimeType As String
    • thumb As CPhotoSize
  • Location
    Methods:
    • Initialize
    • IsInitialized As Boolean
    • latitude As Float
    • longitude As Float
  • Message
    Methods:
    • Initialize
    • IsInitialized As Boolean
    • audio As CAudio
    • caption As String
    • channelChatCreated As Boolean
    • chat As CChat
    • contact As CContact
    • date As Integer
    • deleteChatPhoto As Boolean
    • document As CDocument
    • forwardDate As Integer
    • forwardFrom As CUser
    • from As CUser
    • groupChatCreated As Boolean
    • leftChatParticipant As CUser
    • location As CLocation
    • messageId As Integer
    • migrateFromChatId As Long
    • migrateToChatId As Long
    • newChatParticipant As CUser
    • newChatPhoto As CPhotoSize[]
    • newChatTitle As String
    • photo As CPhotoSize[]
    • replyToMessage As CMessage
    • sticker As CSticker
    • supergroupChatCreated As Boolean
    • text As String
    • video As CVideo
    • voice As CVoice
  • PhotoSize
    Methods:
    • Initialize
    • IsInitialized As Boolean
    • fileId As String
    • fileSize As Integer
    • height As Integer
    • width As Integer
  • SMMTeleBot
    Events:
    • _updates (updates As List)
    • sent (result As Message)
  • Methods:
    • GetUpdatesList (offset As Int, limit As Int, timeout As Int)
    • Initialize (Token As String, EventName As String)
    • rgetUpdatesList (offset As Int, limit As Int, timeout As Int) As List
    • sendMessage (chatId As String, MessageText As String)
    • sendPhoto (chatId As String, folder As String, filename As String, caption As String, reply_to_message_id As Integer, bForceReply As Boolean, bReplyKeyboardHide As Boolean)
    • sendVideo (chatId As String, folder As String, filename As String, caption As String, reply_to_message_id As Integer, bForceReply As Boolean, bReplyKeyboardHide As Boolean, duration As Int)
  • Sticker
    Methods:
    • Initialize
    • IsInitialized As Boolean
    • fileId As String
    • fileSize As Integer
    • height As Integer
    • thumb As CPhotoSize
    • width As Integer
  • Update
    Methods:
    • Initialize
    • IsInitialized As Boolean
    • chosenInlineResult As ChosenInlineResult
    • inlineQuery As InlineQuery
    • message As Message
    • updateId As Integer
  • User
    Methods:
    • Initialize
    • IsInitialized As Boolean
    • firstName As String
    • id As Integer
    • lastName As String
    • username As String
  • Video
    Methods:
    • Initialize
    • IsInitialized As Boolean
    • duration As Integer
    • fileId As String
    • fileSize As Integer
    • height As Integer
    • mimeType As String
    • thumb As CPhotoSize
    • width As Integer
  • Voice
    Methods:
    • Initialize
    • IsInitialized As Boolean
    • duration As Integer
    • fileId As String
    • fileSize As Integer
    • mimeType As String
  • sendResponse
    Methods:
    • Initialize
    • IsInitialized As Boolean
    • isOk As Boolean
    • message As Message

Extract all files inside archive to your Additional Libraries folder


B4X:
Dim tele As SMMTeleBot
tele.Initialize(smmtoken,"tele")

tele.getUpdatesList(0,20,9000)

Sub tele_updates(updates As List)
    If updates.IsInitialized Then
        For up = 0 To updates.Size-1
            Dim u As Update =updates.Get(up)
            Log(u.updateId)
            Dim mmmm As Message =u.message
            Log(mmmm.text&"this message was sent on"&DateTime.Date( DateUtils.UnixTimeToTicks( mmmm.date)))

        Next
    Else
        Log("No updates")
    End If

End Sub
Sub tele_sent(some As Message)

    Log(some.IsInitialized)
    If some.IsInitialized Then
    Log(some.caption&" message sent: "&DateTime.Time(DateUtils.UnixTimeToTicks(some.date))&CRLF&some.video.duration)

    Else
        Log("Fail")
    End If


End Sub

It is still a work in progress please note that bugs and errors are very likely to happen :) If you got any problem or you think a feature should be ported please post it here or send me PM .
 

Attachments

  • SMMTeleBot.zip
    335.4 KB · Views: 1,154
  • SMMTeleBot103.zip
    274.5 KB · Views: 797
  • okhttp-3.3.1.zip
    316 KB · Views: 1,057
  • SMMTeleBot105.zip
    274.7 KB · Views: 803
  • SMMTeleBot106.zip
    297.4 KB · Views: 923
  • SMMTeleBot112.zip
    294.5 KB · Views: 675
  • SMMTeleBot113.zip
    35.8 KB · Views: 588
  • SMMTeleBot115.zip
    41.5 KB · Views: 664
  • java-telegram-bot-api-4.6.0.zip
    188.4 KB · Views: 702
Last edited:

Payne

New Member
Licensed User
Longtime User
I am new to B4X group. It's very hard for me to start big project. If someone could share a more complete project code , I think all beginners are very appreciate. The beginner can grow up soon by "learn","copy" from giant of you.

thanks advance for all of your help on my B4x road.
 

Blueforcer

Well-Known Member
Licensed User
Longtime User
Yes it would be very nice to have a demo code. Because your "old" sample code only checks the last messages after start. dont know how to send or recieve a message
Also i dont know how to send a message or use the customkeyboards.
It would be also nice , if you documenting you lib. There are so many Methods etc and i dont know what it is and where or how to use it:)
 

Blueforcer

Well-Known Member
Licensed User
Longtime User
Im getting the latest message once, shortly after sending with

B4X:
Sub tele_updates(updates As List)
    If updates.IsInitialized Then
        Dim update As Update=updates.Get(updates.size-1)
         Dim Message As Message = update.message
         ID=update.updateId
         Log(Message.text)
    End If
End Sub

'1sec Timer
Sub update_tick
    bot.GetUpdatesList(ID+1,1,9000)
End Sub

But after that, each call of GetUpdatesList throws an catched error
Exception in getting updates :null

how can i avoid this?
 

DonManfred

Expert
Licensed User
Longtime User
I think you are using it wrong.

GetUpdatelist should be used with the last ID given by a recent Update.
ID=update.updateId
THIS is the id which should be used.

Additionally i would try it with a 10 Seconds interval or so. Just to make sure you get it working correctly. You may lower it when everything is working as expected.
Note that you are sending a lot of requests to Telegram service with this interval (1 sec)...
 

DonManfred

Expert
Licensed User
Longtime User
To not further fill this thread (we should have created a new thread in the questionsforum).
I suggest to switch to questionsforum.

I started playing (wrapping :D) with another Botimplementation: jTelegramBot.
It is using a "longpollingBot" which raises an Event if a new Update comes in. (No need for a timer based request!)
This is the progress so far. For any questions/want to try please create a new Thread in the questionsforum and i´ll answer there. It is another library than the one from this thread so we should talk about in another thread ;-)
B4X:
Sub JTB_onMessageReceived(api As Object, id As Int, msg As Object)
    Log($"JTB_onMessageReceived( ${id}, ${msg}"$) 
    Dim message As Message = msg
    Log($"MessageFrom: ${message.From}"$)
    Dim from As User = message.From
    Log($"FromFirstName: ${from.FirstName}"$)
    Log($"FromLastName: ${from.LastName}"$)
    Log($"FromUserName: ${from.Username}"$)
    Log($"FromID: ${from.Id}"$)
    Log($"FromisBot: ${from.isBot}"$)
    Log($"FromLanguageCode: ${from.LanguageCode}"$)
  
    Log($"MessageChat: ${message.Chat}"$)
    Dim chat As Chat = message.Chat
    Log($"ChatFirstName: ${chat.FirstName}"$)
    Log($"ChatFirstName: ${chat.LastName}"$)
    Log($"ChatUserName: ${chat.Username}"$)
    If chat.Title <> Null Then
        Log($"ChatTitle: ${chat.Title}"$)
    End If
    Log($"ChatID: ${chat.Id}"$)

    If message.Caption <> Null Then
        Log($"MessageCaption: ${message.Caption}"$)
    End If
    If message.Contact <> Null Then
        Log($"MessageContact: ${message.Contact}"$)
    End If
        Log($"MessageDate: ${message.Date}"$)
    If message.Entities <> Null Then
        Log($"MessageEntities: ${message.Entities}"$)
    End If
    Log($"MessageText: ${message.Text}"$)
End Sub
JTelegramBot (donmanfred_bot) starts in "Polling" mode.
JTB_onMessageReceived( 193433885, Message{messageId=176, from=User{id=302708740, firstName='Manfred', lastName='Ssykor', username='DonManfred'}, date=1552727473, chat=Chat{id=302708740, type=private, title='null', username='DonManfred', firstName='Manfred', lastName='Ssykor'}, forwardFrom=null, forwardFromChat=null, forwardDate=null, replyToMessage=null, editDate=0, text='6575', entities=null, audio=null, document=null, photo=null, sticker=null, video=null, voice=null, caption='null', contact=null, location=null, venue=null, newChatMember=null, leftChatMember=null, newChatTitle='null', newChatPhoto=null, deleteChatPhoto=null, groupChatCreated=null, superGroupChatCreated=null, channelChatCreated=null, migrateToChatId=null, migrateFromChatId=null, pinnedMessage=null}

MessageFrom: User{id=302708740, firstName='Manfred', lastName='Ssykor', username='DonManfred'}
FromFirstName: Manfred
FromLastName: Ssykor
FromUserName: DonManfred
FromID: 302708740
FromisBot: false
FromLanguageCode: de

MessageChat: Chat{id=302708740, type=private, title='null', username='DonManfred', firstName='Manfred', lastName='Ssykor'}
ChatFirstName: Manfred
ChatFirstName: Ssykor
ChatUserName: DonManfred
ChatID: 302708740

MessageDate: 1552727473
MessageText: 6575

PS: Der einfachheit halber evtl. im Deutschen Forum :D
For the easyness eventually in the german Forum :D
 
Last edited:

hears

Active Member
Licensed User
Longtime User
Bot.sendMessage(-1001369534349, "Hello World", 0, 0, Null)
i have try send message is ok,do not know how to recieve message

Can you please give me simple example for sending and receiving messages?
 

rasoolarj77

New Member
Thanks
But this method has only two parameters
1-chat id
2-text
I want to use other library but need i see errors for not finding necessary .jar file
 

somed3v3loper

Well-Known Member
Licensed User
Longtime User
Thanks
But this method has only two parameters
1-chat id
2-text
I want to use other library but need i see errors for not finding necessary .jar file
I have uploaded version 1.15 to the first post
Please download
I work on this wrapper from time to time for my own purposes :D so I might have added some undocumented changes and features
 

ivanomonti

Expert
Licensed User
Longtime User
Hi, this library could do for me but I see it is dated 2016, I was wondering if it is still valid or Telegram has changed APIWeb?

I should do an implementation on my management and it could be good for me, it would be nice to be able to do it also to WhastApp, but if this works I'd be happy already.

is it possible to have a simplified demo project, login + user list and how to send messages... as well as a simple library installation manual?

B4J 8.10
Java 8
Windows
 
Last edited:

somed3v3loper

Well-Known Member
Licensed User
Longtime User
Hi, this library could do for me but I see it is dated 2016, I was wondering if it is still valid or Telegram has changed APIWeb?

I should do an implementation on my management and it could be good for me, it would be nice to be able to do it also to WhastApp, but if this works I'd be happy already.

is it possible to have a simplified demo project, login + user list and how to send messages... as well as a simple library installation manual?

B4J 8.10
Java 8
Windows
This is a telegram bot api library I don't think there is a login
I've uploaded version 1.15 to the first post
Please note that this library has a lot of functions and objects and I just wrap part of them

There is a simple demo hers
 

DonManfred

Expert
Licensed User
Longtime User
how send a photo or document with this library?
Start with looking at the available methods in the docu?

  • SMMTeleBot
    Events:
    • _updates (updates As List)
    • sent (result As Message)
  • Methods:
    • GetUpdatesList (offset As Int, limit As Int, timeout As Int)
    • Initialize (Token As String, EventName As String)
    • rgetUpdatesList (offset As Int, limit As Int, timeout As Int) As List
    • sendMessage (chatId As String, MessageText As String)
    • sendPhoto (chatId As String, folder As String, filename As String, caption As String, reply_to_message_id As Integer, bForceReply As Boolean, bReplyKeyboardHide As Boolean)
    • sendVideo (chatId As String, folder As String, filename As String, caption As String, reply_to_message_id As Integer, bForceReply As Boolean, bReplyKeyboardHide As Boolean, duration As Int)
 

DonManfred

Expert
Licensed User
Longtime User
no methods to send document file for example zip or txt ...
So i guess sending a DOCUMENT is not implemented in this Library.

You can use
Here you are able to send a Document (ZIP, TXT, XLS, whatever). It is implemented in the Example.
Note that this library is not maintained as i do not use Telegram any longer.
 

seyed_27

Member
So i guess sending a DOCUMENT is not implemented in this Library.

You can use
Here you are able to send a Document (ZIP, TXT, XLS, whatever). It is implemented in the Example.
Note that this library is not maintained as i do not use Telegram any longer.
Can this Library be used in the b4a?
 
Top