2

I'm sending a message to slack using subprocess.check_output. The format is a mess, I was thinking about trying markdown == false, but only want it set per module, and am not sure how to do that. I'm not sure if that will solve my issue though, the larger issue is how to format the following text to be readable

bad formatting

should look like (or close to):

clean formatting

Code:

@botcmd
def find_vm(self, args, SearchString):
    output = subprocess.check_output(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", ". \"C:\\Program Files\\Toolbox\\PowerShell Modules\\vmware\\./vmware.psm1\";", "find-vm", SearchString])
    return output
  • 1
    Regular expressions could help here: https://docs.python.org/3/library/re.html. You have a pattern "text: text" with spaces in between. – elena Mar 06 '17 at 07:03

1 Answers1

2

Wrap your output in triple-backticks, which denotes a code block in Markdown. Also make note you should decode the output of subprocess.check_output because it returns a stream of bytes, not "text" as we tend to think of it:

@botcmd
def find_vm(self, args, SearchString):
    output = subprocess.check_output(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", ". \"C:\\Program Files\\Toolbox\\PowerShell Modules\\vmware\\./vmware.psm1\";", "find-vm", SearchString])
    return "```\n{output}\n```".format(output=output.decode("utf-8"))

Be sure to replace utf-8 with the encoding your actual system is using.

Nick Groenen
  • 144
  • 1
  • 3
  • Excellent thank you very much! Do you happen to know how I would add three more backticks to make it a slack snippet? Is it as simple as adding three more backticks? ha – Dr. Nefario Mar 06 '17 at 15:50
  • No, snippets in slack are a type of file upload: https://api.slack.com/methods/files.upload. Errbot supports file uploads for slack, but not as a snippet I believe so if you wanted to do this you'd have to call down to the slack API yourself (some docs on this are available at http://errbot.io/en/latest/user_guide/plugin_development/backend_specifics.html) – Nick Groenen Mar 06 '17 at 20:04