B4J Library [BETA] jTelegramBot - Create your own Telegram Bot

Discussion in 'B4J Libraries & Classes' started by DonManfred, Mar 17, 2019.

  1. DonManfred

    DonManfred Expert Licensed User

    jTelegramBot is a wrapper build on this github project.
    DonManfred (Wrapper)
    Version: 0.11

    - To get the Example running you first need to adapt the Initialize line with your Bots Name and ApiToken.
    Sub AppStart (Args() As String)
    End Sub
    - The Example is using Static Filenames for the Document, MP3, Photo, Sticker send.
    Make sure to adapt the path and Filenames to match Files you want to use.

    If message.Text = "/buttons" Then
    Dim mark As InlineKeyboardMarkup
    Dim buttons As Message = jtb.sendMessage(jtb.byId(from.Id),"Click the Button!","MARKDOWN",False,False,message.MessageId,mark)
    else If message.Text = "/location" Then
    else If message.Text = "/url" Then
    "[jTelegramBot Thread](https://www.b4x.com/android/forum/threads/jtelegrambot.103778/#post-650611)","MARKDOWN",False,False,message.MessageId,Null)
    else If message.Text = "/plainurl" Then
    "Link without Preview [jTelegramBot Thread](https://www.b4x.com/android/forum/threads/jtelegrambot.103778/#post-650611)",message.MessageId,False,True,Null,jtb.parsemodeMARKDOWN)
    else If message.Text = "/text" Then
    $"Testtext <b>Bold</b>, <i>Italic</i>
        new row..."$
    else If message.Text = "/document" Then
    else If message.Text = "/photo" Then
    else If message.Text = "/audio" Then
    File.DirApp,"myaudio.mp3"),223,"Fleedwood Mac","Eyes of the World",False,message.MessageId,replbld.forceReply)
    else If message.Text = "/sticker" Then
    else If message.Text = "/start" Then
    $" Hello ${chat.Username}
    `Command   | Result
    /location | Returns a Locationobject
    /document | Returns a Document
    /photo    | Returns a Photo
    /audio    | Returns a Audiofile
    /url      | Returns a URL with Preview
    /plainurl | Returns a URL without Preview
    /buttons  | Shows some buttons To Click on
    Thank you For choosing Me :-)"$
    In this example you need to replace the following Filereferences:
    - mydocument.zip
    - myimage.png
    - myaudio.mp3
    - mysticker.webp

    Due to the Size of the needed Additional JARs they can be downloaded

    For any Question/Issue you have please Create a new Thread in the B4J Questionsforum.

    Attached Files:

    Last edited: Mar 21, 2019
    Kope, joulongleu, Kwame Twum and 10 others like this.
  2. Blueforcer

    Blueforcer Active Member Licensed User

    Hello again:)
    Get this error while compiling
    file has wrong version 55.0, should be 52.0
    Got latest B4J with with JDK 1.8.0_201
  3. DonManfred

    DonManfred Expert Licensed User

    Try the file in Post #1 again please.
    joulongleu likes this.
  4. Blueforcer

    Blueforcer Active Member Licensed User

    Jup, this works!
    Lets play with it:)
    Thank you
    DonManfred likes this.
  5. Blueforcer

    Blueforcer Active Member Licensed User

    Do you know how to avoid it, that others can use my bot?
    Everbody can find my bot in telegram and control my application^^
  6. DonManfred

    DonManfred Expert Licensed User

    Please start a new thread in the Questionsforum for any Question you have. We do not want to fill this Thread.
    Please also describe more clearly what you want to do with your bot.
    I think i already have an Idea.
    Last edited: Mar 17, 2019
    joulongleu likes this.
  7. Blueforcer

    Blueforcer Active Member Licensed User

    Sorry, Ive got it.
    Just save the chatID with the first message and return if the next messages doesnt match the saved chatID

    But related to this thread:
    Can you implement the sendAnimation function?
    Last edited: Mar 17, 2019
  8. DonManfred

    DonManfred Expert Licensed User

    even if. Please start a new Thread for any new question.
    Do you have an Example GIF or small video without sound?
    joulongleu likes this.
  9. DonManfred

    DonManfred Expert Licensed User

    Start a Chat with my bot
    and send the Command /anim :D

    Last edited: Mar 17, 2019
    inakigarm and Blueforcer like this.
  10. DonManfred

    DonManfred Expert Licensed User

    Next version will have a possibility to change the InlineKeyboard. In the 1st version only a dummykeyboard is generated inside the library which could not be changed.

    Dim ibld As InlineKeyboardButtonBuilder
    Dim mark As InlineKeyboardMarkup
    Dim buttons As Message = jtb.sendMessage(jtb.byId(from.Id),"Click the Button!","MARKDOWN",False,False,message.MessageId,mark)
    inakigarm likes this.
  11. CGP

    CGP Member Licensed User

    Thanks a lot @DonManfred !

    I've tried to add my bot into a group and hangs, cannot handle "new_chat_participant" action.

    com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "new_chat_participant" (class io.fouad.jtb.core.beans.Message), not marked as ignorable (34 known properties: "entities""new_chat_member""edit_date""animation""new_chat_photo""message_id""audio""chat""document""delete_chat_photo""contact""migrate_from_chat_id""forward_from""caption""forward_date""left_chat_member""from""text""date""venue""new_chat_title""game""location""reply_to_message""video""supergroup_chat_created""sticker""group_chat_created""forward_from_chat""pinned_message""photo""channel_chat_created""voice""migrate_to_chat_id"])
    Can you take a look?

    Many thanks
  12. DonManfred

    DonManfred Expert Licensed User

    Please create a new thread in the questionsforum for any question/Problem you have.
    I don´t want to fill this thread with questions.

    Edit to add: I´ve added the missing Property to the Message. Check V0.10 in Post #1 and try again.
    Last edited: Mar 20, 2019
  13. DonManfred

    DonManfred Expert Licensed User

    Kind of Offtopic but still related:

    Left side is a Android app which is using the TDL Database Library and act as a Client for my TelegramAccount.

    On right side my Telegram Desktop client where i am talking to my telegrambot. Message which i get in telegram desktop client (Pictures in this case) the Android client also get in an Update.

    Have fun watching :)

    js1234, BillMeyer, inakigarm and 2 others like this.
  14. Erel

    Erel Administrator Staff Member Licensed User

    Great work!
    DonManfred likes this.
  15. DonManfred

    DonManfred Expert Licensed User

    It also works for Documents (Files). The Client first get an info about the file (that it can be downloaded) and then call a method to download it.

    else if msg.Content Is MessageDoc Then
    Log("Messagecontent Document")
    Dim msgdoc As MessageDoc = msg.Content
    If msgdoc.Document <> Null Then
    Log("Document <> null")
    Dim doc As Document = msgdoc.Document
    If doc.InnerDocument <> Null Then
    Dim dfile As TdFile= doc.InnerDocument
                        Log($"FileID: ${dfile.Id}, Size: ${dfile.Size}"$)
                        Dim floc As LocalFile = dfile.Local
    Log($"Downloading active=${floc.isDownloadingActive}"$)
    Log($"Downloading Completed=${floc.isDownloadingCompleted}"$)
    If floc.canBeDownloaded And floc.isDownloadingCompleted = False Then
    End If
    The result of the downloaded file...
    "Path" is a absolute path (/path/to/file/onsdcard/file.ext)
    else if tlo Is UpdateFile Then
    Dim updFile As UpdateFile = tlo
    Dim tfile As TdFile = updFile.TdFile
    Dim lfile As LocalFile = tfile.Local
    Log("Downloading Completed: "&lfile.isDownloadingCompleted)
    Log("Downloaded Size: "&lfile.downloadedSize)
    Log("Downloaded Path: "&lfile.Path)
    In case of the images in the Video above:

    else if tlo Is UpdateNewMessage Then
    Dim newmsg As UpdateNewMessage = tlo
    Dim msg As Message = newmsg.Message
    If msg.Content Is MessagePhoto Then
    Log("Messagecontent Photo")
    Dim msgphoto As MessagePhoto = msg.Content
    Dim p As Photo = msgphoto.Photo
    Dim sizes As List = p.Sizes
    If sizes <> Null And sizes.Size > 0 Then
    For i= 0 To sizes.Size -1
    Dim phsizeObj As Object = sizes.Get(i)
    Dim phsize As PhotoSize = phsizeObj
    Dim tfile As TdFile = phsize.Photo
    If tfile.Local <> Null Then
    Dim locfile As LocalFile = tfile.Local
    Dim complpath As String = locfile.Path
    If complpath.Length > 1 Then
    Dim path As String = GetPath(complpath)
    Dim filename As String = GetFilename(complpath)
                                Log($"${filename} -> ${path}"$)
    Dim bmp As Bitmap
                                ImageView1.Bitmap = bmp
    End If

    It is quite complicated to work with the amount of Objects in this SDK. But they opens a lot of great possibilities :)
    Don Oso, somed3v3loper and Erel like this.
  16. DonManfred

    DonManfred Expert Licensed User

    While downloading you´ll get multiple events (updates) for the running download. Until the download finishes.

    Don Oso likes this.
  17. devpialkt

    devpialkt Member Licensed User

    Got error with Version: 0.11

    com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of 
    type `io.fouad.jtb.core.enums.MessageEntityType` from String "phone_number": value not one of declared Enum instance names: [italic, pre, bold, text_mention, mention, text_link, code, url, bot_command, email, hashtag]
    Thank You
  18. DonManfred

    DonManfred Expert Licensed User

    1. You should always create a new thread in the Questionsforum.

    2. You are not providing enough useful informations. Try to log the incoming update. Maybe possible to get more info about it.

    Do you know how i can reproduce it? What do i need to send/receive to get this error?

    3. See the notes in Post 1

    Edit to add:
    Try V0.13 from Post #1
    Last edited: Apr 14, 2019
    devpialkt likes this.
  19. devpialkt

    devpialkt Member Licensed User

    Great, version v0.13 WORKED !!!

    many thanks @DonManfred

  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