Search code examples
pythonslack-apislackerrbot

Formatting Slack messages sent from Errbot


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

Solution

  • 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.