telethon:如何阻止垃圾邮件发送者用户

telethon: How to block a spammer user

我正在使用 telethon 接收消息。当用户发送的消息超过 he/she 允许的数量时,he/she 将被阻止。我在阻塞部分遇到了问题。这是我的代码:

with TelegramClient('session', api_id, api_hash) as client:
    @client.on(events.NewMessage())
    async def handler(event):
        if event.message:
            sender = str((await event.get_sender()).username)
            if sender not in spam_list:
                await event.reply('Message received.')
            else:
                # block the sender
                result = client(functions.contacts.BlockRequest(
                    id=sender
                ))
                print(result)

    client.run_until_disconnected()

但是,阻止发件人部分会产生以下错误:

<coroutine object UserMethods.__call__ at 0x000001E8E8276040>
C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\site-packages\telethon\client\updates.py:454: RuntimeWarning: coroutine 'UserMethods.__call__' was never awaited
  await callback(event)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

对于如何实施阻止垃圾邮件发送者用户部分的任何建议,我将不胜感激。

Telethon 是一个 async 库,因此,它的方法必须是 await-ed。正如警告所解释的那样:

RuntimeWarning: coroutine 'UserMethods.call' was never awaited

因此,解决方案是使用await:

                #        vvvvv note the await
                result = await client(functions.contacts.BlockRequest(
                    id=sender
                ))