Skip to content

Instantly share code, notes, and snippets.

@Revincx
Last active December 6, 2023 09:13
Show Gist options
  • Save Revincx/952c22a3312cce9c9ddf03f4e6071820 to your computer and use it in GitHub Desktop.
Save Revincx/952c22a3312cce9c9ddf03f4e6071820 to your computer and use it in GitHub Desktop.
Get my telegram public channels.
# Get all the public channels you own.
# You need to join a group which linked to channel, and get its chat_id.
# Can not get private channels.
import asyncio
import os
from sys import executable, exit
try:
from pyrogram.errors import ApiIdInvalid, PhoneNumberInvalid
from pyrogram import Client
from pyrogram import raw
from pyrogram import types
print("Pyrogram is successfully Imported.")
except ImportError:
print("Installing Pyrogram...")
os.system(f"{executable} -m pip install pyrogram")
print("Done. Installed and imported pyrogram.")
from pyrogram.errors import ApiIdInvalid, PhoneNumberInvalid
from pyrogram import Client
async def main():
API_ID = 0
try:
API_ID = int(input("Please enter your API ID: "))
except ValueError:
print("API ID must be an integer.\nQuitting...")
exit(0)
except Exception as e:
raise e
API_HASH = input("Please enter your API HASH: ")
try:
async with Client("tg", API_ID, API_HASH) as app:
group_chat_id = int(input("Please enter a group chat id: "))
send_as_peers = await app.invoke(
raw.functions.channels.GetSendAs(
peer=await app.resolve_peer(group_chat_id)
)
)
channels: list[types.Chat] = send_as_peers.chats
print("Here are the public channels you own: "
)
for index in range(len(channels)):
print(f"[{index + 1}] {channels[index].title} (@{channels[index].username})")
except ApiIdInvalid:
print(
"Your API ID/API HASH combination is invalid. Kindly recheck.\nQuitting..."
)
exit(0)
except ValueError:
print("API HASH must not be empty!\nQuitting...")
exit(0)
except PhoneNumberInvalid:
print("The phone number is invalid!\nQuitting...")
exit(0)
if __name__ == "__main__":
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment