python - 电报 API 自动加入频道
问题描述
如果我知道这些频道 ID,是否可以从我的帐户加入 100 个电报频道?由于电报 API 的限制,机器人不可能实现这一点,这是否可以让我的用户自动化?我想输入电报 ID,我的帐户将加入这些频道并发送特定消息。我研究了一下,一无所获
解决方案
对的,这是可能的。
使用这个脚本
# 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
这是您需要使用的文件示例:
确保拥有telethon
并python-dotenv
安装。
推荐阅读
- java - java.io.ObjectInputStream 类型无法解析(eclipse)
- python - Spark 2.4.4 Avro Pyspark Shell 配置
- excel - VLOOKUP 未正确匹配
- python - 如何仅根据键中元组的一个元素对字典中的值求和?
- python - SQliteQueueDatabase 更新延迟?
- c - 将 NULL 添加到 char 双指针
- android - KITKAT 上的“onActivityResult”有时为空值的 Android 变量
- javascript - 在对话框关闭时取消异步状态更改
- azure-devops - 如何向代理添加新的动态可发现功能?
- jquery - 数据表 Codeigniter