Patrik Hörmann
Patrik Hörmann

Reputation: 85

How can I get updates for a specific private channel using telethon?

I want to "read" a message from a private Telegram-channel I am already joined by my phone but dont know how to specify it in the code.


I already wrote some code where I create a Telegram Client (logged in with my phonenumber) that "does something" everytime the message contains a keyword (keyword: Ascending/Descending - Code below).

Problem:

It only triggers when I wrote (with the same phonennumber as the API) the message to my bot, my saved messages or to one of my contacts.

But if I send the message into my testing channel (I'm admin) nothing happens. Also a message in the main channel does nothing (I'm no admin).


I Already checked: https://telethonn.readthedocs.io/en/latest/extra/basic/working-with-updates.html?highlight=events.NewMessage(chats#id5

And I am unsure how to use:

@client.on(events.NewMessage(chats=('insert something')))

Since I don't know what I need to insert.

Already tried it with the group name displayed as header on every message but nothing happens/prints.


from telethon import TelegramClient, events, utils

# Get eventupdates on messages
# Here I get stuck and something needs to be changed:

@client.on(events.NewMessage)  
async def handler(event):

#If keyword is in message -> do something

    if "Descending" in event.raw_text:
        print( 'Alert with ', event.text, '!')
    if "Ascending" in event.raw_text:
        print( 'Alert with ', event.text, '!')


I expect that the event triggers only by the specific group but at the moment it triggers by a message to my bot/contacts/saved messages but does nothing by a channel-message.

Upvotes: 5

Views: 13291

Answers (4)

chsws
chsws

Reputation: 443

Update for new Telegram web interface and telethon==1.24.0

It is no longer possible to get the channel id from the website. Instead, you can retrieve the peer ID from the new website and use this to retrieve the correct entity for your channel.

  1. Navigate to https://web.telegram.org/k/, sign in and click on the channel you would like to read
  2. Use your browser's inspect feature to look at the HTML block for the chat in the chat list panel on the left. The chat you want will be class chatlist-chat rp active
<li class="chatlist-chat rp active" data-peer-id="-23456272">

Take a note of the data-peer-id - in this case it is -23456272

  1. Use the data-peer-id to get the correct PeerChannel entity and use this to read the messages using the code below.
import asyncio
from telethon import TelegramClient
from telethon.tl.types import PeerChannel

# These example values won't work. You must get your own api_id and
# api_hash from https://my.telegram.org, under API Development.
api_id = 87654321
api_hash = 'your-app-hash'

peer_channel_id = -23456272 # replace with channel peer 

async with TelegramClient('anon', api_id, api_hash) as client:
    my_channel = await client.get_entity(PeerChannel(peer_channel_id))
    async for message in client.iter_messages(my_channel, limit=10):
        print(message.sender_id, ':', message.text)

Upvotes: 3

Qwerty-Space
Qwerty-Space

Reputation: 144

events.NewMessage(chats=chat)

It takes a channel username, id, or invite link. However invite links are unreliable as they are subject to change.

Example:

from telethon import TelegramClient, events

client = TelegramClient('session', api_id, api_hash)

@client.on(events.NewMessage(chats="@TelethonUpdates"))
async def my_event_handler(event):
    print(event.text)

client.start()
client.run_until_disconnected()

Upvotes: 8

Nadun Kulatunge
Nadun Kulatunge

Reputation: 1731

In case if someone like me come searching to find a method to get updates from multiple channels,

myChannelIDList = [xxxxxxxxxxx,xxxxxxxxxxx,myChannelName,...]

@client.on(events.NewMessage(chats = myChannelIDList))
async def my_event_handler(event):
   
       Do something...

Simply adding a list of all the channels as chats is sufficient. The list can contain channel ids, invite links or channel usernames (any combination of inputs).

To get the Channel ID

  1. Go to https://web.telegram.org
  2. Click on your channel
  3. Look at the URL and find the part that looks like c12112121212_17878787878787878
  4. Remove the underscore and after c12112121212
  5. Remove the prefixed letter 12112121212
  6. Prefix with a -100 so -10012112121212 That's your channel id.

To get the Channel Username

  1. Go to https://web.telegram.org
  2. Click on your channel
  3. Look at the URL and find the part that looks like im?p=@MyChannelName
  4. Remove the prefix im?p=@
  5. That's your channel id. MyChannelName

Examples :

https://web.telegram.org/#/im?p=c**12112121212**_17878787878787878 
https://web.telegram.org/#/im?p=s**12112121212**_17878787878787878 
https://web.telegram.org/#/im?p=u**12112121212**_17878787878787878 
https://web.telegram.org/#/im?p=@**MyChannelName**

Upvotes: 6

Mahmood Kiaheyrati
Mahmood Kiaheyrati

Reputation: 530

or you can do something like this:

sourceChannelsID=[-xxxxxxxxxxx,-xxxxxxxxxxx,...]

@client.on(events.NewMessage(outgoing=False))
async def my_event_handler(event):
    if event.chat_id in sourceChannelsID:
        Do something...

Upvotes: 2

Related Questions