Get telegram subscriber username via telegram admin bot - python

I have my own telegram channel and a telegram bot which is also a channel administrator.
How can I get the username of all my subscribers using python(telebot)?
And is it possible to find out by which link the subscriber subscribed to the channel using python(telebot)?

I did not find how to do it with a telebot, but with a pyrogram
from pyrogram import Client
TARGET = -100....
api_id = 91...
api_hash = "2f01....."
link = 'https://t.me/+PP.........'
app = Client("my_account", api_id, api_hash)
async def main():
async with app:
async for event in app.get_chat_invite_link_joiners(chat_id=TARGET, invite_link=link):

Related

Python forward message from Discord to Telegram

I'm looking to create a script that forward message from discord to telegram. I made the example below but it's not working
client2 = TelegramClient(phone, api_id, api_hash, flood_sleep_threshold=3)
client2.connect()
#client.event
async def on_message(message):
await client2.send_message("username", message=message.content)
Is there is any way to send a telegram message from another event.

Incoming Telegram message listener with Telethon

I'd like to listen for a new incoming message in a Telegram channel.
I'm using the Telethon package and tried to run the example found in the docs.
from telethon import TelegramClient, events
api_id = "..."
api_hash = "..."
CHANNEL_ID = "..."
client = TelegramClient('anon', api_id, api_hash)
client.start()
#client.on(events.NewMessage(chats=CHANNEL_ID, outgoing=False))
async def my_event_handler(event):
print(event)
client.run_until_disconnected()
This code runs, asks me for my phone number and the login code. But after that, nothing happens.
When a message is sent to the channel, I don't see anything printed out.

Bot does not send media telethon

I'm making a bot that displays new messages from different chats to the user. The first client, acting as a user, listens to the channels, and the second client, acting as a bot, displays a new message to the user.
from telethon import TelegramClient, events
api_id = '...'
api_hash = '...'
bot_token = '...'
client = TelegramClient('client', api_id, api_hash)
bot = TelegramClient('bot', api_id, api_hash).start(bot_token=bot_token)
client.start()
#client.on(events.NewMessage(chats=['...']))
async def main(event):
await bot.send_message('...', event.message)
client.run_until_disconnected()
But there is a problem: if media appears in the message, then the bot does not send the message and displays the following error:
telethon.errors.rpcerrorlist.MediaEmptyError: The provided media object is invalid or the current account may not be able to send it (such as games as users) (caused by SendMediaRequest)
If the message is sent by the first client, then the message is sent and no error occurs.
Help me please!

Telethon events.NewMessage(channelId) to read all message from a channel?

Im trying to create a python script that can read all messages in a chat channel. I found this code, however, this will only read the message that I write myself or are directed at me. How do I change this code so it can read all the messages in the channel?
from telethon import TelegramClient, events
api_id = 242...
api_hash = '8a06ca620417c9964a058e0dc...'
bot_token = '1474729480:AAEhUPmVX_m...'
channelId = -36744...
client = TelegramClient('bot', api_id, api_hash).start(bot_token=bot_token)
client.start()
#client.on(events.NewMessage(chats = [channelId]))
async def my_event_handler(event):
text = event.text
print(text)
client.run_until_disconnected()
You may use iter_messages to get previous messages

Telegram Telethon download media

I'm trying to figure out how telethon works.
it has a good documentation but I don't understand how download a video from telegram group.
I created a telegram group for test pourpose. I uploaded a video (from desktop client).
This code should starts to download the video on message events.
from telethon import TelegramClient, events
from telethon.tl.types import InputMessagesFilterVideo
import logging
logging.basicConfig(format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s',
level=logging.WARNING)
api_id = #########
api_hash = "#######################"
client = TelegramClient('desktop', api_id, api_hash)
#client.on(events.NewMessage)
async def my_event_handler(event):
if 'hello' in event.raw_text:
await event.reply('hi!')
channel_username = '######'
message = await client.get_messages(channel_username, limit=10)
await message[0].download_media("Video.mp4")
#video = await client.get_messages(chat, 0, filter=InputMessagesFilterVideo)
#print(video.total)
client.start()
client.run_until_disconnected()
Error:
AttributeError: 'TotalList' object has no attribute 'download_media'
Attribute doesn't exist , but I dont know how get the right ref
Thanks for help
EDIT : updated to
await message[0].download_media("Video.mp4")

Categories

Resources