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

Discussion in 'B4J Libraries & Classes' started by DonManfred, Mar 17, 2019 at 5:29 PM.

  1. DonManfred

    DonManfred Expert Licensed User

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

    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.
     

    Attached Files:

    Last edited: Mar 17, 2019 at 6:14 PM
  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.
     
  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 at 6:59 PM
  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 at 7:41 PM
  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?
     
  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 at 9:05 PM
    inakigarm and Blueforcer like this.
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