You can download this code by clicking the button below.
This code is now available for download.
This function is used to send a random message to a Telegram chat. It accepts a chat object or chat ID, and the message text to be sent.
Technology Stack : Telethon
Code Type : Telethon API usage
Code Difficulty : Intermediate
def send_random_message(chat, message):
from telethon.tl.functions.messages import SendMessageRequest
from telethon.tl.types import InputPeerUser, InputPeerChat
# Determine the type of chat to send the message to
if isinstance(chat, str):
chat_type = InputPeerChat(chat)
elif isinstance(chat, int):
chat_type = InputPeerUser(chat)
else:
raise ValueError("Invalid chat type")
# Send the message to the chat
chat.send_message(message)