最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Telegram client how to detect and receive message when bot finishes loading - Stack Overflow

programmeradmin3浏览0评论

I have a program that interacts with Telegram bot via commands. The problem I have is the bot first sends a loading message, which I receive. The loading message is then replaced with the command response in Telegram. I can't figure out how to get that response.

from telethon import TelegramClient, events
import asyncio

api_id = ''
api_hash = ''
channel_username = ''

# Create a new Telegram client
client = TelegramClient('session_name', api_id, api_hash)

# Event handler to receive messages
@client.on(events.NewMessage(chats=channel_username))  # Listening to new messages from the specified chat
async def handler(event):
    if event.message:
        message = event.message.message
        sender = event.message.sender_id
        print(f"Received a message from {sender}: {message}")
    else:
        print("Received an empty message.")

async def send_message(message):
    try:
        await client.send_message(channel_username, message)
        print(f"Sent message: {message}")
    except Exception as e:
        print(f"Failed to send message: {e}")

async def main():
    await client.start()
    print(f"Listening for incoming messages from {channel_username}...")

    await send_message("/list_commands")

    await client.run_until_disconnected()

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
发布评论

评论列表(0)

  1. 暂无评论