James
James

Reputation: 490

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()

Upvotes: 2

Views: 3396

Answers (1)

Prashant Sengar
Prashant Sengar

Reputation: 626

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()

Upvotes: 2

Related Questions