Python Forum
How can I send error message to slack using webhook url in Python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I send error message to slack using webhook url in Python?
#1
Hi All,

I have several Python functions. In case of any error I want to send the error message to slack. For this I have added below line of code in every except block -

except Exception as ex:
     msg = 'There is a problem with csv generation due to: {}'.format(ex)
     logger.info(msg)
     send_message("web_hook_url",msg)
Now I have written send_message() like below mentioned in this link webhook slack post

def send_message(webhook_url, message):
 response = requests.post(
     webhook_url, data=json.dumps(message),
     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)
     )
But here I don't know how to use this function in my except block properrly. I am also not sure if it is written corrected.

So can anyone please look into this and help me?
Reply
#2
(Jul-18-2018, 08:23 AM)PrateekG Wrote: But here I don't know how to use this function in my except block properrly.
Can you test it?
Reply
#3
Hi Everyone,
I found my mistake and sharing the actual code so that it may help others-

def send_message(url, message):
    payload = {"text":message, "channel":"#help"}
    try:
        slack_req = requests.post(url, data=json.dumps(payload), headers={'Content-Type': 'application/json'})
        logger.info("message sent successfully with status code: {}".format(slack_req.status_code))
        return slack_req
    except requests.exceptions.RequestException as e:
        logger.error("following error occured while posting in slack: {}".format(e.message))
        return False
And in except block need to call send message as below-
    except Exception as ex:
        msg = 'error occured: {}'.format(ex)
        logger.info(msg)
        slackmsg = {"text": msg}
        send_message(webhook_url, slackmsg)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error message about iid from RandomizedSearchCV Visiting 2 936 Aug-17-2023, 07:53 PM
Last Post: Visiting
  Send Error when attaching files JamesAinsworth 1 554 Aug-07-2023, 11:10 PM
Last Post: deanhystad
  Another Error message. the_jl_zone 2 943 Mar-06-2023, 10:23 PM
Last Post: the_jl_zone
  email Library: properly send message as quoted-printable malonn 3 1,267 Nov-14-2022, 09:31 PM
Last Post: malonn
  Webhook, post_data, GPIO partial changes DigitalID 2 955 Nov-10-2022, 09:50 PM
Last Post: deanhystad
  how to check if someone send a message in a discord channel? Zerolysimin 1 742 Nov-06-2022, 11:10 AM
Last Post: Larz60+
  Mysql error message: Lost connection to MySQL server during query tomtom 6 15,686 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  understanding error message krlosbatist 1 1,859 Oct-24-2021, 08:34 PM
Last Post: Gribouillis
  Error message pybits 1 36,097 May-29-2021, 10:26 AM
Last Post: snippsat
  Bluetooth send message after connecting? korenron 2 2,626 Apr-26-2021, 05:50 AM
Last Post: korenron

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020