B4J Library Telegram Bots API

Discussion in 'B4J Libraries & Classes' started by somed3v3loper, May 2, 2016.

  1. somed3v3loper

    somed3v3loper Well-Known Member Licensed User

    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
    Code:
    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


    Code:
    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 .
     

    Attached Files:

    Last edited: Jun 24, 2018
    fredo, DonManfred, hibrid0 and 13 others like this.
  2. u2005k

    u2005k Member Licensed User

    Hi,
    Any further updates on this Library?

    Thanks & regards...
    Uday
     
  3. somed3v3loper

    somed3v3loper Well-Known Member Licensed User

    I am sorry I was planning to update the library but got busy.
    Do you want a specific feature so I can try to add it ?
     
  4. u2005k

    u2005k Member Licensed User

    Hi,
    Thanks a for quick reply. I don't think I need any special feature right now. Let me try it out and see if any additional features are required.
    If I understand it correctly in the example code, I need to poll updates. If I want to use Webhook then I guess I need to build WebApp in B4J and request handler will be webhook url in telegram bot settings. I will get message in JSON format which I can parse and use your library to extract various pieces and respond. It will be great if you can assemble sample bot handler with different types like (Audio, Image, Location) or complete bot app but I know it will consume your time and I don't want you to consume your time unnecessarily. Let me give it a try and if I am successful in building good example then I will post it in the forum.
    If you are interested in custom project, please PM me.

    Thanks & regards...
    Uday
     
  5. u2005k

    u2005k Member Licensed User

    I tried it and I get run time error with sample code.
    Code:
    Sub AppStart (Args() As String)
       
       
        
    Dim DoneIndiaBotKey As String = "My BOT API Key"
        
    Dim tele As SMMTeleBot
        tele.Initialize(DoneIndiaBotKey, 
    "updates")
       
       
       
        tele.getUpdatesList(
    0,20,9000)
        StartMessageLoop
    End Sub

    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
    Program started.
    retrofit.RetrofitError: Read timed out
    at retrofit.RetrofitError.networkError(RetrofitError.java:27)
    at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:395)
    at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
    at com.sun.proxy.$Proxy0.getUpdates(Unknown Source)
    at com.pengrad.telegrambot.TelegramBot.getUpdates(TelegramBot.java:139)
    at smm.telebot.smmtelebot.rgetUpdatesList(smmtelebot.java:214)
    at smm.telebot.smmtelebot$3.call(smmtelebot.java:205)
    at smm.telebot.smmtelebot$3.call(smmtelebot.java:199)
    at anywheresoftware.b4a.BA$4.run(BA.java:207)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:170)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
    at sun.security.ssl.InputRecord.read(InputRecord.java:503)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
    at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:930)
    at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:704)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1536)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
    at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
    at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
    at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
    ... 12 more

    What's wrong with this?
    Thanks a lot for your help.
    Regards...
    Uday
     
  6. somed3v3loper

    somed3v3loper Well-Known Member Licensed User

    Is telegram working at the time of this error ?
    As it is a timeout error , it could be related to telegram servers .
     
  7. u2005k

    u2005k Member Licensed User

    Yes, it is working (I mean, Telegram & Bot is working) ... I have tried same Bot in other PHP library and I get updates in PHP. In B4J code, I either get error (dump was sent earlier) or nothing happens even if there are updates (Sub tele_updates(updates As List) never get fired)

    Thanks a lot.

    Uday
     
  8. somed3v3loper

    somed3v3loper Well-Known Member Licensed User

    Sorry for that .
    I tried the library and I got another error so I downloaded a new version of GitHub library and started re-wrapping it .
     
    Husam and amir7900 like this.
  9. somed3v3loper

    somed3v3loper Well-Known Member Licensed User

    A new update with timeout setting is added to the first post . Can you try it ? Sorry for taking too much time :)
     
  10. MTV

    MTV Member Licensed User

    Thanks for this awesome library!
    please wrap Keyboards :)
     
  11. hibrid0

    hibrid0 Active Member Licensed User

    hi, thanks for your lib. Can you show how can send messages to the users?
    I see tele.sendMessage and ask for a ChatID and I dont know from where I get it.
     
  12. somed3v3loper

    somed3v3loper Well-Known Member Licensed User

    Can you check new update 106?

    I think you can get chatId from an update .
     
    hibrid0 likes this.
  13. hibrid0

    hibrid0 Active Member Licensed User

    Yes thank you, I get it from the chat.
    Now I have other problem.
    Only get the messages with update on the start of application, but now when I send new messages.
    New messages not showed on the log, but when I stop and start again I see the old messages.

    For test I just try to make my bot for repeat what I say.

    After, I want to make queries connected to a local database.

    COMMAND1 PARAM1

    Param1 is an internal number on our database.

    I need to extract more info and show private info.

    I thinking on use a command something like password.

    CommandPSW password.
    If password is OK, then add this user to internal table to show sensible info.

    Please show me the way I need to follow.
     
    Last edited: Jan 18, 2017
  14. jrat

    jrat Member Licensed User

    I tried use lib. Copy code from thread header post.
    When program run:
    Waiting for debugger to connect...
    Program started.
    java.lang.NullPointerException
    at smm.telebot.smmtelebot.rgetUpdatesList(smmtelebot.java:274)
    at smm.telebot.smmtelebot$3.call(smmtelebot.java:247)
    at smm.telebot.smmtelebot$3.call(smmtelebot.java:241)
    at anywheresoftware.b4a.BA$4.run(BA.java:271)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    Help please.
     
  15. somed3v3loper

    somed3v3loper Well-Known Member Licensed User

    Sorry if I could not help as it's been a long time since I last worked on this library :)
    I am not sure if it is still working with new telegram API .
    I will try to find this library and try it again
     
  16. somed3v3loper

    somed3v3loper Well-Known Member Licensed User


    It seems working .
    Can you share your code ?
     
  17. jrat

    jrat Member Licensed User

    Thanks, i found my error. Token must be 123:assdsdfgdFGDFgrd I mistake only assdsdfgdFGDFgrd
     
    somed3v3loper likes this.
  18. GanjaKyp

    GanjaKyp Member

    Library is still working?
     
  19. somed3v3loper

    somed3v3loper Well-Known Member Licensed User

    I think it is still working but have not fully tested it .
     
  20. GanjaKyp

    GanjaKyp Member

    Can you please give me simple example for sending and receiving messages?
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice