Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scraping Telegram Messages in Telethon Using Channel ID

I am trying to scrape new messages from a Telegram channel I am a member of. I have the ID and invite link but not the actual address.

The code below works fine with the Reuters channel I am using to test.

Is it possible to use the ID or invite link instead of the actual address?

import configparser
import json
import re
from telethon.errors import SessionPasswordNeededError
from telethon import TelegramClient, events, sync
from telethon.tl.functions.messages import (GetHistoryRequest)
from telethon.tl.types import (
PeerChannel
)

api_id = '*******'
api_hash = '**************************'
client = TelegramClient('anon', api_id, api_hash)

user_input_channel = 'https://t.me/ReutersWorldChannel'

@client.on(events.NewMessage(chats=user_input_channel))
async def newMessageListener(event):
    newMessage = event.message.message
    print(newMessage)

with client:
    client.run_until_disconnected()
like image 826
James Avatar asked Jan 22 '26 08:01

James


1 Answers

If you have the chat_id of the correct channel, then yes you can get the messages.


import configparser
import json
import re
from telethon.errors import SessionPasswordNeededError
from telethon import TelegramClient, events, sync
from telethon.tl.functions.messages import (GetHistoryRequest)
from telethon.tl.types import (
PeerChannel
)

api_id = '*******'
api_hash = '**************************'
client = TelegramClient('anon', api_id, api_hash)

chat_ids = [-100123562772, -55627728]

@client.on(events.NewMessage(chats=chat_ids))
async def newMessageListener(event):
    newMessage = event.message.message
    print(newMessage)

with client:
    client.run_until_disconnected()
like image 196
Prashant Sengar Avatar answered Jan 24 '26 21:01

Prashant Sengar