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())