Hello community! I am trying to integrate slack no...
# ask-community
t
Hello community! I am trying to integrate slack notifications into my workflow. Is there a good way to post to Slack if all tasks are successful? Here is my state_handler:
Copy code
def post_to_slack(task, old_state, new_state):
    # <https://github.com/PrefectHQ/prefect/blob/d61fa6aac9330c5817cc8e8b8f8cca2d634ea7e1/src/prefect/engine/state.py>
    if new_state.is_retrying():
        msg = f"<@XXXX> {os.path.basename(__file__)} | {task} | {new_state}"
        <http://requests.post|requests.post>("<https://hooks.slack.com/services/>", json={"text": msg})

    if new_state.is_failed():
        msg = f"<@XXXX> {os.path.basename(__file__)} | {task} | {new_state}"
        <http://requests.post|requests.post>("<https://hooks.slack.com/services/>", json={"text": msg})

    # if new_state.is_successful():
    #     msg = f"<@XXXX> {os.path.basename(__file__)} | {task} | {new_state}"
    #     <http://requests.post|requests.post>("<https://hooks.slack.com/services/>", json={"text": msg})

    return new_state
I would like to avoid sending a success message for each task, and instead send one only if every task ran successfully. Thanks!
b
If you use cloud, there is an inbuilt "automations" in the UI so you don't have to write any code. It's awesome ☺️
t
I'm just using core at the moment, cloud would be great.
k
Hey @Thomas Nakamoto, I assume you have tasks that can Fail without the flow Failing, so you want a notification only if everything succeeds? Or do you just want a way to know if the Flow succeeds?
t
Hi @Kevin Kho just a way to know the Flow succeeded. I have a workflow that will run hourly and I would like a confirmation that it ran.
k
Oh. You should just use the SlackTask or Slack Notifier at the end on your Flow. The SlackTask is more configurable as I think the Slack Notifier requires you store the Secret on Prefect Cloud
t
@Kevin Kho thank you I will try out the SlackTask