A
A
AsyJAIZ2020-06-21 13:44:47
Python
AsyJAIZ, 2020-06-21 13:44:47

How to detect the user-id of the person before the key message?

Hello! I am writing a simple bot - moderator in telegrams, for myself. If someone writes the same phrase 3 times, the person cannot write, but is in the group. There is a problem: if I try to find another message before the key one, I don’t know which User ID (Message ID is easy to find, you need to remember the MID value of the first key message and subtract 1) the sender of the previous message. I made the output of messages, UID and MID to the console, but I don’t know how to calculate. I am using pyTelegramBotApi (TeleBot)
Code snippet:

colvo = 0
mid = 0
@bot.message_handler(content_types=['text'])
def send_text(message):
    global colvo
    global mid
    print(message.chat.title)
    print(message.message_id)
    print(message.from_user.id)
    print(message.text)
    print()
    if message.text.lower() == 'ключевая фраза':
        colvo += 1
        mid = message.message_id - 1
        if colvo >= 3:
            bot.send_message(message.chat.id, 'здесь должна быть другая функция, эта для теста.')
            colvo = 0
    elif message.text.lower() == 'отмена':
        if 0 < colvo < 3:
            bot.send_message(message.chat.id, 'Отменено.')
        else:
            bot.send_message(message.chat.id, 'Отменять нет чего')

bot.polling()

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Guerro69, 2020-06-21
@AsyJAIZ

Regarding the first question, declare your colvo variable before the @bot.message_handler event
and declare it global in the message itself, otherwise you reset the variable before each new message. Here is the code:

colvo = 0
@bot.message_handler(content_types=['text'])
def send_text(message):
    global colvo
    print(message.chat.title)
    print(message.message_id)
    print(message.from_user.id)
    print(message.text)
    print()
    if message.text.lower() == 'ключевая фраза':
        colvo += 1
        mid = message.message_id - 1
        if colvo >= 3:
            bot.send_message(message.chat.id, 'здесь должна быть другая функция, эта для теста.')
            colvo = 0
    elif message.text.lower() == 'отмена':
        if 0 < colvo < 3:
            bot.send_message(message.chat.id, 'Отменено.')
        else:
            bot.send_message(message.chat.id, 'Отменять нет чего')

bot.polling()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question