Thread
#prefect-community
    Guilherme Petris

    Guilherme Petris

    9 months ago
    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
    Sylvain Hazard

    Sylvain Hazard

    9 months ago
    Are you working on a Linux environment ?
    Anna Geller

    Anna Geller

    9 months ago
    @Guilherme Petris it’s in your home directory: ~/.prefect but you can set it using env variable which is easier:
    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.
    Guilherme Petris

    Guilherme Petris

    9 months ago
    Yeah, webhooks!
    Well, i don’t have a .prefect folder here
    Anna Geller

    Anna Geller

    9 months ago
    can you try the env variable approach?
    Guilherme Petris

    Guilherme Petris

    9 months ago
    Well, i just run it on my venv and ran a test flow and nothing
    Anna Geller

    Anna Geller

    9 months ago
    If you set this in the terminal session:
    export PREFECT__CONTEXT__SECRETS__SLACK_WEBHOOK_URL="your_webook_url"
    and then in the same terminal session run:
    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?
    Guilherme Petris

    Guilherme Petris

    9 months ago
    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.
    Anna Geller

    Anna Geller

    9 months ago
    then this? 🙂
    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”?
    Guilherme Petris

    Guilherme Petris

    9 months ago
    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
    Anna Geller

    Anna Geller

    9 months ago
    in your terminal
    Guilherme Petris

    Guilherme Petris

    9 months ago
    { “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" } }
    Anna Geller

    Anna Geller

    9 months ago
    and if the Slack task doesn’t work for you due to secret, you can accomplish the same running:
    <http://requests.post|requests.post>(url=webhook_url, data=json.dumps({"text": "your slack message"}))
    Guilherme Petris

    Guilherme Petris

    9 months ago
    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.
    Anna Geller

    Anna Geller

    9 months ago
    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:
    prefect backend server
    prefect server start
    You can also set this env variable to be sure:
    export PREFECT__CLOUD__USE_LOCAL_SECRETS=true
    Guilherme Petris

    Guilherme Petris

    9 months ago
    Still getting the same error🙄
    Anna Geller

    Anna Geller

    9 months ago
    then you can use this instead:
    <http://requests.post|requests.post>(url=webhook_url, data=json.dumps({"text": "your slack message"}))
    Guilherme Petris

    Guilherme Petris

    9 months ago
    With the last one it works
    Then i can’t use the state_handlers pre built?
    Anna Geller

    Anna Geller

    9 months ago
    you can. Ex:
    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)
    Guilherme Petris

    Guilherme Petris

    9 months ago
    If i don’t have a .prefect , folder should i create one?
    Anna Geller

    Anna Geller

    9 months ago
    you can, but should be created automatically