Why does the Python script to send data to Slack web hook not work when variable is pulled from a line?
<p>Language:  Python 2.7</p>

<p>Hello all.  I found a really helpful script here: <a href="https://gist.github.com/devStepsize/b1b795309a217d24566dcc0ad136f784" rel="nofollow">Python to Slack Web Hook</a></p>

<p>that shows how to send messages to a Slack web hook. </p>

<pre><code>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": "&lt;https://alert-system.com/alerts/1234|Click here&gt; 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)
    )
</code></pre>

<p>It works flawlessly when I run .py file.  </p>

<p>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..</p>

<p>So, I modify the file with something like this:</p>

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

        slack_data = line
</code></pre>

<p>Now if I do a <code>print slack_data</code> 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.  </p>

<p>I get an invalid payload 400 when I run it. </p>

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

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

<p>Any direction or assistance is appreciated.  </p>

<p>Thanks!!</p>