Android Question Am I going the right way ?

somed3v3loper

Well-Known Member
Licensed User
Longtime 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
B4X:
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
B4X:
        Case "GetMe"
            Dim u As User
            u.Initialize
            Dim jn As JSONParser
            jn.Initialize(job.GetString)
            Dim umap As Map = jn.NextObject.Get("result")
            u.first_name = umap.Get("first_name")
            u.is_bot = umap.Get("is_bot")
            u.id = umap.Get("id")
            u.username = umap.Get("username")
            u.last_name=umap.Get("last_name")
            u.language_code=umap.Get("language_code")
            If SubExists(callbac,evname&"_me") Then
                CallSub2(Main,evname&"_me",u)
            End If

Should I go ahead and do all other types and functions in this way or do you guys have better suggestions/solutions ?

Regards
 

DonManfred

Expert
Licensed User
Longtime User
How many custom types will you need to write?
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.

The problem is in casting as the result in a json string
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.
B4X:
/*
 * 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.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package io.fouad.jtb.core.beans;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * This object represents one size of a photo or
 * a file / sticker thumbnail.
 */
public class PhotoSize
{
    /**
     * Unique identifier for this file.
     */
    @JsonProperty("file_id")
    private String fileId;
 
    /**
     * Photo width.
     */
    @JsonProperty("width")
    private int width;
 
    /**
     * Photo height.
     */
    @JsonProperty("height")
    private int height;
 
    /**
     * Optional. TelegramFile size.
     */
    @JsonProperty("file_size")
    private Integer fileSize;
 
    /**
     * Optional. File path. Use https://api.telegram.org/file/bot<token>/<file_path> to get the file.
     */
    @JsonProperty("file_path")
    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;}
 
    @Override
    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;
    
    }
 
    @Override
    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;
    }
 
    @Override
    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.
B4X:
@ShortName("PhotoSize")
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))
https://telegram.me/donmanfred_bot
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.

telegrambot086.png
 
Last edited:
Upvote 0

somed3v3loper

Well-Known Member
Licensed User
Longtime 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.



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

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) :
B4X:
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)
    mmap.Initialize
    mmap=umap
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
 
Upvote 0
Top