'Conn_id isn't defined - Airflow-Discord Connection

am new at airflow and am trying to send messages to a discord server using airflow. I created the following dag to do it

from datetime import datetime

from airflow.models import DAG
from airflow.models import Variable
from airflow.providers.discord.operators.discord_webhook import DiscordWebhookOperator




with DAG('discord_alert', schedule_interval='@daily',
         catchup=False,
         tags=['discord']) as dag:


    discord_alert = DiscordWebhookOperator(
        task_id='discord_alert',
        http_conn_id = Variable.get('discord_conn_id'),
        webhook_endpoint = Variable.get('discord_webhook'),
        message = 'teste',
    )

    [discord_alert]

Its returning the following error The conn_id isn't defined

Tried creating a discord connection with UI where Connection type==Discord



Solution 1:[1]

You are mixing Connection and Variables.

You need to define Discord/HTTP connection in the UI as follows:

host as https://discord.com/api/ and default webhook endpoint in the extra field in the form of {"webhook_endpoint": "webhooks/{webhook.id}/{webhook.token}"}

Then you can use it as:

discord_alert = DiscordWebhookOperator(
        task_id='discord_alert',
        http_conn_id = "my_conn_id",
        message = 'teste',
    )

In cases where you want to specify a non default endpoint you can set:

 discord_alert = DiscordWebhookOperator(
        task_id='discord_alert',
        http_conn_id = "my_conn_id",
        webhook_endpoint="webhooks/{webhook.id}/{webhook.token}" #replace with actual values
        message = 'teste',
    )

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Elad Kalif