Android Question Am I going the right way ?

Discussion in 'Android Questions' started by somed3v3loper, Jul 26, 2019.

  1. somed3v3loper

    somed3v3loper Well-Known Member Licensed User

    Hello all ,
    I am trying/planning to make a B4X version of Telegram bots Api by accessing the REST API but I am not sure I am doing it the right way ?
    For example , I am using custom types for telegram types like this
    Type User (id As Int,is_bot As Boolean,first_name As String,last_name As String,username  As String,language_code  As String)
    The problem is in casting as the result in a json string , I am writing every item like
    Case "GetMe"
    Dim u As User
    Dim jn As JSONParser
    Dim umap As Map = jn.NextObject.Get("result")
                u.first_name = umap.Get(
                u.is_bot = umap.Get(
       = umap.Get(
                u.username = umap.Get(
    If SubExists(callbac,evname&"_me"Then
    End If
    Should I go ahead and do all other types and functions in this way or do you guys have better suggestions/solutions ?

  2. Erel

    Erel Administrator Staff Member Licensed User

    Moved to the questions forum.

    The answer is that it depends. How many custom types will you need to write? What are you doing with these types?
    Another option is to keep them as Maps.
    somed3v3loper likes this.
  3. DonManfred

    DonManfred Expert Licensed User

    I am using another Telegrambot wrapper. The api is using around 50 Customtypes for the diffefent messages, Chats, Groups. Video, Picture, thumbnails, InlineKeyboard and so on.

    My Telegrambot is based on jTelegramBot. Internally it is using Jackson Annotations to map the json results into the different types.

    As example here is the "PhotoSize" CustomType.
     * The MIT License (MIT)
     * Copyright (c) 
    2016 Fouad Almalki
     * Permission 
    is hereby granted, free of charge, to any person obtaining a copy
     * of this software 
    and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     * copies of the Software, 
    and to permit persons to whom the Software is
     * furnished 
    to do so, subject to the following conditions:
     * The above copyright notice 
    and this permission notice shall be included in all
     * copies 
    or substantial portions of the Software.
     * FITNESS 
     * AUTHORS 
     * OUT OF 
     * SOFTWARE.

    package io.fouad.jtb.core.beans;

    import com.fasterxml.jackson.annotation.JsonProperty;

     * This object represents one size of a photo 
     * a 
    file / sticker thumbnail.
    public class PhotoSize
         * Unique identifier 
    for this file.
    private String fileId;
         * Photo width.
    private int width;
         * Photo height.
    private int height;
         * Optional. TelegramFile size.
    private Integer fileSize;
         * Optional. 
    File path. Use<token>/<file_path> to get the file.
    private String filePath;
    public PhotoSize(){}
        public PhotoSize(String fileId, int width, int height, Integer fileSize, String filePath)
            this.fileId = fileId;
            this.width = width;
            this.height = height;
            this.fileSize = fileSize;
            this.filePath = filePath;
        public String getFileId(){return fileId;}
        public int getWidth(){return width;}
        public int getHeight(){return height;}
        public Integer getFileSize(){return fileSize;}
        public String getFilePath(){return filePath;}
        public boolean equals(Object o)
            if(this == o) return true;
            if(o == null || getClass() != o.getClass()) return false;
            PhotoSize photoSize = (PhotoSize) o;
            if(width != photoSize.width) return false;
            if(height != photoSize.height) return false;
            if(fileId != null ? !fileId.equals(photoSize.fileId) : photoSize.fileId != null) return false;
            if(fileSize != null ? !fileSize.equals(photoSize.fileSize) : photoSize.fileSize != null) return false;
            return filePath != null ? filePath.equals(photoSize.filePath) : photoSize.filePath == null;
        public int hashCode()
            int result = fileId != null ? fileId.hashCode() : 0;
            result = 31 * result + width;
            result = 31 * result + height;
            result = 31 * result + (fileSize != null ? fileSize.hashCode() : 0);
            result = 31 * result + (filePath != null ? filePath.hashCode() : 0);
            return result;
        public String toString()
            return "PhotoSize{" +
                    "fileId='" + fileId + '\'' +
                    ", width=" + width +
                    ", height=" + height +
                    ", fileSize=" + fileSize +
                    ", filePath='" + filePath + '\'' +
    i do have a wrapper for this Type in my librarywrap.
    public class PhotoSizewrapper extends AbsObjectWrapper<PhotoSize>
    Same applies to all Types i am using in my Library.

    You can reach my Bot (it it is running (not always the case, only in Development))
    Start a conversation with him and write "/start" for example. Or
    - /buttons
    - /photo
    - /url
    - /document

    Last note: I can share the Librarywrapper code with you if you are interested to see.

    Last edited: Jul 28, 2019
  4. somed3v3loper

    somed3v3loper Well-Known Member Licensed User

    Thank you very much for the last note :) you are always generous with us
    I have wrapped another library for telegram and I did the same for Types but now I wanted some B4x code that can be used and modified easily by anyone using B4X language
    I already went with using Maps before Erel suggested that and I think it might be Ok , the type is a class that holds a map check this (User type) :
    Sub Class_Globals
    '    Private fx As JFX
        Private mmap As Map
    End Sub

    'Initializes the object. You can add parameters to this method if needed.
    Public Sub Initialize(umap As Map)
    End Sub
    Public Sub getid() As Int
    Return mmap.Get("id")
    End Sub
    Public Sub getisBot As Boolean
    Return mmap.Get("is_bot")
    End Sub

    Public Sub getfirst_name As String
    Return mmap.Get("first_name")
    End Sub

    Public Sub getlast_name As String
    Return mmap.Get("last_name")
    End Sub
    Public Sub getusername As String
    Return mmap.Get("username")
    End Sub

    Public Sub getlanguage_code As String
    Return mmap.Get("language_code")
    End Sub
    I might share the project but it is still way far from complete
    DonManfred likes this.
  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