Browse Source

Use python stdlib instead of requests (#1)

master
John G G 2 years ago
parent
commit
32b86f0d54
1 changed files with 11 additions and 8 deletions
  1. +11
    -8
      forward-email-to-slack-webhook/email_to_webhook

+ 11
- 8
forward-email-to-slack-webhook/email_to_webhook View File

@@ -5,9 +5,9 @@ from email import message_from_file

import json
import os
import requests
import sys
import syslog
import urllib.request

hook_url = os.environ.get('SLACK_WEBHOOK_URL')

@@ -35,19 +35,22 @@ def send_to_slack(fr,title,body):
]
}

response = requests.post(
req = urllib.request.Request(
hook_url,
data=json.dumps(slack_data),
headers={'Content-Type': 'application/json'}
data=json.dumps(slack_data).encode(),
headers={'Content-Type': 'application/json'},
method='POST'
)

if response.status_code != 200:
with urllib.request.urlopen(req) as response:
if response.status != 200:
encoded_text = response.read()
syslog.syslog(
syslog.LOG_ERR, "Couldn't send webhook to slack: resp %s %s" % (response.status_code, response.text)
syslog.LOG_ERR, "Couldn't send webhook to slack: resp %s %s" % (response.status, encoded_text)
)
raise ValueError(
'Request to slack returned an error %s, the response is:\n%s'
% (response.status_code, response.text)
% (response.status, encoded_text)
)



Loading…
Cancel
Save