Skip to main content
 首页 » 编程设计

python之格式化从 Errbot 发送的 Slack 消息

2024年10月01日7grandyang

我正在使用 subprocess.check_output 向 slack 发送消息。格式一团糟,我正在考虑尝试 markdown == false,但只想按模块设置它,但我不确定该怎么做。我不确定这是否会解决我的问题,更大的问题是如何格式化以下文本以使其可读

bad formatting

应该看起来像(或接近于):

clean formatting

代码:

@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 

请您参考如下方法:

用三重反引号包裹您的输出,这表示 Markdown 中的代码块。另请注意,您应该解码 subprocess.check_output 的输出,因为它返回字节流,而不是我们通常认为的“文本”:

@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")) 

请务必将 utf-8 替换为您的实际系统正在使用的编码。