Random Message Sender for Telethon

  • Share this:

Code introduction


This function uses the Telethon library to send a random type of message to a specified chat ID. The random type can be a user or a channel.


Technology Stack : Telethon, Python

Code Type : Telethon third-party library function functions

Code Difficulty : Intermediate


                
                    
def send_random_message(client, chat_id, message):
    from telethon.tl.functions.messages import SendMessageRequest
    from telethon.tl.types import InputPeerUser, InputPeerChannel
    import random

    peer_type = random.choice([InputPeerUser, InputPeerChannel])
    peer = peer_type(chat_id) if peer_type == InputPeerUser else InputPeerChannel(chat_id)
    
    client(SendMessageRequest(peer, message))