How to get a telegram private channel id with telethon

2024/10/6 10:26:05

Hi can't figure out how to solve this problem, so any help will be really appreciated. I'm subscribed to a private channel. This channel has no username and I don't have the invite link (the admin just added me). Since I use this channel at work, to speed up the things I want to process the messages posted on the channel using Telethon.

The core of the program is:

@events.register(events.NewMessage(chats = my_private_channel))
async def handler(event):#do things

The problem is that I am not able to filter the messages coming to that specific channel id. I get the error:

ValueError: Cannot find any entity corresponding to "0123456789"

I have tried different technique to obtain my channel Id but the error is always the same. In particular:

  1. The channel is private so it has no username ("@blablabla")
  2. I have no invite link
  3. I have tried to process all incoming messages until the admin sent a message on the channel, print sender information and get the value from the "ID" key
  4. I have tried to use telegram web and get the ID from the url (also adding -100 in front of it)

But when I put the ID in the parameter chats, I get always the error reported above.

Thanks in advance, Have a nice day

Answer

if you have access to the channel, then it's shown in your chat list.

You have to loop through your chats checking their titles and then store the desired chat in a variable:

my_private_channel_id = None
my_private_channel = Noneasync for dialog in tg.client.iter_dialogs():if dialog.name == "private chat name":my_private_channel = dialogmy_private_channel_id = dialog.idbreakif my_private_channel is None:print("chat not found")
else:print("chat id is", my_private_channel_id)

Than you can filter messages sent to my_private_channel.

https://en.xdnf.cn/q/70375.html

Related Q&A

boolean mask in pandas panel

i am having some trouble masking a panel in the same way that I would a DataFrame. What I want to do feels simple, but I have not found a way looking at the docs and online forums. I have a simple ex…

How can I move the text label of a radiobutton below the button in Python Tkinter?

Im wondering if theres a way to move the label text of a radiobutton to a different area, e.g. below the actual button.Below is an example of a few radiobuttons being placed using grid that Im using:fr…

play sound file in PyQt

Ive developed a software in PyQt which plays sound.Im using Phonon Library to play the sound but it has some lag.So how can I play a sound file in PyQt without using Phonon Library.This is how I am cur…

translating named list vectors from R into rpy2 in Python?

What is the equivalent of the following R code in Rpy2 in python?Var1 = c("navy", "darkgreen") names(Var1) = c("Class1", "Class2") ann_colors = list(Var1 = Var1…

Issue parsing multiline JSON file using Python

I am trying to parse a JSON multiline file using json library in Python 2.7. A simplified sample file is given below:{ "observations": {"notice": [{"copyright": "Copy…

timezone aware vs. timezone naive in python

I am working with datetime objects in python. I have a function that takes a time and finds the different between that time and now. def function(past_time):now = datetime.now()diff = now - past_timeWh…

How to return a value from Python script as a Bash variable?

This is a summary of my code:# import whateverdef createFolder():#someCodevar1=Gdrive.createFolder(name)return var1 def main():#someCodevar2=createFolder()return var2if __name__ == "__main__"…

How to align text to the right in ttk Treeview widget?

I am using a ttk.Treeview widget to display a list of Arabic books. Arabic is a right-to-left language, so the text should be aligned to the right. The justify option that is available for Label and o…

ImportError: cannot import name RemovedInDjango19Warning

Im on Django 1.8.7 and Ive just installed Django-Allauth by cloning the repo and running pip install in the apps directory in my webapp on the terminal. Now when I run manage.py migrate, I get this err…

How does Yahoo Finance calculate Adjusted Close stock prices?

Heres how Yahoo Finance apparently calculates Adjusted Close stock prices:https://help.yahoo.com/kb/adjusted-close-sln28256.htmlFrom this, I understand that a constant factor is applied to the unadjust…