tdlnode

Telegram nodejs client https://github.com/tdlib/td

Usage no npm install needed!

<script type="module">
  import tdlnode from 'https://cdn.skypack.dev/tdlnode';
</script>

README

tdlnode

Telegram nodejs client https://github.com/tdlib/td

Requirements

  1. Node.js >= 10 preferred (minimum >= 9.0.0)
  2. TDLib binary https://github.com/tdlib/td

Getting started

const path = require('path')
const { Client } = require('tdlnode')

const api_id = 'your api_id'
const api_hash = 'your api_hash'
const phone_number = '+12345679021' // or const token = 'your token'

const configuration = {
    path_to_binary_file: path.resolve(__dirname, '../lib/libtdjson'),
    database_directory: path.resolve(__dirname, '../storage'),
    files_directory: path.resolve(__dirname, '../downloads'),
    log_file_path: path.resolve(__dirname, '../logs/tdl.log'),
}

const up = async () => {
    const client = new Client({ api_id, api_hash, phone_number }, configuration)

    await client.init()

    const chats = await client.fetch({
        '@type': 'getChats',
        'offset_order': '9223372036854775807',
        'offset_chat_id': 0,
        'limit': 100,
    })

    console.log(chats)

    client.stop()
}

up()

Events

Subscribe to:

const callback = msg => {console.log('event', msg)}

client.on('updateOption', callback)

client.once('updateOption', callback)

client.off('updateOption', callback)

More examples

https://github.com/fonbah/tdlnode/tree/master/examples

Options

const conf = {
    path_to_binary_file: path.resolve(__dirname, '../lib/libtdjson'), //default 'libtdjson'
    enable_storage_optimizer: true, //default true
    use_message_database: true, //default true
    use_secret_chats: false, //default false
    system_language_code: 'en', //default 'en'
    application_version: version, //default pacage.json version
    device_model: 'nodejs', //default 'nodejs'
    system_version: '10', //default 10
    database_directory: path.resolve(__dirname, '../storage'), //default './storage'
    files_directory: path.resolve(__dirname, '../downloads'), //default './downloads'
    log_file_path: path.resolve(__dirname, '../logs/tdl.log'), //default './logs/tdl.log'
    use_test_dc: true, //default false If set to true, the Telegram test environment will be used instead of the production environment. 
    verbosity_level: 10, //default 2
    fatal_error_callback: e => { console.log(e) }, //default console.log
}

Client input handling

const types = client.input.getTypes()

const verificationCodeHandler = resolve => {
    const code = 'get somehow your code here'
    resolve(code)
}

const inputHandler = (type, resolve) => {
    switch (type) {
        case 'code':
            verificationCodeHandler(resolve)
            break
    }
}
const unsubscribe = client.input.subscribe(inputHandler)

full example https://github.com/fonbah/tdlnode/blob/master/examples/input.js

Errors handling

client.on('error', update => {
    console.log('client error', update)
    if (update.code == 429) {
        client.stop()
    }
})

Getting started with telegram TDLib