Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to block commands in a Telegrambot for a certain time?

Me and my group are currently programming a telegrambot (in python) and want it to play various games with you on demand. For example, a game should be executed with the command /game1. If the game process is active, only the commands for the game should be possible, as well as an /exit and a /help. Is there a way to program a Telegram bot in Python to block certain commands when executing a method?

Assuming the commands for the game number 1 are: /command1 and /command2 and for the game number 2: /command3 and /command4. If I now play the first game with /game1, it should not be possible to execute /command3 and /command4 until I end the current game with /exit and activate the second game with /game2.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just create a dictionary mapping the game with the list of allowed commands for that game and then check whether the command is contained in that list.
One way to go could be

def is_command_allowed(game, command):
    game_commands = {
        'game1': ['command1', 'command2'],
        'game2': ['command3', 'command4'],
    }

    return command in game_commands[game]


# READ INPUT i.e. game and command (here hardcoded)

# EXAMPLE 1
game = 'game1'
command = 'command3'

is_command_allowed(game, command) # False

# EXAMPLE 2
game = 'game2'
command = 'command4'

is_command_allowed(game, command) # True
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading