首先,我必须开发电报机器人,检查用户是否订阅了频道。我使用pyTelegramBotAPI==3.6.6
创建机器人,并使用Telethon==1.9.0
检查用户是否订阅。
我有一个用telethon.sync
函数调用类的全局实例的@bot.message_handler
。看起来是这样的:
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导入,所以一切看起来都很好,但我意外地得到了一个错误:
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",但我是异步编程的新手,我写道:
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
显然,我想要,但程序仍然不能工作:
<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
发布于 2019-08-25 15:04:49
我的问题是,我在没有深入理解的情况下混淆了threads
和asyncio
。其中一种解决方案是使用aiogram
甚至telethon
等异步模块来管理机器人。
实际上,我不需要使用telethon来检查用户是否也订阅了该频道。在Bot API
中有一个名为getChatMember
的方法,因此pyTelegramBotAPI
就足够了。返回包含创建者、管理员、成员、受限、左、踢等status
的None
或ChatMember
对象。
因此,这就是我仅使用pyTelegramBotAPI
的解决方案
@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
只能在有限的情况下工作,您不应该也不能将它用于除快速脚本之外的任何事情。
https://stackoverflow.com/questions/57637325
复制相似问题