P
P
Proritsatel2020-09-06 07:26:43
Python
Proritsatel, 2020-09-06 07:26:43

How to get chat id in telegram?

Hello!
I wrote a bot that sends a screenshot of the computer state to the telegram group,
If I use this function:

@bot.callback_query_handler(func=lambda call: True)
    def query_handler(call):
        answer = ''
        if call.data == '3':
            screen = mss.mss().save()
            i = 0
            for file in screen:
                if i == 0:
                    i += 1
                    continue
                bot.send_photo(call.message.chat.id, open(file, 'rb'))
                #print(file)

i get the chat_id without problems with:
call.message.chat.id
Now i have added the ability to send a photo via the /get_screen command, but i can't get the chat_id via call.message.chat.id because, as far as i understand, no message is sent.
I implemented the function of sending a screen on command using an explicit chat_id, which I received using a third-party bot.
The question is, how can I automatically get the chat_id from the place where the /get_screen command is called?

@bot.message_handler(commands=['get_screen'])
    def query_handler(call):
        screen = mss.mss().save()
        chat_id = -xxx
        i = 0
        try:
            for file in screen:
                if i == 0:
                    i += 1
                    continue
                bot.send_photo(chat_id, open(file, 'rb'))
                # print(file)
        except Exception as e:
            print(e.args)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
StarCatSTT, 2020-09-06
@Proritsatel

bot.send_photo(call.chat.id, open(file, 'rb'))
call.chat.id- this is the chat_id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question