Starting the Client

Here you will learn how you can construct and start a client assuming that you have already installed MTKruto.

Acquiring API Credentials

For your client to be able to authorize accounts, you need to have your own Telegram API credentials. To do that, you should create an application.

  1. Visit my.telegram.org/apps.
  2. Enter your phone number and login.
  3. Fill in the form.
  4. Click “Create application.”

You should then be moved to somewhere where you can see your app’s API ID and API hash.

It is recommended that these credentials are not leaked.

Constructing the Client

Client is the most essential class—it represents an MTProto client. Through it, you can interact with Telegram.

In fact, the constructor has no required arguments, but you usually pass the first three arguments unless you know what you are doing. The first argument is a storage adapter, the second and third ones are your app’s credentials, and the last one is an object of other parameters.

The following examples show you how you can construct a client that uses a localStorage storage adapter.

import { Client, StorageLocalStorage } from "https://deno.land/x/mtkruto@0.1.800/mod.ts";

const client = new Client({
  storage: new StorageLocalStorage("my_client"),
  // Replace these with your app's credentials.
  apiId: 123456,
  apiHash: "",
});

Connecting the Client and Authorizing an Account

Use the start method to connect the client and authorize an account.

If you pass no arguments to it, you will be requested the authorization credentials in runtime. You can also design your own user authorization experience by specifying resolver functions for each authorization step. For bots, it is recommended that the bot token is passed directly. If an account is already authorized or all credentials are passed directly, no credentials will be requested.

Here is how you can authorize a bot with its token.

await client.start({ botToken: "1234567890:AABCDEFGHIJKLMNOP" });

And here is an example on how you can use your own resolver functions for authorizing a user.

// The functions may also return promises.
await client.start({
  phone: () => prompt("Enter your phone number:")!,
  code: () => prompt("Enter the code you received:")!,
  password: () => prompt("Enter your account's password:")!,
});

console.log("Started.");

Now try running your code and see if the “Started.” message is logged. If it did, you are good to go!