Airgram

Guides

Authorization

This page describes authorization process for NodeJS environment. For the web version, you need to implement authorization yourself.

Airgram provides Auth component, which implements basic logic for authorization or registration new Telegram account.

import { Airgram, Auth } from 'airgram'

type AuthConfigOption = string | (() => string) | (() => Promise<string>)

interface AuthConfig {
  code?: AuthConfigOption
  firstName?: AuthConfigOption
  lastName?: AuthConfigOption
  phoneNumber?: AuthConfigOption
  password?: AuthConfigOption
  token?: AuthConfigOption
}

const authConfig: AuthConfig = {
  // options
}

const airgram = new Airgram()

// Use authorization middleware
airgram.use(new Auth(authConfig))

Sign in as a bot

You need to specify bot secret token:

import { Airgram, Auth } from 'airgram'

const airgram = new Airgram(/* options */)

airgram.use(new Auth({
  token: 'xxx'
}))

Sign in as a user

You need to specify the phone number and the secret code received from Telegram:

import { Airgram, Auth, prompt } from 'airgram'

const airgram = new Airgram()

airgram.use(new Auth({
  phoneNumber: () => prompt(`Please enter your phone number:\n`),
  code: () => prompt(`Please enter the secret code:\n`)
}))

Sign up

In addition to your phone number, you must provide the user name:

new Auth(airgram, {
  phoneNumber: () => prompt(`Please enter your phone number:\n`),
  code: () => prompt(`Please enter the secret code:\n`),
  firstName: () => prompt(`Please enter your first name:\n`),
  secondName: () => prompt(`Please enter your second name:\n`)
})

Logout

For proper logout and closing the TDLib instance use the logout API method. All local data will be destroyed. After the logout completes, updateAuthorizationState with authorizationStateClosed will be sent.

await airgram.api.logOut()