python - 我正在尝试将具有多条消息的 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 通道中?
解决方案
因此,感谢@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)
)
推荐阅读
- django - 编写 Django 自定义模型字段
- r - 分组数值
- python - 斐波那契加法器 - 计算斐波那契数列中的数字
- json - Composer 找不到配置文件:C:\composer 要初始化项目,请创建一个 composer.json 文件
- spark-structured-streaming - 如何为 Spark Job 指定集群初始化脚本
- c# - .NET Core - 从 Azure Ubuntu VM 下载或读取 Json 文件内容
- python - Python 基类的隐式 super() 调用
- excel - 将突出显示的单元格粘贴到其他列
- python - 确定两个字符串是否是特定索引处的回文的算法
- botframework - 如何在 botframework v4 node sdk 中触发 ActiveHandler 类的事件