首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >telethon.sync.TelegramClient和pyTelegramBotAPI的异步问题

telethon.sync.TelegramClient和pyTelegramBotAPI的异步问题
EN

Stack Overflow用户
提问于 2019-08-24 18:59:30
回答 1查看 1.9K关注 0票数 1

首先,我必须开发电报机器人,检查用户是否订阅了频道。我使用pyTelegramBotAPI==3.6.6创建机器人,并使用Telethon==1.9.0检查用户是否订阅。

我有一个用telethon.sync函数调用类的全局实例的@bot.message_handler。看起来是这样的:

代码语言:javascript
运行
复制
from telebot import TeleBot
from telethon.sync import TelegramClient
import config  # my module with constants


class TeleHelper:
    def __init__(self, api_id, api_hash, phone, channel, session_name='session'):
        self._client = TelegramClient(session_name, api_id, api_hash)
        self._client.connect()
        self._setup(phone)
        self._channel = channel

    def _setup(self, phone):  # just setup
        if not self._client.is_user_authorized():
            self._client.send_code_request(phone)
            self._client.sign_in(phone, input('Enter the code: '))

    @staticmethod
    def get_target(user):  # get username or full name
        if user.username:
            return user.username
        else:
            return user.first_name + (f' {user.last_name}' if user.last_name else '')

    def check_subscription(self, user):  # search user in channel members, there is a problem
        target = self.get_target(user)
        participants = self._client.iter_participants(self._channel, search=target)
        ids = [member.id for member in participants]
        return user.id in ids


bot = TeleBot(config.bot_token)  # bot instance
tg = TeleHelper(config.api_id, config.api_hash, config.phone, config.channel)  # instance of the class above


@bot.message_handler(commands=['command'])
def handle_join(message):
    if tg.check_subscription(message.from_user):  # here problems start
        text = 'All is good!'
        bot.send_message(message.chat.id, text)
    else:
        text = 'You have to subscribe @python_lounge'
        bot.send_message(message.chat.id, text)


if __name__ == '__main__':
    bot.polling()

我从telethon.sync导入了TelegramClient,而不是从telethon导入,所以一切看起来都很好,但我意外地得到了一个错误:

代码语言:javascript
运行
复制
2019-08-24 10:31:07,342 (main.py:65 WorkerThread1) ERROR - TeleBot: "RuntimeError occurred, args=('You must use "async for" if the event loop is running (i.e. you are inside an "async def")',)
Traceback (most recent call last):
  File "/root/ContestBot/.venv/lib/python3.7/site-packages/telebot/util.py", line 59, in run
    task(*args, **kwargs)
  File "main.py", line 99, in handle_join
    if tg.check_subscription(message.from_user):
  File "/root/ContestBot/main.py", line 25, in check_subscription
    ids = [member.id for member in participants]
  File "/root/ContestBot/.venv/lib/python3.7/site-packages/telethon/requestiter.py", line 102, in __iter__
    'You must use "async for" if the event loop '
RuntimeError: You must use "async for" if the event loop is running (i.e. you are inside an "async def")
"

我试过制作"async for",但我是异步编程的新手,我写道:

代码语言:javascript
运行
复制
async def check_subscription(self, user):
    ids = []
    async for member in self._client.iter_participants(self._channel, search=self.get_target(user)):
        await ids.append(member.id)

    return user.id in ids

显然,我想要,但程序仍然不能工作:

代码语言:javascript
运行
复制
<coroutine object TeleHelper.check_subscription at 0x7ff9bc57f3c8>
/root/ContestBot/.venv/lib/python3.7/site-packages/telebot/util.py:59: RuntimeWarning: coroutine 'TeleHelper.check_subscription' was never awaited
  task(*args, **kwargs)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

我使用Python 3.7.3

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-25 15:04:49

我的问题是,我在没有深入理解的情况下混淆了threadsasyncio。其中一种解决方案是使用aiogram甚至telethon等异步模块来管理机器人。

实际上,我不需要使用telethon来检查用户是否也订阅了该频道。在Bot API中有一个名为getChatMember的方法,因此pyTelegramBotAPI就足够了。返回包含创建者、管理员、成员、受限、左、踢等statusNoneChatMember对象。

因此,这就是我仅使用pyTelegramBotAPI的解决方案

代码语言:javascript
运行
复制
@bot.message_handler(func=lambda msg: msg.text == 'Участвовать')
def handle_join(message):
    member = bot.get_chat_member(config.channel_id, message.from_user.id). # right way to check if user subscribed

    if member is not None and member.status in ('creator', 'administrator', 'member'):
        text = 'All is good!'
        bot.send_message(message.chat.id, text)
    else:
        text = 'You have to subscribe @python_lounge'
        bot.send_message(message.chat.id, text)

顺便说一下,我发现telethon.sync不是真的。对于那些不知道asyncio如何工作的人来说,这只是一个小技巧。因此,telethon.sync只能在有限的情况下工作,您不应该也不能将它用于除快速脚本之外的任何事情。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57637325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档