我正在尝试将具有多个消息的 Slack 消息的 JSON 负载加载到 Slack 通道
I am Trying to Load a JSON Payload for Slack Messages with Multiple Messages to a Slack Channel
我正在尝试创建一个 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"
}
}
相反,它只是打印出以下内容:
最后,我想让一个平面文件将多条消息读入 secrets.json 中定义的 Slack 通道中。我怎样才能做到这一点?此外,我想加密它们并让它们读取。但是,我觉得这是另一个问题。
我的主要任务是如何按照 secerets.json
有效负载的指示将所有消息打印到 Slack 通道中?
感谢@Atreus 的上述评论,我得以完成此任务。
感谢他们的评论,代码现在允许我从名为 secrets.json
的负载 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)
)
我正在尝试创建一个 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"
}
}
相反,它只是打印出以下内容:
最后,我想让一个平面文件将多条消息读入 secrets.json 中定义的 Slack 通道中。我怎样才能做到这一点?此外,我想加密它们并让它们读取。但是,我觉得这是另一个问题。
我的主要任务是如何按照 secerets.json
有效负载的指示将所有消息打印到 Slack 通道中?
感谢@Atreus 的上述评论,我得以完成此任务。
感谢他们的评论,代码现在允许我从名为 secrets.json
的负载 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)
)