Hey! Noob question here - can’t seem to find where...
# ask-community
g
Hey! Noob question here - can’t seem to find where is the .prefect folder to add the slack token in the config.toml as described here: https://docs.prefect.io/core/advanced_tutorials/slack-notifications.html#installation-instructions
i know that .prefect is hidden, but i guess this one is playing hide and seek here
s
Are you working on a Linux environment ?
a
@Guilherme Petris it’s in your home directory: ~/.prefect but you can set it using env variable which is easier:
Copy code
export PREFECT__CONTEXT__SECRETS__SLACK_WEBHOOK_URL="your_webook_url"
just to be sure: with “token” you mean Slack webhook, correct? Asking because Slack task expects a webhook, from what I remember Slack was using but then deprecated tokens in favor of webhooks.
g
Yeah, webhooks!
👍 1
Well, i don’t have a .prefect folder here
a
can you try the env variable approach?
g
Well, i just run it on my venv and ran a test flow and nothing
a
If you set this in the terminal session:
Copy code
export PREFECT__CONTEXT__SECRETS__SLACK_WEBHOOK_URL="your_webook_url"
and then in the same terminal session run:
Copy code
from prefect.tasks.notifications import SlackTask

SlackTask("Hi Guilherme!").run()
You should get: If not then either your webhook doesn’t work, or something is wrong with your Prefect installation
If the above doesn’t work, can you send the output of your “prefect diagnostics” along with your flow file?
g
Copy code
Python 3.9.7 (default, Oct 13 2021, 06:45:31) 
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from prefect.tasks.notifications import SlackTask
>>> SlackTask("Hi!").run()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/guilherme.petris/analytics-toolbox/venv/lib/python3.9/site-packages/prefect/utilities/tasks.py", line 456, in method
    return run_method(self, *args, **kwargs)
  File "/Users/guilherme.petris/analytics-toolbox/venv/lib/python3.9/site-packages/prefect/tasks/notifications/slack_task.py", line 61, in run
    webhook_url = webhook_url or cast(str, Secret(webhook_secret).get())
  File "/Users/guilherme.petris/analytics-toolbox/venv/lib/python3.9/site-packages/prefect/client/secrets.py", line 167, in get
    raise ValueError(
ValueError: Local Secret "SLACK_WEBHOOK_URL" was not found.
a
then this? 🙂
Copy code
from prefect.tasks.notifications import SlackTask
import os

os.environ["PREFECT__CONTEXT__SECRETS__SLACK_WEBHOOK_URL"] = "your_url"
SlackTask("Hi Guilherme!").run()
I mean, you can see that something with your local secret is wrong. Are you on Cloud?
can you send the output of your “prefect diagnostics”?
g
I’m not on Cloud - need to test some stuff locally and because of privacy stuff we need to run stuff locally
What do you mean by prefect diagnostics? can’t find this here
a
in your terminal
g
{ “config_overrides”: {}, “env_vars”: [ “PREFECT__CONTEXT__SECRETS__SLACK_WEBHOOK_URL” ], “system_information”: { “platform”: “macOS-11.6.1-x86_64-i386-64bit”, “prefect_backend”: “cloud”, “prefect_version”: “0.15.9", “python_version”: “3.9.7" } }
a
and if the Slack task doesn’t work for you due to secret, you can accomplish the same running:
Copy code
<http://requests.post|requests.post>(url=webhook_url, data=json.dumps({"text": "your slack message"}))
g
Copy code
Even with the other code that you sent i'm getting this: Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/guilherme.petris/analytics-toolbox/venv/lib/python3.9/site-packages/prefect/utilities/tasks.py", line 456, in method
    return run_method(self, *args, **kwargs)
  File "/Users/guilherme.petris/analytics-toolbox/venv/lib/python3.9/site-packages/prefect/tasks/notifications/slack_task.py", line 61, in run
    webhook_url = webhook_url or cast(str, Secret(webhook_secret).get())
  File "/Users/guilherme.petris/analytics-toolbox/venv/lib/python3.9/site-packages/prefect/client/secrets.py", line 167, in get
    raise ValueError(
ValueError: Local Secret "SLACK_WEBHOOK_URL" was not found.
a
Regarding privacy: in Prefect Cloud we don’t receive any user code or data, your data remains completely private within your infrastructure: https://www.prefect.io/why-prefect/hybrid-model/ Regarding your issue: your config says you are on Cloud, so if you want to run this on Server, you should do:
Copy code
prefect backend server
prefect server start
You can also set this env variable to be sure:
Copy code
export PREFECT__CLOUD__USE_LOCAL_SECRETS=true
g
Still getting the same error🙄
a
then you can use this instead:
Copy code
<http://requests.post|requests.post>(url=webhook_url, data=json.dumps({"text": "your slack message"}))
g
With the last one it works
🙌 1
Then i can’t use the state_handlers pre built?
a
you can. Ex:
Copy code
import pendulum
from typing import cast

import prefect
from prefect import task, Flow
from prefect.engine import signals
from prefect.client import Secret
from prefect.storage import Docker
from prefect.tasks.notifications import SlackTask


def notify_on_failure(task, old_state, new_state):
    if new_state.is_failed():
        if isinstance(new_state.result, Exception):
            value = "```{}```".format(repr(new_state.result))
        else:
            value = cast(str, new_state.message)
        now = pendulum.now().isoformat()
        msg = (
            f"The task `{prefect.context.task_name}` failed on {now} "
            f"in a flow run {prefect.context.flow_run_id} "
            f"with an exception `{value}`"
        )
        # we use Slack here, but it might be Teams or a hook URL to any other messaging service
        # webhook_url = Secret("SLACK_WEBHOOK_URL").get()
        <http://requests.post|requests.post>(url=webhook_url, data=json.dumps({"text": msg}))
    return new_state


@task(state_handlers=[post_to_slack_on_failure])
def fail_successfully(x):
    return 1 / x


with Flow(name="state-handler-demo-flow") as flow:
    result = fail_successfully(x=0)
g
If i don’t have a .prefect , folder should i create one?
a
you can, but should be created automatically