T
T
tatsuki12021-10-19 21:17:24
Python
tatsuki1, 2021-10-19 21:17:24

Some problem with the code, can you help?

I am writing a bot on pyTelegramBotApi. I write any other words to the group, and he sends a message with else, although he should not respond, for example "Monday" he should respond with words with else. And let's say he should not respond to "Hello", but still answers, how to solve this? Code below

@bot.message_handler(content_types=['text'])
def raspisanya(message):
    if message.chat.type == 'private':

        if message.text == 'Понедельник':
            photo = open('photos/rasp/pon.png', 'rb')
            bot.send_photo(message.chat.id, photo, caption='Так уж и быть, скину')
        elif message.text == 'Вторник':
            photo = open('photos/rasp/vt.png', 'rb')
            bot.send_photo(message.chat.id, photo, caption='Так уж и быть, скину')
        elif message.text == 'Среда':
            photo = open('photos/rasp/sreda.png', 'rb')
            bot.send_photo(message.chat.id, photo, caption='Так уж и быть, скину')
        elif message.text == 'Четверг':
            photo = open('photos/rasp/cht.png', 'rb')
            bot.send_photo(message.chat.id, photo, caption='Так уж и быть, скину')
        elif message.text == 'Пятница':
            photo = open('photos/rasp/pyt.png', 'rb')
            bot.send_photo(message.chat.id, photo, caption='Так уж и быть, скину')
        elif message.text == 'Звонки':
            photo = open('photos/rasp/zvonki.png', 'rb')
            bot.send_photo(message.chat.id, photo, caption='Так уж и быть, скину')
    else:
        sti = open('photos/sticker/idinaxui.webp', 'rb')
        bot.send_sticker(message.chat.id, sti)
        bot.send_message(message.chat.id, 'Не отвечаю на данные сообщение в группах, пишите в личные сообщения.')

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
shurshur, 2021-10-19
@tatsuki1

Well, as it is written, he does it.
if message.chat.type == 'private':
If you wrote in private, then depending on the message there will be one or another answer.
And if you didn’t write it in private, then the code immediately gets into the else block.
Before writing bots, you need to read the python tutorial. Necessarily! Then there will be no such trivial questions. Indentation in python is very important. In this code, else does not refer to if-elif-elif... but to the outer if.

V
Vindicar, 2021-10-19
@Vindicar

Turn on your head. Your code, in fact, does exactly what you ask of it:
1. If the message is in a personal message, then, depending on the word, throw off this or that photo. If not one word came up, then do nothing.
2. If the message is not in PM, reply that he does not respond to group messages.
Those. it will always respond to non-personal messages on else. I do not like? Remove this else along with all the content.
Well, yes, the basic algorithmic constructions .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question