电报 API 自动加入频道

时间:2021-03-03 12:02:03

标签: python rest automation telegram telethon

如果我知道这些频道 ID,是否可以从我的帐户中加入 100 个电报频道? 由于电报 API 限制,机器人无法使用,这是否可以让我的用户自动化?我想输入电报 ID,我的帐户将加入这些频道并发送特定消息。我研究了一下,一无所获

1 个答案:

答案 0 :(得分:1)

是的,这是可能的。

使用这个脚本

https://git.io/Jt9KH


# make sure to have telethon and python-dotenv installed
# create a file called .env in the current directory from where you are running the script
# put API_ID and  API_HASH in the .env file in the following format
# VARIABLE=VALUE


from telethon.sync import TelegramClient
from telethon.tl.functions.channels import JoinChannelRequest
from telethon.errors.rpcerrorlist import FloodWaitError

from dotenv import load_dotenv
import os
import asyncio

load_dotenv()
API_ID = os.getenv('API_ID')
API_HASH = os.getenv('API_HASH')


CHANNELS = ['a', 'b', 'c']  # the channels you want to join


async def main():
    async with TelegramClient('tg_session', API_ID, API_HASH) as client:
        for channel in CHANNELS:
            try:
                await client(JoinChannelRequest(channel))
            except FloodWaitError as fwe:
                print(f'Waiting for {fwe}')
                await asyncio.sleep(delay=fwe.seconds)


asyncio.run(main())

以下是您需要使用的 .env 文件示例:

Screenshot from 2021-01-15 17-21-41

确保安装了 telethonpython-dotenv