Interacting with Telegram API
Representation of Telegram API Objects
-
MTKruto represents the functions and the constructors of Telegram API in a bare object with its “_” key set to the name of the constructor according to the API schema.
-
An instance of
inputPeerSelf
is represented as follows:
{
_: "inputPeerSelf";
}
- Other keys of the object can be the parameters of the constructor with the
same casing as in API scheme files. So, a
peerUser
instance with itsuser_id
parameter set to1234
is represented like:
{ _: "peerUser", user_id: 1234n }
- Parameters of the type
long
are represented as big ints.
// constructor1#FFFFFF param:bigint
{
_: "constructor1",
param: 1234n,
}
// constructor1#FFFFFF param:int duration:double
{
_: "constructor1",
param: 10,
duration: 1.0,
}
- Parameters of the type
string
are represented as strings.
// constructor1#FFFFFF name:string
{
_: "constructor1",
name: "MTKruto",
}
- Parameters of the type
bytes
are represented as instances ofUint8Array
.
// constructor1#FFFFFF data:bytes
{
_: "constructor1",
data: new Uint8Array([77, 84, 75, 114, 117, 116, 111]),
}
- Parameters of the type
flags.N?true
are either omitted from the object or have the valuetrue
.
// constructor1#FFFFFF flags:# check:flags.0?true
{
_: "constructor1",
check: true,
}
// or omit `check`
{
_: "constructor1",
}
- Parameters that are other constructors are also represented the same way.
// constructor2#EEEEEE
// constructor1#FFFFFF param:constructor2
{
_: "constructor1",
param: {
"_": "constructor2",
},
}
- Parameters like
flags:#
are never provided explicitly. They are processed internally.
Type Declarations
Type declaraions for Telegram API constructors are accessible from the Api
namespace, which is exported from the root.
- All types have the same casing as the API schema. So the types and functions are all-lower, and enums are first-upper.
- When accessing type declarations, the periods in original identifiers must be
replaced with an underscore. So, the declaration for
updates.updates
is atApi.updates_updates
, and so on.
Utilities
To make dealing with these kinds of objects easier, you can use the built-in utilities.
- Use
is()
to compare types.
if (is("user", object)) {
/* ... */
}
- Use
isOfEnum()
to see if a specific object is a member of a specific enum.
if (isOfEnum("Updates", object)) {
/* ... */
}
Calling Functions
To call Telegram API functions, use the client’s invoke()
method. It takes an
instance of a function object, and returns a promise resolving to the result.
const config = await client.invoke({ _: "help.getConfig" });