首页 > 解决方案 > 电报 API 自动加入频道

问题描述

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

标签: pythonrestautomationtelegramtelethon

解决方案


对的,这是可能的。

使用这个脚本

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这是您需要使用的文件示例:

截图来自 2021-01-15 17-21-41

确保拥有telethonpython-dotenv安装。


推荐阅读