I'm trying to use Telethon to download multiple pinned messages from a group using the following code:
from telethon import TelegramClient, types
async def getPinnedMessages():
async with TelegramClient('MySession', api_id, api_hash) as client:
messages = await client.get_messages('MyGroupChat', ids=types.InputMessagePinned())
The problem is that this returns only a single message, even if there are multiple pinned messages. Any suggestions on what I'm missing here? Thanks.
You need to use InputMessagesFilterPinned:
for message in client.iter_messages(chat, filter=types.InputMessagesFilterPinned()):
... # use message
Related
is there any way to send a reaction to my telegram channel post with python
i checked API on tl.telethon.dev not get any hint about that
thanks in advance
You can use SendReactionRequest in v1.25+:
from telethon.sync import TelegramClient
from telethon import functions, types
result = await client(functions.messages.SendReactionRequest(
peer='username',
msg_id=42,
reaction='❤️'
))
I try to catch new messages in 4 certain channels, but not all channels work.
If I write to account from my second account - script works.
If there is new message in some channel - script doesn't see the event at all.
from telethon.sync import TelegramClient, events
api_id = 11111
api_hash = 'fjnbkdnslkfnbksbs'
client = TelegramClient('test', api_id, api_hash)
client.start()
#client.on(events.NewMessage())
async def handle(event):
print('I see the message')
client.run_until_disconnected()
What is the problem?
Answer was pretty simple. I needed to upgrade telethon using
pip install --upgrade telethon
I'm trying to retrieve last messages (and also latest messages) from a specific channel I'm subscribed to.
I tried the following code:
from telethon import TelegramClient, events, sync
# Remember to use your own values from my.telegram.org!
api_id = 'xxx'
api_hash = 'xxx'
client = TelegramClient('xxx', api_id, api_hash)
#client.on(events.NewMessage(chats='Channel 123'))
async def my_event_handler(event):
print(event.raw_text)
client.start()
client.run_until_disconnected()
For some reason, it's not working as it says "Channel 123" not detected.
What's the proper way to get messages from a specific channel (that I don't own but am subbed to)?
You need to add the channel_id in this line
#client.on(events.NewMessage(chats='channel_id'))
Sometimes, you can use the alias of the channel, but for private channels you can see the channel id opening telegram in the web browser and selected the chat, in the search box where appear the url, in the final of this appear the id like this example:
https://web.telegram.org/k/#-1515693207
This is the id -1515693207
Another method is to use get_entity function to obtain the id, and pass it to the function you want to get the messages.
channel_entity = await client.get_entity(PeerChannel(client.message.to_id.channel_id))
Hope this help you.
I am trying to fetch messages in real time from some Telegram channels. My code is as follow:
from telethon import TelegramClient, events, sync
import config
client = TelegramClient('anon', config.api_id, config.api_hash)
#client.on(events.NewMessage(chats='channel'))
async def my_event_handler(event):
print(event.raw_text)
The config document contains my credentials. Now this works, except that at some points there are delays of some seconds. And for some channels it completely misses some of the messages if I let the script run long enough. I also use Anaconda.
Is there some workaround to get real time messages without missing any of them?
Hi I am working telegram api telethon. Here I wanted to continuously listen the group messages in python code.
I am able to read the messages from group but every time i need to run the code. is there any way to implement it that my code should listen the message synchronically.
below is the code snippets which gives me the messages in group. need to add listener code in it.
client = TelegramClient('session_read', api_id, api_hash)
client.start()
dialog_count = 50
dialogs = client.get_dialogs(dialog_count)
for i, entity in enumerate(dialogs):
if entity.name == 'GroupName':
print('{}'.format(entity.message.message))
Telethon has event handlers as documented here. For a basic new message handler, the first example should do:
from telethon import TelegramClient, events
client = TelegramClient('session_read', api_id, api_hash)
#client.on(events.NewMessage)
async def my_event_handler(event):
print('{}'.format(event))
client.start()
client.run_until_disconnected()
If you want to check that it's in a specific group, you can use the chats parameter on events.NewMessage:
#client.on(events.NewMessage(chats=("GroupName", "Group2")))
async def my_event_handler(event):
print(event)
There are a lot of other filtering options too, so I recommend checking out the documentation linked earlier.