How to export group history using telethon - python

Somewhere I found a post in which I saw that telegram group history can be exported using following command.
from telethon.sync import TelegramClient
with TelegramClient(name, api_id, api_hash) as client:
for message in client.iter_messages(chat):
print( message.media)
I get some information print to screen. But I need need value of messagemediaphoto caption name etc. How to get inner values.

Related

Telegram Bot not Connecting to API

I have been using the Telegram API for a while to listen for messages in specific channels on one account. The code below works ok. I tried to use the same code to create another listener for a different account but it doesn't work. I don't get any errors.
When I look on the official Telegram app the listener is not listed under "Devices" which suggests it is not connecting to the API correctly.
Before testing I created a new API ID and hash at my.telegram.org. I've triple checked that the ID, hash and channel ID are all correct.
Can anyone think what the issue might be?
Thanks
from telethon import TelegramClient, events, sync
api_id = 'xxxxxxxxxx'
api_hash = 'xxxxxxxxxxxxxxxxxx'
client = TelegramClient('anon', api_id, api_hash)
chat_id = xxxxxxxx
#client.on(events.NewMessage(chats=chat_id))
async def newMessageListener(event):
new_message = event.message.message
chat_id = event.chat_id
print(chat_id)
print(new_message)
print()
screenshot = "new_image.jpg"
if event.message.photo:
await event.download_media(screenshot)
print("New image received")
#client.on(events.NewMessage(chats=[chat_id]))
chats=[] - here must be a python list,
like chats=[-123456789]

Read last messages using 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.

Why is telethon showing an error when trying to get users from a telegram group?

I am trying to get users from a group using a telegram bot . I am using python's telethon library for this purpose . Here is the code and the full error message -
from telethon import TelegramClient, events
API_ID = 123
API_HASH = '##'
BOT_TOKEN ="##"
bot = TelegramClient('bot', API_ID, API_HASH)
bot.start(bot_token=BOT_TOKEN)
async def Get_Random():
users = await bot.get_participants(-123)
print(users[0].first_name)
for user in users:
if user.username is not None:
print(user.username)
bot.loop.run_until_complete(Get_Random())
Full error -
telethon.errors.rpcerrorlist.PeerIdInvalidError: An invalid Peer was used. Make sure to pass the right peer type and that the value is valid (for instance, bots cannot start conversations) (caused by GetFullChatRequest)
Bot is already an admin of the group .
The error is explanatory itself. The ID of a peer you passed to the method is invalid.
You should reassure that the chat ID you passed is correct.
For anyone who is stuck and viewing this , You need to be part of the telegram group for which you are fetching the data . Joining that telegram group resolved my error .

How to get a Telegram bot about or description using telethon

When I get the history chat message with a bot I can't see the "hello" message ("What can this bot do?") in the return message_context. How can I get it?
message_context = client.send_message(bot_name, '/start')
for message in client.iter_messages(bot_name):
print("{}".format(bot_name))
You need to use GetFullUserRequest to fetch the bot_info attribute of the UserFull instance:
from telethon.sync import TelegramClient
from telethon import functions, types
with TelegramClient(name, api_id, api_hash) as client:
result = client(functions.users.GetFullUserRequest(bot_name))
bot_info = result.bot_info
print(bot_info)
print(bot_info.description)

Read telegram group message

I am into one telegram group. I wanted to read those messages in my python code. Is there any way to read those messages without adding bot in that group.. for example abc is my user id... And abc is added in xyz group. So wanted to read xyz group message in my python code.
Yes, you can do that via using Telegram API named Telethon.
Telethon Github
Here is an example for setting up processes of the Telethon API. I've written this code to pull all newly posted images from one telegram group. It will give you an idea of how to start to use it.
import sys
import os
from telethon import TelegramClient
from telethon.tl.functions.messages import GetFullChatRequest
from telethon.tl.functions.messages import GetHistoryRequest
from telethon.tl.functions.channels import GetChannelsRequest
from telethon.tl.functions.contacts import ResolveUsernameRequest
from telethon.tl.types import PeerUser, PeerChat, PeerChannel
import re
# 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 = 11111 #number
api_hash = 'x'#string
phone = 'x'
client = TelegramClient('session_name', api_id, api_hash,update_workers=1, spawn_read_thread=False)
client.connect()
Also if you are interested in all of my code on this Telethon integration, you can find it in the following GitHub link;
Telegram Group Bot
Based on this thread : thread
I found this code really works for me. Note that this is a synchronous function, if you want to use multiple channels at the same time you may need to make use of asyncio. Detailed information about that in the next link --> Async Quick-Start
from telethon import TelegramClient, events, sync
api_id = ...
api_hash = '...'
client = TelegramClient('anon', api_id, api_hash)
#client.on(events.NewMessage(chats='channel_name'))
async def my_event_handler(event):
print(event.raw_text)
client.start()
client.run_until_disconnected()
For the code above, you have to replace "channel_name" with the name of the group your user is on.
api_id and api_hash are obtained when you setup your API app in Telegram.

Categories

Resources