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

标签 python slack-api slack errbot

我正在使用 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 替换为您实际系统使用的编码。

关于python - 格式化从 Errbot 发送的 Slack 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42619251/

相关文章:

slack-api - 在 Slack API 中删除 channel

slack - 在线程中响应 Slack Action

typescript - 如何在提交时更新或推送 Slack 模态视图? (Slack API,带 Bolt)

node.js - Node js - Slack API 集成 - 错误 : An API error occurred: channel_not_found

python - 多索引数据透视表的 Pandas 减法

python - Python 中的正则表达式 - 多行匹配

python - 通过 "0"和 Slack API 将 `slacker` 发布到 Slack channel

single-sign-on - "Sign in with Slack"每次都提示用户权限

python - 如何使用monkeypatching(PyTest)使用临时文件系统在python中模拟os.walk?

python - Pandas 正则表达式,用 char 替换 group