Apache Airflow Telegram Provider

4.9.4 · active · verified Thu Apr 16

The `apache-airflow-providers-telegram` package extends Apache Airflow's functionality by providing operators to interact with the Telegram messaging platform. It allows users to send messages, images, and files directly from their Airflow DAGs. The current version is 4.9.4, and it follows Airflow's provider release cadence, with frequent updates to support new Airflow versions and Telegram API changes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up an Airflow DAG to send a message to Telegram using the `TelegramOperator`. You'll need to first create a Telegram bot via BotFather to get an API token, and then configure an Airflow connection named `telegram_default` with this token. The `chat_id` should be the ID of the chat, group, or channel where the message will be sent.

import os
from datetime import datetime

from airflow import DAG
from airflow.providers.telegram.operators.telegram import TelegramOperator

# Configure Telegram Connection in Airflow UI:
# Conn Id: telegram_default
# Conn Type: Telegram
# Password: <YOUR_TELEGRAM_BOT_TOKEN> (from BotFather)
# Host (optional): https://api.telegram.org (default)

# Replace with your actual chat ID or fetch dynamically
TELEGRAM_CHAT_ID = os.environ.get('TELEGRAM_CHAT_ID', '-1234567890') # Example: Group/Channel Chat ID, starts with '-' or User Chat ID
TELEGRAM_BOT_TOKEN = os.environ.get('TELEGRAM_BOT_TOKEN', 'YOUR_BOT_TOKEN') # Only if not using Airflow Connection

with DAG(
    dag_id='telegram_notification_dag',
    start_date=datetime(2023, 1, 1),
    schedule_interval=None,
    catchup=False,
    tags=['telegram', 'notification'],
) as dag:
    send_message_task = TelegramOperator(
        task_id='send_test_message',
        telegram_conn_id='telegram_default', # This uses the connection configured in Airflow UI
        chat_id=TELEGRAM_CHAT_ID,
        text='Hello from Airflow! This is a test message from {{ dag.dag_id }}.',
        token=TELEGRAM_BOT_TOKEN if TELEGRAM_BOT_TOKEN != 'YOUR_BOT_TOKEN' else None # Optional: override connection token
    )

view raw JSON →