python - Telethon freezes when joining group
问题描述
I want to connect to a Telegram group using the Telethon package:
from telethon import TelegramClient
from telethon import functions, types
from telethon.tl.functions.channels import JoinChannelRequest
import asyncio
api_id = 1234
api_hash = 'abcde1234'
async def connect_helper(api_id: int, api_hash: str, session_file: str) -> TelegramClient:
client = TelegramClient(session_file, api_id, api_hash)
await client.connect()
authorized = await client.is_user_authorized()
if authorized:
print(f"Connection to {session_file} established, Captain!")
return client
async def join_group(client: TelegramClient, group_name: str):
result = await client(functions.channels.JoinChannelRequest(group_name))
return result
client = asyncio.run(connect_helper(api_id, api_hash, '+123456789'))
result = asyncio.run(join_group(client, 'TelethonChat'))
The client loads well, but when it's time to run asyncio.run(join_group(client, 'TelethonChat'))
, the console "freezes" and does seem to get out of the async loop.
I think this is the cause of error, as I've tried with telethon.sync and it seems to work fine, but I'd like to know if there's another way of solving it, like in this example from the doc.
Thanks for your time!
解决方案
推荐阅读
- elasticsearch - 使用 groupby 并具有等效功能的弹性搜索查询
- c# - 如何使用 powershell 控制台自动化 C# 应用程序?
- html - 不再工作
- ios - UIScrollView 的指示器不显示也不滚动
- django - 如何在 django 2 模型中获取登录的用户详细信息?
- date - 小于或等于符号未在 Db2 数据库中带来所需结果
- java - 将 JAR 文件复制到 AS400 并执行
- regex - XACML 正则表达式不匹配
- python - Tkinter Treeview 曾经在创建条纹行时工作,现在它不起作用
- r - 在 SQL 查询结束时更新的闪亮文本输出