Skip to content

Connection

Ilya Perkovec edited this page Sep 2, 2016 · 4 revisions

Class: Connection

Client.connect(connection => {
  connection.on('message', msg => {
    if (msg.text === 'ping') {
      msg.reply('pong') // or connection.reply(msg.id, 'pong')
    }
  });

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

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

Instance Methods

connection.send(peer, message)

  • peer String
  • message String
connection.on('message', msg => {
  connection.send(msg.to.print_name, 'pong');
});

Send message to peer.

connection.forward(peer, message_id)

  • peer String
  • message_id String
connection.on('message', msg => {
  connection.forward(msg.to.print_name, msg.id);
});

Forward message by message_id to peer.

connection.reply(reply_id, message)

  • reply_id String
  • message String
connection.on('message', msg => {
  connection.reply(msg.id, 'pong');
});

Reply to message by id with message.

connection.deleteMsg(message_id)

  • message_id String
connection.on('message', msg => {
  connection.deleteMsg(msg.id);
});

Delete message by message_id.

connection.sendImage(peer, path)

  • peer String
  • path String
connection.on('message', msg => {
  connection.sendImage(msg.to.print_name, 'telegram.png');
});

Send image from path to peer.

connection.sendDocument(peer, path)

  • peer String
  • path String
connection.on('message', msg => {
  connection.sendDocument(msg.to.print_name, 'telegram.txt');
});

Send document from path to peer.

connection.userInfo(peer_id)

  • peer_id String
connection.on('message', msg => {
  connection.userInfo(msg.from.id).then(data => {
    console.log(data)
  });
});

Get info about user by peer_id.

connection.contactList()

Returns:

connection.on('message', msg => {
  if (msg.text === 'ping') {
    connection.contactList().then(data => {
      console.log(data[0]);
    });
  }
});

Get contact list.

connection.dialogList()

Returns:

connection.on('message', msg => {
  if (msg.text === 'ping') {
    connection.dialogList().then(data => {
      console.log(data[0]);
    });
  }
});

Get dialog list.

Instance Events

Event: 'message'

Returns:

Event: 'disconnect'

Emitted when Connection close connection with tg-cli process.

Event: 'error'

Returns:

  • 'error' Object

Emitted when handled error.

Clone this wiki locally