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.
    JTelegramBot
    Author:
    DonManfred (Wrapper)
    Version: 0.11

    Setup:
    - To get the Example running you first need to adapt the Initialize line with your Bots Name and ApiToken.
    Code:
    Sub AppStart (Args() As String)
        jtb.Initialize(
    "JTB","yourbotname","youtbottoken")
        glmap.Initialize
        jtb.startAsync
        StartMessageLoop
    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.

    Code:
    If message.Text = "/buttons" Then
            
    Dim mark As InlineKeyboardMarkup
            mark.initialize(jtb.CreateDummyKeyboard)
            
    Dim buttons As Message = jtb.sendMessage(jtb.byId(from.Id),"Click the Button!","MARKDOWN",False,False,message.MessageId,mark)
            glmap.Put(buttons.MessageId,buttons)
        
    else If message.Text = "/location" Then
            jtb.sendChatAction(jtb.byId(from.Id),jtb.ChatActionFIND_LOCATION)
            jtb.sendLocation(jtb.byId(from.Id),
    50.8337006,6.441118,False,message.MessageId,Null)
        
    else If message.Text = "/url" Then
            jtb.sendMessage(jtb.byId(from.Id),
    "[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
            bld.sendMessage(chat.Id,
    "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
            jtb.sendMessage(jtb.byId(from.Id),
    $"Testtext <b>Bold</b>, <i>Italic</i>
        new row..."$
    ,"HTML",True,False,message.MessageId,Null)
        
    else If message.Text = "/document" Then
            jtb.sendChatAction(jtb.byId(from.Id),jtb.ChatActionUPLOAD_DOCUMENT)
            jtb.sendDocument(jtb.byId(from.Id),jtb.MediaByFile(
    File.DirApp,"mydocument.zip"),"DocumentCaption",False,message.MessageId,Null)
        
    else If message.Text = "/photo" Then
            jtb.sendChatAction(jtb.byId(from.Id),jtb.ChatActionUPLOAD_PHOTO)
            jtb.sendPhoto(jtb.byId(from.Id),jtb.MediaByFile(
    File.DirApp,"myimage.png"),"DocumentCaption",False,message.MessageId,replbld.forceReply)
        
    else If message.Text = "/audio" Then
            jtb.sendChatAction(jtb.byId(from.Id),jtb.ChatActionUPLOAD_VIDEO)
            jtb.sendAudio(jtb.byId(from.Id),jtb.MediaByFile(
    File.DirApp,"myaudio.mp3"),223,"Fleedwood Mac","Eyes of the World",False,message.MessageId,replbld.forceReply)
        
    else If message.Text = "/sticker" Then
            jtb.sendSticker(jtb.byId(from.Id),jtb.MediaByFile(
    File.DirApp,"mysticker.webp"),False,message.MessageId,Null)
        
    else If message.Text = "/start" Then
            jtb.sendMessage(jtb.byId(from.Id),
    $" 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 :-)"$
    ,"MARKDOWN",False,False,message.MessageId,Null)
    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
    HERE.

    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
    Code:
    \jtelegrambot.jar(de/donmanfred/JTBwrapper.class)
        class 
    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?
    https://core.telegram.org/bots/api#sendanimation
     
    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

    https://t.me/donmanfred_bot
     
    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.

    Code:
    Dim ibld As InlineKeyboardButtonBuilder
            
    Dim mark As InlineKeyboardMarkup
            mark.initialize(ibld.Initialize.newRow.newButton(
    "1").withCallbackData("BTN1").newButton("2").withCallbackData("BTN2").newButton("3").withCallbackData("BTN3").newRow.newButton("Google").withUrl("https://www.google.com").newRow.build)
            
    Dim buttons As Message = jtb.sendMessage(jtb.byId(from.Id),"Click the Button!","MARKDOWN",False,False,message.MessageId,mark)
    [​IMG]
     
    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.

    Code:
    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"])
     at......
    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.

    Code:
    else if msg.Content Is MessageDoc Then
                
    Log("Messagecontent Document")
                
    Dim msgdoc As MessageDoc = msg.Content
                
    Log(msgdoc)
                
    If msgdoc.Document <> Null Then
                    
    Log("Document <> null")
                    
    Dim doc As Document = msgdoc.Document
                    
    Log(doc.Filename)
                    
    Log(doc.mimeType)
                    
                    
    If doc.InnerDocument <> Null Then
                        
    Dim dfile As TdFile= doc.InnerDocument
                        
    Log(dfile)
                        
    'Log(dfile.Id)
                        'Log(dfile.expectedSize)
                        Log($"FileID: ${dfile.Id}, Size: ${dfile.Size}"$)
                        
    'Log(dfile.Size)
                        Dim floc As LocalFile = dfile.Local
                        
    Log($"canBeDeleted=${floc.canBeDeleted}"$)
                        
    Log($"canBeDownloaded=${floc.canBeDownloaded}"$)
                        
    Log($"Downloading active=${floc.isDownloadingActive}"$)
                        
    Log($"downloadedPrefixSize=${floc.downloadedPrefixSize}"$)
                        
    Log($"downloadedSize=${floc.downloadedSize}"$)
                        
    Log($"Downloading Completed=${floc.isDownloadingCompleted}"$)
                        
    Log(floc.Path)
                        
    If floc.canBeDownloaded And floc.isDownloadingCompleted = False Then
                            sdk.DownloadFile(dfile.Id,
    1)
                        
    End If
    The result of the downloaded file...
    "Path" is a absolute path (/path/to/file/onsdcard/file.ext)
    Code:
    else if tlo Is UpdateFile Then
            
    Dim updFile As UpdateFile = tlo
            
    Dim tfile As TdFile = updFile.TdFile
            
    'Log(tfile.Size)
            Log(tfile.Local)
            
    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:

    Code:
    else if tlo Is UpdateNewMessage Then
            
    Dim newmsg As UpdateNewMessage = tlo
            
    Dim msg As Message = newmsg.Message
            
    Log(msg)
            
    Log(msg.senderUserId)
            
    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
                
    Log(sizes)
                
    If sizes <> Null And sizes.Size > 0 Then
                    
    For i= 0 To sizes.Size -1
                        
    Dim phsizeObj As Object = sizes.Get(i)
                        
    Log(phsizeObj)
                        
    Dim phsize As PhotoSize = phsizeObj
                        
    Log(phsize)
                        
    Dim tfile As TdFile = phsize.Photo
                        
    Log(tfile)
                        
    Log(tfile.Id)
                        
    Log(tfile.Size)
                        
    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(path)
                                Log($"${filename} -> ${path}"$)
                                
    Dim bmp As Bitmap
                                bmp.Initialize(
    path,filename)
                                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

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



     
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