Sending Notifications to Rocket.Chat with Airflow

Ertan Çelik
2 min readJun 24, 2024

--

In this article, we will discuss how to automate your analytical processes using Apache Airflow and send notifications to Rocket.Chat when these processes are completed. We will cover creating a bot account on Rocket.Chat and sending notifications to the your team channel.

Requirements

· Apache Airflow installation

· Rocket.Chat installation and access credentials

· Rocket.Chat bot account

· Rocket.Chat channel

Step 1: Define the Airflow DAG

First, you need to define your analytical processes within an Airflow DAG (Directed Acyclic Graph). In the code below, various tasks for the analytical processes are defined and set to run sequentially:

from airflow import DAG
from airflow.operators.python import PythonOperator
from airflow.operators.bash import BashOperator
from airflow.utils.dates import timezone
from datetime import datetime, timedelta
from airflow.operators.email_operator import EmailOperator
from airflow.operators.oracle_operator import OracleOperator
import pytz
from rocketchat_API.rocketchat import RocketChat

default_args = {
'owner': 'airflow',
'depends_on_past': False,
'start_date': datetime(2023, 9, 13, tzinfo=pytz.timezone('Europe/Istanbul')),
'retries': 0,
'retry_delay': timedelta(minutes=5)
}

dag = DAG(
'Analytic_Process',
default_args=default_args,
schedule_interval="0 10 * * 1",
catchup=False
)

# Task definitions (bash_task, t1, t2, t3, t4, t5, t6, t7, t8)
# ...

send_success_email = EmailOperator(
task_id='send_success_email',
to='**********.com.tr',
subject='Analytic_Process',
html_content="Analytic_Process Finished Successfully",
dag=dag)

Step 2: Create a Rocket.Chat Bot Account

You need to create a bot account on Rocket.Chat, which will be used to send notifications.

· Go to the Rocket.Chat admin panel.

· Navigate to Administration > Users.

· Create a new user (e.g., username: bot-yourteam, password: ****).

· Assign the bot role to this user.

Step 3: Create a Rocket.Chat Channel

Create a channel where the notifications will be sent (e.g., channel name: YourTeam).

· Go to the Rocket.Chat main page.

· Click on Create a new channel.

· Enter the channel name and create it.

· Add the bot account to this channel.

Step 4: Sending Notifications to Rocket.Chat

Define a Python function to send notifications using the Rocket.Chat API:

def send_notification_to_rocketchat(**kwargs):
rocket = RocketChat('bot-yourteam', '****',
server_url='https://rocketURL')
rocket.chat_post_message("TKN Extraction(your_message)", room_id="your_room_id")

Step 5: Use PythonOperator to Send Notifications

Add the notification function to your Airflow DAG and define a PythonOperator to call this function after the processes are completed:

notify_rocketchat_task = PythonOperator(
task_id='rocketchat_task',
python_callable=send_notification_to_rocketchat,
provide_context=True,
dag=dag
)

Conclusion

By following these steps, you can send notifications to your Rocket.Chat channel when your analytical processes defined in Airflow are completed. This setup makes it easier to monitor your processes and ensure they are completed successfully. With the Team bot account and channel settings, you can ensure notifications reach the right people.

--

--

Ertan Çelik
Ertan Çelik

Written by Ertan Çelik

Lead Data and Analytics Engineer

No responses yet