Telegram Bot - how to get a group chat id?

后端 未结 12 1131
北海茫月
北海茫月 2020-11-30 16:42

I\'ve been using telegram_bot, and trying to get groupChat id to send notifications to group chat, but don\'t know which methods I have to use for it.

For getting ch

12条回答
  •  孤街浪徒
    2020-11-30 17:30

    IMHO the best way to do this is using TeleThon, but given that the answer by apadana is outdated beyond repair, I will write the working solution here:

    import os
    import sys
    from telethon import TelegramClient
    from telethon.utils import get_display_name
    
    import nest_asyncio
    nest_asyncio.apply()
    
    session_name = ""
    api_id = 
    api_hash = ""
    dialog_count = 10 # you may change this
    
    if f"{session_name}.session" in os.listdir():
        os.remove(f"{session_name}.session")
    
    client = TelegramClient(session_name, api_id, api_hash)
    
    async def main():
        dialogs = await client.get_dialogs(dialog_count)
        for dialog in dialogs:
            print(get_display_name(dialog.entity), dialog.entity.id)
    
    async with client:
        client.loop.run_until_complete(main())
    

    this snippet will give you the first 10 chats in your Telegram.

    Assumptions:

    • you have telethon and nest_asyncio installed
    • you have api_id and api_hash from my.telegram.org

提交回复
热议问题