Add your description here
Project description
zangetsu-notification
多様な通知メカニズムをシンプルなインターフェースで提供する通知ライブラリです。
特徴
- 多様な通知先サポート:Slack、Microsoft Teams、Email、LINE Notify、カスタム Webhook など
- 統一インターフェース:すべての通知先で一貫したメソッド呼び出し
- 複数チャネル対応:1 回の操作で複数の通知先に同時送信
- エラー耐性:一部の通知先が失敗しても処理を継続
- 環境変数サポート:設定の外部化が容易
- 拡張性:新しい通知先を追加する仕組みを提供
- 柔軟な設定:コード内での設定と環境変数による設定の両方をサポート
インストール
pip install zangetsu-notification
基本的な使い方
Slack 通知
from zangetsu_notification import SlackNotification
# 環境変数 SLACK_WEBHOOK_URL を使用する場合
slack = SlackNotification()
# または直接Webhook URLを指定
slack = SlackNotification(webhook_url="https://hooks.slack.com/services/XXX/YYY/ZZZ")
# 基本的なメッセージ送信
slack.send_message("テスト通知です", username="通知ボット", icon_emoji=":bell:")
# エラー通知
slack.send_error_message(
"バッチ処理エラー",
"データ処理中にタイムアウトが発生しました"
)
# 成功通知
slack.send_success_message(
"バッチ処理完了",
"100件のデータを正常に処理しました"
)
Microsoft Teams 通知
from zangetsu_notification import TeamsNotification
# 環境変数 TEAMS_WEBHOOK_URL を使用する場合
teams = TeamsNotification()
# またはURLを直接指定
teams = TeamsNotification(webhook_url="https://outlook.office.com/webhook/XXX/YYY/ZZZ")
# 基本的なメッセージ送信
teams.send_message(
"テスト通知です",
title="通知タイトル",
subtitle="詳細情報"
)
# エラー通知(赤色のカード)
teams.send_error_message("API接続エラー", "認証情報が無効です")
# 成功通知(緑色のカード)
teams.send_success_message("デプロイ完了", "アプリケーションがデプロイされました")
メール通知
from zangetsu_notification import EmailNotification
# SMTP設定を直接指定
email = EmailNotification(
smtp_server="smtp.gmail.com",
smtp_port=587,
smtp_username="your-email@gmail.com",
smtp_password="your-app-password",
sender_email="your-email@gmail.com"
)
# 基本的なメール送信
email.send_message(
message="テストメッセージ本文",
subject="テスト通知",
recipients=["recipient@example.com"],
cc=["cc-recipient@example.com"],
html_message="<p>HTMLフォーマットの<strong>メッセージ</strong></p>"
)
# エラー通知メール
email.send_error_message(
"バックアップエラー",
"バックアップ処理中にディスク容量不足が発生しました",
recipients=["admin@example.com"]
)
LINE Notify 通知
from zangetsu_notification import LineNotification
# 環境変数 LINE_NOTIFY_TOKEN を使用する場合
line = LineNotification()
# またはトークンを直接指定
line = LineNotification(access_token="YOUR_LINE_NOTIFY_TOKEN")
# 基本的なメッセージ送信
line.send_message("テスト通知です")
# 画像付きメッセージ
line.send_message(
"画像付きメッセージ",
image_url="https://example.com/image.jpg"
)
# エラー通知
line.send_error_message("システムエラー", "サーバー接続が切断されました")
Webhook 通知
from zangetsu_notification import WebhookNotification
# Webhook URL設定
webhook = WebhookNotification(
webhook_url="https://example.com/api/webhook",
headers={"Authorization": "Bearer token123"}
)
# 基本的なメッセージ送信
webhook.send_message(
"テスト通知です",
payload={
"severity": "info",
"source": "batch_process"
}
)
# エラー通知
webhook.send_error_message(
"データベースエラー",
"クエリのタイムアウトが発生しました"
)
複数通知先への同時送信
from zangetsu_notification import create_notifier
# 複数通知先の設定
notifier = create_notifier(
["slack", "email"], # 通知タイプのリスト
{
"slack": {
"webhook_url": "https://hooks.slack.com/services/XXX/YYY/ZZZ"
},
"email": {
"smtp_server": "smtp.gmail.com",
"smtp_port": 587,
"smtp_username": "your-email@gmail.com",
"smtp_password": "your-password",
"sender_email": "your-email@gmail.com"
}
}
)
# 両方の通知先に一度に送信
notifier.send_message("重要な更新があります")
notifier.send_error_message("緊急エラー", "システムがダウンしています")
notifier.send_success_message("デプロイ成功", "新バージョンがデプロイされました")
環境変数から自動設定
from zangetsu_notification import from_env
# 環境変数の設定例
# export NOTIFICATION_TYPE=slack,line
# export SLACK_WEBHOOK_URL=https://hooks.slack.com/services/XXX/YYY/ZZZ
# export LINE_NOTIFY_TOKEN=your-line-token
# 環境変数から通知設定を自動的に読み込み
notifier = from_env()
# 設定されたすべての通知先に送信
notifier.send_message("環境変数から設定された通知です")
環境変数リファレンス
各通知先では以下の環境変数がサポートされています:
| 通知先 | 環境変数 | 説明 |
|---|---|---|
| 全般 | NOTIFICATION_TYPE |
カンマ区切りの通知タイプリスト(例: slack,email,line) |
| Slack | SLACK_WEBHOOK_URL |
Slack の Webhook URL |
| Teams | TEAMS_WEBHOOK_URL |
Microsoft Teams の Webhook URL |
SMTP_SERVER |
SMTP サーバーアドレス | |
SMTP_PORT |
SMTP ポート(デフォルト: 587) | |
SMTP_USERNAME |
SMTP ユーザー名 | |
SMTP_PASSWORD |
SMTP パスワード | |
SENDER_EMAIL |
送信元メールアドレス | |
DEFAULT_EMAIL_RECIPIENTS |
デフォルトの受信者(カンマ区切り) | |
| LINE | LINE_NOTIFY_TOKEN |
LINE Notify アクセストークン N |
| Webhook | WEBHOOK_URL |
カスタム Webhook URL |
拡張
新しい通知先を追加するには、NotificationBaseクラスを継承して必要なメソッドを実装します:
from zangetsu_notification.base import NotificationBase
class MyCustomNotification(NotificationBase):
"""カスタム通知クラス"""
def __init__(self, api_key=None):
super().__init__()
self.api_key = api_key or os.getenv("MY_SERVICE_API_KEY")
# 初期化処理
def send_message(self, message, **kwargs):
# メッセージ送信の実装
return True
def send_error_message(self, error_message, error_details=None, **kwargs):
# エラーメッセージ送信の実装
return True
def send_success_message(self, success_message, additional_info=None, **kwargs):
# 成功メッセージ送信の実装
return True
貢献
- リポジトリをフォーク
- 機能ブランチを作成 (
git checkout -b feature/amazing-feature) - 変更をコミット (
git commit -m 'Add some amazing feature') - ブランチをプッシュ (
git push origin feature/amazing-feature) - プルリクエストを作成
ライセンス
[社内ライセンス名] - 詳細は LICENSE ファイルを参照してください。
依存関係
- Python 3.9+
- requests
トラブルシューティング
- Slack 通知が送信されない: Webhook URL が正しいことと、アプリがチャンネルに追加されていることを確認してください
- メール送信エラー: SMTP 設定を確認し、Gmail の場合はアプリパスワードを使用してください
- LINE 通知エラー: トークンの有効期限が切れていないか確認してください
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file zangetsu_notification-0.1.3.tar.gz.
File metadata
- Download URL: zangetsu_notification-0.1.3.tar.gz
- Upload date:
- Size: 20.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dcd180e2197cee4bc64c990dc1ac5791f4bc47dac82e55b03127a32ab167c805
|
|
| MD5 |
89b7dfaf50b61b6be31050009e6544c9
|
|
| BLAKE2b-256 |
b62facbd84ef8d298213629db65b48f4ea56115bbf2d37640cc8493013463f11
|
File details
Details for the file zangetsu_notification-0.1.3-py3-none-any.whl.
File metadata
- Download URL: zangetsu_notification-0.1.3-py3-none-any.whl
- Upload date:
- Size: 17.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
789641d195795632e3cf8a7de1e3a417d607f36767496bbc04673814adb33d00
|
|
| MD5 |
4c63aa33aea5193efd603dc1ea9111d3
|
|
| BLAKE2b-256 |
abb19624de25b597808e1a5fbf124748ba889ed97136aafbf1821eeb0f8ad9fd
|