Skip to content

TelegramAPI

Ilya Perkovec edited this page Sep 2, 2016 · 1 revision

TelegramAPI

Init tg-cli wrapper.

const TelegramAPI = require('tg-cli-node');
const config = require('./config');

// create tg-cli-node instance with configs 
const Client = new TelegramAPI(config);

// spawn tg-cli process and connect to socket
Client.connect(connection => {
  connection.on('message', msg => {
    if (msg.text === 'ping') {
      msg.reply('pong')
    }
  });

  connection.on('error', e => {
    console.log('Error from Telegram API:', e);
  });

  connection.on('disconnect', () => {
    console.log('Disconnected from Telegram API');
  });
});

Class: TelegramAPI

It creates a new TelegramAPI with configs.

const path = require('path');
const TelegramAPI = require('tg-cli-node');

const Client = new TelegramAPI({
  telegram_cli_path: path.join(__dirname, 'tg/bin/telegram-cli'),
  telegram_cli_socket_path: path.join(__dirname, 'socket'),
  server_publickey_path: path.join(__dirname, 'tg/tg-server.pub')
});

new TelegramAPI(configs)

  • configs Object
    • telegram_cli_path String - path to tg-cli
    • telegram_cli_socket_path String - path for socket file
    • server_publickey_path String - path to server key (traditionally, in tg/tg-server.pub)

Instance Methods

client.connect(callback)

  • callback Function - called after connecting to tg-cli process with Connection argument.

Spawn tg-cli process and connect to him by socket.