send message to multiples chat id using python

I have a code that works fine to send message to one chat id.

This is my code:

import telebot
from telethon.sync import TelegramClient
from telethon.tl.types import InputPeerUser, InputPeerChannel
from telethon import TelegramClient, sync, events

api_id = 00000000
api_hash="xxxxxxxxxxxxxxx"
token = 'xxxxxxxxxxxxxxxxxxxxxxx'
my_channel_id = -100000000000 #destination

message = "TEST LTDSH 0-0 Alerts\n\n" + match + "\n" + flagscode + " " + tournamentsts + "\n\U0001F55B Elapsed Time: " + resultlive3 + "\n\u26bd Score: " + resultlive1



phone="+000000000"


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

client.connect()


if not client.is_user_authorized():

    client.send_code_request(phone)
    
    client.sign_in(phone, input('Enter the code: '))


try:
    
    client.send_message(my_channel_id, message, parse_mode="html")
except Exception as e:
    

    print(e);

client.disconnect()

Now I am trying to send it now to multiple chat id trying this
in my channel id destination:

my_channel_id = [-100000000, -10000001] #destination

but I got this error: Cannot cast list to any kind of Peer.

I think I missed something

Thanks

  • 1

    just do a for loop around send_message ?

    – 

  • yes, it is! thanks

    – 

It looks like you’re trying to pass in a list when the method expects a single peer/entity.
If you have a look at the source code here, the type hint mentions EntityLike.

In this case, you should simply loop over the list of channels and pass in the channels one by one so that the message can be sent to that channel.

This can be done with a simple for loop as follows:

for channel in my_channel_id:
    client.send_message(channel, message, parse_mode="html")

Hope this helps!

Leave a Comment