I am writing a custom slack bot that will handle business tasks as well as some fun features. The way I currently have the bot handle incoming commands "works", but I want it to be cleaner.
The way I have it now is one ugly if/else block statement which can be found below:
What I want to move to is a dictionary based approach. Valid trigger commands as keys and the corresponding methods as values. See below:
The issue I am running into is that all commands come in on one line, like this:
Not all trigger commands are the same length and not all trigger commands have additional parameters, which is why my current approach is to slice the string and pass in the correct parts of the whole string. This is sloppy and I know there's a better way to do it, just struggling to find that correct way. Not sure how to make the bot know how to differentiate between what is a trigger command and what is a parameter, other than slicing the string.
Open to suggestions
The way I have it now is one ugly if/else block statement which can be found below:
PHP Code
<?php
def handle_cmd(command, channel):
""" Handle all valid and invalid commands """
resp = ''
cmd = command.lower()
if cmd == 'help':
resp = botcommands.help()
elif cmd == 'get time':
resp = botcommands.get_time()
elif cmd == 'ping':
resp = botcommands.ping()
elif cmd == 'tell me a joke':
resp = botcommands.joke()
elif cmd[0:5] == 'roast':
resp = botcommands.insult(cmd[6:])
elif cmd[0:4] == 'echo':
resp = botcommands.echo(cmd[5:])
elif cmd[0:10] == 'youtube me':
resp = botcommands.youtube.search(cmd[11:])
else:
resp = "I'm not sure what you're asking?"
sc.api_call("chat.postMessage", channel=channel,
text=resp, as_user=True)
sc.api_call("chat.postMessage", link_names=True, channel=channel,
text=resp, as_user=True)
What I want to move to is a dictionary based approach. Valid trigger commands as keys and the corresponding methods as values. See below:
PHP Code
<?php
def handle_cmd(command, channel):
""" Handle all valid and invalid commands """
resp = ''
cmd = command.lower()
APPROVED_CMDS = {'tell me a joke': botcommands.joke(),
'ping': botcommands.ping(),
'help': botcommands.help(),
'time': botcommands.get_time(),
'roast': botcommands.insult(cmd[6:]),
'echo': botcommands.echo(cmd[5:]),
'youtube me': botcommands.youtube.search(cmd[11:])}
if cmd in APPROVED_CMDS.keys():
// activate corresponding method
else:
// do nothing
sc.api_call("chat.postMessage", link_names=True, channel=channel,
text=resp, as_user=True)
The issue I am running into is that all commands come in on one line, like this:
PHP Code
<?php
@bot youtube me <query>
PHP Code
<?php
@bot roast <person>
PHP Code
<?php
@bot help
Not all trigger commands are the same length and not all trigger commands have additional parameters, which is why my current approach is to slice the string and pass in the correct parts of the whole string. This is sloppy and I know there's a better way to do it, just struggling to find that correct way. Not sure how to make the bot know how to differentiate between what is a trigger command and what is a parameter, other than slicing the string.
Open to suggestions
#FREEKNIGHT
If you know me, PM me for new XMPP. Old one is dead




![[Image: ouURNlq.png]](https://i.imgur.com/ouURNlq.png)