Read telegram channel messages
Asked Answered
D

3

12

So I need to read all new messages of one specific channel that I'm in (not as an admin). I searched for different client apis (.NET, PHP, nodejs) but none of them helped.

Do you have any idea how I could do this?

Thanks!

Decathlon answered 22/10, 2016 at 14:15 Comment(2)
Well that's the problem. Reading channel messages is really the only thing I want to do so i was hoping for a telegram client in C#, VB, PHP, Java or Nodejs that I can use for this.Decathlon
See this answer.Flub
E
8

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')
    })
})
Endoskeleton answered 20/7, 2017 at 20:50 Comment(3)
is there a way to receive posts from a specific channel using pytg.Hetaera
@Endoskeleton where is the channel you are receiving messages from? Or are you only receiving direct messages?Riyal
what is the socket file?Aroma
C
0

Try Telethon. https://github.com/LonamiWebs/Telethon It allows you to read messages as telegram user.

Compensable answered 24/11, 2023 at 10:21 Comment(1)
any affiliation? /help/promotionSurratt
M
-2

First step is to add the telegram bot as channel admin if not you can't read channel messages!!!

Morale answered 10/6, 2017 at 13:14 Comment(1)
If ypu can use python then you may wish to use telethonHypergolic

© 2022 - 2024 — McMap. All rights reserved.