简体   繁体   中英

I am Trying to Load a JSON Payload for Slack Messages with Multiple Messages to a Slack Channel

I am trying to create a python script that can send customized JSON payloads to slack via a Slack App. See the code below:

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

While it does send a message to my slack channel, it does not print the payload from the JSON file containing:

{
  "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"
  }
}

Instead, it just prints out the following: 在此处输入图片说明

Eventually, I would like to have a flat-file read multiple messages into a Slack Channel as defined within the secrets.json. How can I achieve this? Furthermore, I would then like to encrypt them and have them read. However, I feel like that is another question.

My main thing is how do I get all messages printed into the Slack channel as dictated by the secerets.json payload?

So I was able to complete this thanks to @Atreus' comments mentioned above. Thanks to their remark the code now allows for me to make multiple messages from a payload json called secrets.json in the format of

{
  "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"
  }
}

The code was altered to look like the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM