[Gambas-user] Request for advice

T Lee Davidson t.lee.davidson at gmail.com
Tue Jul 13 20:37:10 CEST 2021

On 7/13/21 12:53 PM, John Dovey wrote:
> I was under the impression that I would need to create a new object/class for each of the "objects" which the app defines. On 
> the page https://core.telegram.org/bots/api#user <https://core.telegram.org/bots/api#user> you will see the "available types" 
> listed. Each one is _called_ an object. How would I implement those? Each "object" has a list of properties. When requested from 
> the api, each one is returned (or set) as a JSON string. Some objects include others.. such as the message 
> <https://core.telegram.org/bots/api#message> which includes the user <https://core.telegram.org/bots/api#user> (multiple times), 
> the chat <https://core.telegram.org/bots/api#chat> (multiple times), the message <https://core.telegram.org/bots/api#message>, 
> an array of messageentity <https://core.telegram.org/bots/api#messageentity> objects, etc etc.
> Would it make sense to have bot.message which includes bot.message.fromuser , bot.message.forwardfromuser or rather 
> bot.message(fromuser, forwardfrom) (fromuser and forwardfrom are both user objects)? Also, I don't know how to do that.
> Sorry to be an absolute pain, but I am truly lost. If I can't figure this out, it's back to spagetti dotnet VB code...

The API documentation refers to them as objects because they are JSON objects. That doesn't necessarily mean that they correlate 
to, or need to be represented by, Gambas objects.

One main difference between objects and collections is objects can contain methods, Collections cannot. If a set of related data 
does not also require one or more functions (methods) to manage or manipulate that data, I prefer to use Collections. Others 
prefer to use objects. Once created, the manner of accessing data in either one is a matter of syntax, ie. Chat["id"] vs. Chat.id.

The reason I try to prefer Collections over objects is that they are simpler and don't require another file in my project. 
However, for more complex projects (such as this TG API appears to present) it is quite handy to have the various data objects 
defined in a location which is easy to find; which would be in a descriptively named class as opposed to buried among many 
others in, for example, a single module. And, for data structures that are used multiple times, classes are handy for that too.

The Message object has quite a few fields. For simplicity's sake, let's say it has just three: id, user, and chat. Since 'id' is 
an integer, there's no need to create a 'type' for it. So then we would need to create classes for User and Chat.

You've already done User as tb_BotMe.class (which I did not realize when I said the two should be one and apologize if that 
confused you):
' Gambas class file

Property id As String Use $id
Property is_bot As Boolean Use $is_bot
Property first_name As String Use $first_name
Property username As String Use $username
Property can_join_groups As Boolean Use $can_join_groups
Property can_read_all_group_messages As Boolean Use $can_read_all_group_messages
Property supports_inline_queries As Boolean Use $supports_inline_queries

That is the *entirety* of the file. With the use of the "Use" keyword, all of the respective Read() and Write() functions are 
automatically created internally. Also, unless you are creating a component, the Export keyword is not required.

The Chat object has a few more fields. Its 'photo' field is of type ChatPhoto; which I suspect is used only once in the Chat 
object. And, it appears simple enough to be represented by a Collection, or an object if you wish - your choice. The class 
definition through the first seven properties could be:

' Gambas class file

Property id as Integer Use $id
Property type as String Use $type
Property title as String Use $title
Property username as String Use $username
Property first_name as String Use $first_name
Property last_name as String Use $last_name
Property photo as ChatPhoto Use $photo

Suppose the definition for the 'photo' property was:
Property photo as Collection Use $photo

Which is clearer to you (or possibly anyone reading your code)? Which do you think would be easier to maintain?

And, again, with "Use", we do not need to explicitly define the getter and setter functions _unless_ we need to override any of 
their read or write behavior.

Given the syntax, "bot.message(fromuser, forwardfrom)", 'message' would be a method, not a property, of bot. So, the formats 
bot.message.fromuser and bot.message.forwardfromuser make more sense for data access.

And, finally, you're not being a pain. What do you think this list is for? :-) I just hope I have helped instead of causing you 
(even more?) confusion.


More information about the User mailing list