Reading Telegram Channel Messages

Therefore, I need to read all the new posts of one particular channel in which I participate (not as an admin). I was looking for different client apis (.NET, PHP, nodejs), but none of them helped.

Do you have any idea how I can do this?

Thank!

+4
source share
2 answers

Here is how I did it:

Install Telegram https://github.com/vysheng/tg

Install Cli wraper https://github.com/luckydonald/pytg

from pytg import Telegram
from pytg.utils import coroutine
tg      = Telegram(  telegram="./tg/bin/telegram-cli", pubkey_file="./tg/tg-server.pub")
receiver    = tg.receiver
QUIT = False
@coroutine
def main_loop():
  try:
    while not QUIT:
      msg = (yield) # it waits until it got a message, stored now in msg.
      if msg.text is None:
        continue
      print(msg.event)
      print(msg.text)
  except GeneratorExit:
    pass
  except KeyboardInterrupt:
    pass
  else:
    pass

receiver.start()
receiver.message(main_loop())   

NodeJS Version:

const path = require('path');
const TelegramAPI = require('tg-cli-node');
const config = {
    telegram_cli_path: path.join(__dirname, 'tg/bin/telegram-cli'), //path to tg-cli (see https://github.com/vysheng/tg)
    telegram_cli_socket_path: path.join(__dirname, 'socket'), // path for socket file
    server_publickey_path: path.join(__dirname, 'tg/tg-server.pub'), // path to server key (traditionally, in %tg_cli_path%/tg-server.pub)
}

const Client = new TelegramAPI(config)

Client.connect(connection => {
    connection.on('message', message => {
        console.log('message : ', message)
        console.log('message event : ', message.event)
        console.log('message text : ', message.text)        
        console.log('message from :', message.from)
    })
    connection.on('error', e => {
        console.log('Error from Telegram API:', e)
    })
    connection.on('disconnect', () => {
        console.log('Disconnected from Telegram API')
    })
})
+4
source

The first step is to add a telegram bot as the channel administrator if you cannot read the channel messages !!!

0
source

Source: https://habr.com/ru/post/1658539/


All Articles