Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/use_notify/channels/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ def __init__(self, config: dict):
self.config = AdDict(config)

@abstractmethod
def send(self, message):
def send(self, content, title=None):
raise NotImplementedError

@abstractmethod
async def send_async(self, message):
async def send_async(self, content, title=None):
raise NotImplementedError
6 changes: 4 additions & 2 deletions src/use_notify/channels/chanify.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ def build_api_body(content, title=None):
def send(self, content, title=None):
api_body = self.build_api_body(content, title)
with httpx.Client() as client:
client.post(self.api_url, data=api_body, headers=self.headers)
response = client.post(self.api_url, data=api_body, headers=self.headers)
response.raise_for_status()
logger.debug("`chanify` send successfully")

async def send_async(self, content, title=None):
api_body = self.build_api_body(content, title)
async with httpx.AsyncClient() as client:
await client.post(self.api_url, data=api_body, headers=self.headers)
response = await client.post(self.api_url, data=api_body, headers=self.headers)
response.raise_for_status()
logger.debug("`chanify` send successfully")
10 changes: 6 additions & 4 deletions src/use_notify/channels/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ class Console(BaseChannel):
def __init__(self, config=None):
super().__init__(config or {})

def send(self, title, content):
def send(self, content, title=None):
"""发送通知到控制台"""
print(f"\n📢 [默认实例通知] {title}")
title_display = title or "消息提醒"
print(f"\n📢 [默认实例通知] {title_display}")
print(f"📝 {content}")
print("-" * 50)

async def send_async(self, title, content):
async def send_async(self, content, title=None):
"""异步发送通知到控制台"""
print(f"\n📢 [默认实例异步通知] {title}")
title_display = title or "消息提醒"
print(f"\n📢 [默认实例异步通知] {title_display}")
print(f"📝 {content}")
print("-" * 50)
6 changes: 4 additions & 2 deletions src/use_notify/channels/ding.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ def build_api_body(self, content, title=None):
def send(self, content, title=None):
api_body = self.build_api_body(content, title)
with httpx.Client() as client:
client.post(self.api_url, json=api_body, headers=self.headers)
response = client.post(self.api_url, json=api_body, headers=self.headers)
response.raise_for_status()
logger.debug("`钉钉` send successfully")

async def send_async(self, content, title=None):
api_body = self.build_api_body(content, title)
async with httpx.AsyncClient() as client:
await client.post(self.api_url, json=api_body, headers=self.headers)
response = await client.post(self.api_url, json=api_body, headers=self.headers)
response.raise_for_status()
logger.debug("`钉钉` send successfully")
6 changes: 4 additions & 2 deletions src/use_notify/channels/feishu.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ def build_api_body(self, content, title=None):
def send(self, content, title=None):
api_body = self.build_api_body(content, title)
with httpx.Client() as client:
client.post(self.api_url, json=api_body, headers=self.headers)
response = client.post(self.api_url, json=api_body, headers=self.headers)
response.raise_for_status()
logger.debug("`飞书` send successfully")

async def send_async(self, content, title=None):
api_body = self.build_api_body(content, title)
async with httpx.AsyncClient() as client:
await client.post(self.api_url, json=api_body, headers=self.headers)
response = await client.post(self.api_url, json=api_body, headers=self.headers)
response.raise_for_status()
logger.debug("`飞书` send successfully")
48 changes: 24 additions & 24 deletions src/use_notify/channels/pushdeer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

class PushDeer(BaseChannel):
"""pushdeer app 消息通知

支持三种消息类型:
- text: 纯文本消息 (默认)
- image: 图片消息
- markdown: Markdown格式消息

配置参数:
- token: PushDeer的pushkey
- base_url: 可选,自建PushDeer服务的URL,默认为"https://api2.pushdeer.com"
Expand All @@ -29,42 +29,38 @@ def api_url(self):
# 确保token存在
if not self.config.token:
raise ValueError("PushDeer token (pushkey) is required")

# Check if base_url exists in config, otherwise use default
if self.config.base_url:
base_url = self.config.base_url.rstrip("/")
else:
base_url = "https://api2.pushdeer.com"

return f"{base_url}/message/push"

def _prepare_params(self, content, title=None):
"""准备请求参数

Args:
content: 消息内容
title: 消息标题

Returns:
dict: 请求参数
"""
# 默认标题
if not title:
title = "消息提醒"

# 确定消息类型
msg_type = getattr(self.config, "type", "markdown")
if msg_type not in ["text", "markdown", "image"]:
if msg_type: # 只有当type不为空且无效时才记录警告
logger.warning(f"Invalid message type: {msg_type}, fallback to text")
msg_type = "markdown"

params = {
"pushkey": self.config.token,
"text": title,
"type": msg_type
}


params = {"pushkey": self.config.token, "text": title, "type": msg_type}

# 根据消息类型处理内容
if msg_type == "text":
# 对于text类型,content直接作为text参数
Expand All @@ -75,7 +71,7 @@ def _prepare_params(self, content, title=None):
elif msg_type == "image":
# 对于image类型,content应该是图片URL
params["desp"] = content

return params

@property
Expand All @@ -85,26 +81,30 @@ def headers(self):

def send(self, content, title=None):
"""发送PushDeer消息

Args:
content: 消息内容
title: 消息标题
"""
params = self._prepare_params(content, title)

with httpx.Client() as client:
client.get(self.api_url, params=params, headers=self.headers)
logger.debug(f"`pushdeer` send message successfully")
response = client.get(self.api_url, params=params, headers=self.headers)
response.raise_for_status()
logger.debug("`pushdeer` send message successfully")

async def send_async(self, content, title=None):
"""异步发送PushDeer消息

Args:
content: 消息内容
title: 消息标题
"""
params = self._prepare_params(content, title)

async with httpx.AsyncClient() as client:
await client.get(self.api_url, params=params, headers=self.headers)
logger.debug(f"`pushdeer` send message successfully")
response = await client.get(
self.api_url, params=params, headers=self.headers
)
response.raise_for_status()
logger.debug("`pushdeer` send message successfully")
6 changes: 4 additions & 2 deletions src/use_notify/channels/pushover.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ def build_api_body(self, content, title=None):
def send(self, content, title=None):
api_body = self.build_api_body(content, title)
with httpx.Client() as client:
client.post(self.api_url, data=api_body, headers=self.headers)
response = client.post(self.api_url, data=api_body, headers=self.headers)
response.raise_for_status()
logger.debug("`pushover` send successfully")

async def send_async(self, content, title=None):
api_body = self.build_api_body(content, title)
async with httpx.AsyncClient() as client:
await client.post(self.api_url, data=api_body, headers=self.headers)
response = await client.post(self.api_url, data=api_body, headers=self.headers)
response.raise_for_status()
logger.debug("`pushover` send successfully")
6 changes: 4 additions & 2 deletions src/use_notify/channels/wechat.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ def build_api_body(self, title, content):
def send(self, content, title=None):
api_body = self.build_api_body(title, content)
with httpx.Client() as client:
client.post(self.api_url, json=api_body, headers=self.headers)
response = client.post(self.api_url, json=api_body, headers=self.headers)
response.raise_for_status()
logger.debug("`WeChat` send successfully")

async def send_async(self, content, title=None):
api_body = self.build_api_body(title, content)
async with httpx.AsyncClient() as client:
await client.post(self.api_url, json=api_body, headers=self.headers)
response = await client.post(self.api_url, json=api_body, headers=self.headers)
response.raise_for_status()
logger.debug("`WeChat` send successfully")