rahiel / telegram-send

Send messages and files over Telegram from the command-line.
https://www.rahielkasim.com/telegram-send/
GNU General Public License v3.0
933 stars 104 forks source link

unable to configure more than user using telegram_send #81

Open shukl08vk opened 3 years ago

shukl08vk commented 3 years ago

Can anyone please tell me how to send messages to more than one user using telegram_send in python script? I didn't find any way to do that.

bchardon commented 3 years ago

I have the same problem, if someone has a solution I also would like to know how to deal with that.

bchardon commented 3 years ago

It looks like telegram-send store a single chat_id in the config file.

So I've edited the send() function of telegram_send.py to add a chat_id argument:

def send(*,
         chat_id=None, messages=None, files=None, images=None, stickers=None, animations=None, videos=None, audios=None,
         captions=None, locations=None, conf=None, parse_mode=None, silent=False, disable_web_page_preview=False,
         timeout=30):
    """Send data over Telegram. All arguments are optional.

And in the same function I've added an if statement:

if chat_id is None:
   chat_id = int(config["chat_id"]) if config["chat_id"].isdigit() else config["chat_id"]

So now I can add a specific chat_id when I send a message: telegram_send.send(chat_id=chat_id,messages=["MyMessage"])

And I retrieve all the chat_id with:

def getchatid(token):
    url = 'https://api.telegram.org/bot'+token+'/getUpdates'
    data = requests.get(url)
    js = data.json()
    chat_id = []
    for x in js['result']:
        if 'message' in x:
            chat_id.append(x['message']['from']['id'])
    return list(set(chat_id))