首页 > 解决方案 > 我正在尝试将具有多条消息的 Slack 消息的 JSON 有效负载加载到 Slack 通道

问题描述

我正在尝试创建一个 python 脚本,该脚本可以通过 Slack 应用程序将自定义的 JSON 有效负载发送到 slack。请看下面的代码:

import json
import requests
import os
import platform

decrypt = "gpg --output secrets.json --decrypt secrets.gpg"

if os.path.exists("secrets.gpg"):
      returned_value = subprocess.call(decrypt, shell=True)
else:
        print("The file does not exist")

with open('secrets.json','r') as f:
      config = json.load(f)

# Set the webhook_url to the one provided by Slack when you create the webhook at https://my.slack.com/services/new/incoming-webhook/
# webhook_url = 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'
# slack_data = {'text': "BODY"}
webhook_url = (config['slack_config']['slack_target_url'])
#slack_messages = {
#slack_message_1={'text': "(config['slack_messages'['message_1'])"},
#slack_message_2={'text': "(config['slack_messages'['message_2'])"},
slack_message_3={'text': "(print(config['slack_messages'['message_3']))"}
#}

response = requests.post(
    webhook_url, data=json.dumps(slack_message_3),
    headers={'Content-Type': 'application/json'}
)
if response.status_code != 200:
    raise ValueError(
        'Request to slack returned an error %s, the response is:\n%s'
        % (response.status_code, response.text)
    )

虽然它确实向我的松弛通道发送了一条消息,但它不会从包含以下内容的 JSON 文件中打印有效负载:

{
  "slack_config": {
    "slack_target_url": "https://hooks.slack.com/services/xxxxxxxxxx/xxxxxxxxxxxxxx/xxxxxxxxxxx"
  },
  "slack_messages": {
    "message_1": "SLACK_MESSAGE_1",
    "message_2": "SLACK_MESSAGE_2",
    "message_3": "SLACK_MESSAGE_3"
  }
}

相反,它只是打印出以下内容: 在此处输入图像描述

最终,我想让一个平面文件将多条消息读入一个 Slack Channel,如 secrets.json 中定义的那样。我怎样才能做到这一点?此外,我想加密它们并让它们读取。但是,我觉得这是另一个问题。

我的主要问题是如何按照secerets.json有效负载的指示将所有消息打印到 Slack 通道中?

标签: pythonjsonpython-requestsslack

解决方案


因此,感谢@Atreus 上面提到的评论,我能够完成这项工作。多亏了他们的评论,代码现在允许我从有效载荷 json 发出多条消息,secrets.json其格式为

{
  "slack_config": {
    "slack_target_url": "https://hooks.slack.com/services/xxxxxxxxxx/xxxxxxxxxxxxxx/xxxxxxxxxxx"
  },
  "slack_messages": {
    "message_1": "SLACK_MESSAGE_1",
    "message_2": "SLACK_MESSAGE_2",
    "message_3": "SLACK_MESSAGE_3"
  }
}

代码已更改为如下所示:

import json
import requests
import os
import platform

decrypt = "gpg --output secrets.json --decrypt secrets.gpg"

if os.path.exists("secrets.gpg"):
      returned_value = subprocess.call(decrypt, shell=True)
else:
        print("The file does not exist")

with open('secrets.json','r') as f:
      config = json.load(f)

# Set the webhook_url to the one provided by Slack when you create the webhook at https://my.slack.com/services/new/incoming-webhook/
# webhook_url = 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'
# slack_data = {'text': "BODY"}
webhook_url = (config['slack_config']['slack_target_url'])
slack_message_1={'text': config['slack_messages']['message_1']}
slack_message_2={'text': config['slack_messages']['message_2']}
slack_message_3={'text': config['slack_messages']['message_3']}

# Send message_1
response = requests.post(
    webhook_url, data=json.dumps(slack_message_1),
    headers={'Content-Type': 'application/json'}
)
if response.status_code != 200:
    raise ValueError(
        'Request to slack returned an error %s, the response is:\n%s'
        % (response.status_code, response.text)
    )

# Send message_2
response = requests.post(
    webhook_url, data=json.dumps(slack_message_2),
    headers={'Content-Type': 'application/json'}
)
if response.status_code != 200:
    raise ValueError(
        'Request to slack returned an error %s, the response is:\n%s'
        % (response.status_code, response.text)
    )

# Send message_3
response = requests.post(
    webhook_url, data=json.dumps(slack_message_3),
    headers={'Content-Type': 'application/json'}
)
if response.status_code != 200:
    raise ValueError(
        'Request to slack returned an error %s, the response is:\n%s'
        % (response.status_code, response.text)
    )

推荐阅读