ntfy API를 활용한 스마트 알림 시스템 구축

빠른 시작: 첫 번째 알림 보내기

핵심 개념 및 환경 설정

ntfy는 단순한 HTTP 요청으로 모바일 및 데스크톱에 알림을 전송하는 오픈소스 시스템입니다. 주요 구성 요소:

  • 주제(Topic): 고유 이름으로 식별되는 알림 채널
  • 발행(Publish): 특정 주제로 알림 전송
  • 구독(Subscribe): 주제 알림 수신

기본 알림 전송

curl을 사용한 공개 서버 테스트:

curl -d "ntfy 초기 테스트" ntfy.sh/my_channel

이 명령은 'my_channel' 주제로 메시지를 발행하며, ntfy 앱에서 해당 주제를 구독하여 확인 가능합니다.

다중 언어 구현 예제

import requests
channel = "monitor_channel"
requests.post(f"https://ntfy.sh/{channel}", data="파이썬에서 전송")
const topicUrl = 'https://ntfy.sh/log_channel';
fetch(topicUrl, {
  method: 'POST',
  body: '자바스크립트 알림'
});
package main

import (
  "net/http"
  "strings"
)

func main() {
  http.Post("https://ntfy.sh/go_alerts", 
            "text/plain", 
            strings.NewReader("Go 프로그램 알림"))
}

고급 기능 구현

우선순위 설정

5단계 우선순위(min=1, low=2, default=3, high=4, urgent=5)로 알림 중요도 제어:

curl -H "Priority: urgent" -d "디스크 사용량 95% 초과" ntfy.sh/system_alerts

사용자 정의 헤더

타이틀과 태그로 알림 가독성 향상:

curl -H "Title: 작업 완료" -H "Tags: success,checkmark" \
  -d "정기 백업 성공" ntfy.sh/task_status

상호작용 버튼

액션 버튼을 통한 사용자 응답 처리:

curl -H "Actions: http, 차고 열기, https://home-api/garage/open; \
               http, 조명 끄기, https://home-api/lights/off" \
  -d "외출 상태: 장치를 제어하시겠습니까?" ntfy.sh/home_automation

실제 적용 사례

서버 모니터링

Grafana 웹훅 연동:

curl -H "Content-Type: application/json" \
  -d '{"text":"CPU 과부하 발생"}' \
  ntfy.sh/infra_monitor?template=grafana

백업 스크립트 통합

#!/bin/bash
tar -czf /backup/data.tgz /data
if [ $? -eq 0 ]; then
  curl -H "Tags: white_check_mark" -d "백업 성공" ntfy.sh/backup_log
else
  curl -H "Priority: high" -H "Tags: x" -d "백업 실패" ntfy.sh/backup_log
fi

자동화 제어

curl -H "Actions: http, 에어컨 켜기, https://iot-api/ac/on" \
  -d "실내 온도 30°C: 에어컨을 가동할까요?" ntfy.sh/climate_ctrl

자체 호스팅 구성

개인 서버 설정

Docker 기반 배포:

docker run -p 8080:80 -v ntfy_data:/var/lib/ntfy binwiederhier/ntfy serve

접근 제어

# 서버 설정 파일
auth:
  default-access: deny
  users:
    - login: admin
      password: encrypted_pw
      permissions:
        - "alerts:write"
        - "logs:read"

성능 최적화 기법

메시지 일괄 처리

import time
from requests import post

def send_bulk(notifications):
    combined = "\n".join(notifications)
    post("https://ntfy.sh/batch_channel", data=combined)
    time.sleep(0.5)  # 요청 간 간격 설정

상태 동기화

// 읽음 상태 표시
fetch('https://ntfy.sh/my_topic/msg123/read', { 
  method: 'PUT' 
})

태그: ntfy HTTP-API 푸시알림 서버모니터링 자동화

6월 26일 17:55에 게시됨