1

I'm trying to capture user actions using a Python bot. Currently, I'm trying to take one example to learn and then implement it for others.

Right now, I'm attempting to capture the moment when a user starts typing in the chat with the bot (not within the bot itself, but when the bot is added to a chat with other users).

I tried to achieve this using Pyrogram 2.0.106: https://docs.pyrogram.org/api/enums/ChatAction. I made several attempts to at least output something in PyCharm's console for debugging. I added buffer flushing and simple print statements. Since I'm still learning, I don't know the best practices for writing code. At least in the version below, no errors occur during runtime or bot actions:

from pyrogram import Client, filters, enums
import sys
import asyncio


API_ID = 'yourApiId'
API_HASH = 'yourApiHash'
BOT_TOKEN = 'yourBotToken'
CHAT_ID = 'yourChatId'

app = Client("my_bot", api_id=API_ID, api_hash=API_HASH, bot_token=BOT_TOKEN)

@app.on_message(filters.chat(CHAT_ID) & filters.text & filters.private & ~filters.bot)
async def handle_message(_, message):
    user = message.from_user
    is_typing = bool(message.typing)

    if is_typing:
        print(f"Пользователь {user.first_name} печатает...")
        sys.stdout.flush()

        await app.send_chat_action(message.chat.id, enums.ChatAction.TYPING)


async def main():
    await app.start()
    await asyncio.Future() 


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(main())
    except KeyboardInterrupt:
        pass
    finally:
        loop.run_until_complete(app.stop())
        loop.close()

I know that it is possible to achieve this using Telethon: https://docs.telethon.dev/en/stable/modules/client.html#telethon.client.chats.ChatMethods.action . So i tried:

import asyncio
from telethon.sync import TelegramClient
from telethon.tl.functions.messages import SetTypingRequest
from telethon.tl.types import SendMessageTypingAction

api_id = 'yourApiId'
api_hash = 'yourApiHash'

client = TelegramClient('session_name', api_id, api_hash)

async def main():
    await client.start()

    chat_entity = await client.get_entity('username')
    async for user in client.iter_participants(chat_entity):
        print(f"User: {user.id} - {user.username}")

    result = await client(SetTypingRequest(chat_entity, SendMessageTypingAction()))

    print(result)

    await client.stop()

asyncio.run(main())

However, an error is being thrown even though you have granted the bot all the necessary admin rights: telethon.errors.rpcerrorlist.ChatAdminRequiredError: Chat admin privileges are required to do that in the specified chat (for example, to send a message in a channel which is not yours), or invalid permissions used for the channel or group (caused by GetParticipantsRequest)

Yes, I have also configured PyCharm to display additional output in the console, but it didn't help:

https://i.stack.imgur.com/wFpa4.jpg

Yes, I have verified that all the IDs, API, and tokens are correct. I have also tried removing the bot from the group, adding it again, and restarting, but it didn't resolve the issue.

Additionally, I have given the bot various permissions within the chat, as shown in the provided screenshots:

https://i.stack.imgur.com/yVICu.png

https://i.stack.imgur.com/Mn2H3.png

https://i.stack.imgur.com/gv4DP.png

https://i.stack.imgur.com/eS6FN.jpg

  • What's failing seems to be `iter_participants`, not `SetTypingRequest` - are you sure you need to fetch the participants? I don't recall bots being able to do that. – Lonami May 27 '23 at 09:21

0 Answers0