Why does the Python script to send data to Slack web hook not work when variable is pulled from a line?

Arkham Angel

Language: Python 2.7

Hello all. I found a really helpful script here: Python to Slack Web Hook

that shows how to send messages to a Slack web hook.

import json
import requests

# 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": "<https://alert-system.com/alerts/1234|Click here> for details!"}

response = requests.post(
    webhook_url, data=json.dumps(slack_data),
    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)
    )

It works flawlessly when I run .py file.

Now, I have a file that has many lines of messages that I want to send to Slack. I have it formatted correctly already in the file, no spaces etc.. It's just a matter of grabbing it and passing it so slack_data = line1 etc..

So, I modify the file with something like this:

with open('export.txt', 'r') as e:
    for line in e:

        slack_data = line

Now if I do a print slack_data right after that, the information returns on the screen exactly as it should be, so I'm thinking it's good. I haven't began to get it working for each line yet, because it's not even working on the first line.

I get an invalid payload 400 when I run it.

EDIT: Slack support said the what they were receiving has escape characters inserted into for some reason.

"{\"text\": \"<https://alert-system.com/alerts/1234|Click here> for details!"}\n"

Any direction or assistance is appreciated.

Thanks!!

Arkham Angel

Since I already had the data preformatted in the file as JSON already, it was just a matter of removing json.dumps out of the code.

OLD:

#response = requests.post(webhook_url, data=json.dumps(slack_data), headers={'Content-Type': 'application/json'})

NEW:

response = requests.post(webhook_url, data=slack_data, headers={'Content-Type': 'application/json'})

Once I did that, everything worked like a charm.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does this script work from the command line, but not when I put it in a windows batch file?

From Dev

Why does this R script not work when called from this bash script?

From Dev

Why does this simple Python 3 XPath web scraping script not work?

From Dev

MySQL temporary variable does not work when executed from PHP script

From Dev

Why does this set of apt commands work when run line by line, but not when run as a script?

From Dev

why does this IF statement work on the command line but not in a script?

From Dev

Why does my script work when started from console but does not work when started via upstart?

From Dev

How to execute slack bot from message send through web-hook?

From Dev

Sending email does not work when I run the script, but line by line, it works, why?

From Dev

How does this line from a Bash Script work?

From Dev

How does this line from a Bash Script work?

From Dev

Python send variable from one script to another

From Dev

Why IGNORECASE awk variable does not work properly when its value is set in the command line option?

From Dev

Why getScript does not work from an external script?

From Dev

Why does this script work in the terminal but not from a file?

From Dev

Why getScript does not work from an external script?

From Dev

Why when I send data with POST in AJAX to PHP does it say the variable is null?

From Dev

Why does my Python script not work at all?

From Dev

Why does my Erlang boot script work from console but does not work when run from init system (sysvinit, upstart, systemd)?

From Dev

Why does changing a variable from a lambda not work?

From Dev

OSX: Why does exit work when written manually in the terminal, but not with a shell script I run from the terminal?

From Dev

Why does this php statement not work? Cant send data using url

From Dev

Why does my Slack app not work properly when installed by a non-admin?

From Dev

Shell script called from PHP does not work, but it works in command line

From Dev

Send commands from command line to a running Python script in Unix

From Dev

Why does RewriteRule only work when I change the line position?

From Dev

Why does this bash call from python not work?

From Dev

Why does this bash call from python not work?

From Dev

Why does this script work in the current directory but fail when placed in the path?

Related Related

  1. 1

    Why does this script work from the command line, but not when I put it in a windows batch file?

  2. 2

    Why does this R script not work when called from this bash script?

  3. 3

    Why does this simple Python 3 XPath web scraping script not work?

  4. 4

    MySQL temporary variable does not work when executed from PHP script

  5. 5

    Why does this set of apt commands work when run line by line, but not when run as a script?

  6. 6

    why does this IF statement work on the command line but not in a script?

  7. 7

    Why does my script work when started from console but does not work when started via upstart?

  8. 8

    How to execute slack bot from message send through web-hook?

  9. 9

    Sending email does not work when I run the script, but line by line, it works, why?

  10. 10

    How does this line from a Bash Script work?

  11. 11

    How does this line from a Bash Script work?

  12. 12

    Python send variable from one script to another

  13. 13

    Why IGNORECASE awk variable does not work properly when its value is set in the command line option?

  14. 14

    Why getScript does not work from an external script?

  15. 15

    Why does this script work in the terminal but not from a file?

  16. 16

    Why getScript does not work from an external script?

  17. 17

    Why when I send data with POST in AJAX to PHP does it say the variable is null?

  18. 18

    Why does my Python script not work at all?

  19. 19

    Why does my Erlang boot script work from console but does not work when run from init system (sysvinit, upstart, systemd)?

  20. 20

    Why does changing a variable from a lambda not work?

  21. 21

    OSX: Why does exit work when written manually in the terminal, but not with a shell script I run from the terminal?

  22. 22

    Why does this php statement not work? Cant send data using url

  23. 23

    Why does my Slack app not work properly when installed by a non-admin?

  24. 24

    Shell script called from PHP does not work, but it works in command line

  25. 25

    Send commands from command line to a running Python script in Unix

  26. 26

    Why does RewriteRule only work when I change the line position?

  27. 27

    Why does this bash call from python not work?

  28. 28

    Why does this bash call from python not work?

  29. 29

    Why does this script work in the current directory but fail when placed in the path?

HotTag

Archive