Airgram

Guides

Constants

Telegram API contains so many types of objects and updates. It's easy to make a typo or miss some changes when new version of TDLib is coming. To prevent it, Airgram provides a package with names of all the types, predicates and updates.

Installation

npm install @airgram/constants

Usage

Please check out repository to get full list of the constants.

In the following example we import UPDATE and AUTHORIZATION_STATE enums and use them instead of string values:

import { UPDATE, AUTHORIZATION_STATE } from '@airgram/constants'

airgram.on(UPDATE.updateAuthorizationState, async ({ update }, next) => {
  const { authorizationState } = update

  switch (authorizationState._) {
    case AUTHORIZATION_STATE.authorizationStateWaitPhoneNumber: {
      await airgram.api.setAuthenticationPhoneNumber({
        phoneNumber: '1234567890'
      })
      break
    }
    case AUTHORIZATION_STATE.authorizationStateWaitCode: {
      await airgram.api.checkAuthenticationCode({
        code: '1234'
      })
      break
    }
    case AUTHORIZATION_STATE.authorizationStateReady: {
      console.log('Success authorization!')
      return next()
    }
    default: {
      return next()
    }
  }
})

In WebStorm IDE you may get a warning: "Unreachable 'case' branch of a 'switch' statement". Looks like this is a bug of the internal code inspection. You can just switch off the related rule.