feat: add Bark iOS push support with HTTP requests

This commit is contained in:
OpenClaw Agent 2026-02-09 03:42:51 +00:00
parent e715ca7350
commit da23eee4ec
4 changed files with 71 additions and 16 deletions

View File

@ -3,6 +3,10 @@ from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
import os
import sys
# 添加项目根目录到路径
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from backend.database import init_db
from backend.routers import channels, notify, history
@ -35,15 +39,15 @@ app.include_router(channels.router)
app.include_router(notify.router)
app.include_router(history.router)
# 静态文件(前端)
frontend_path = os.path.join(os.path.dirname(__file__), "..", "frontend")
if os.path.exists(frontend_path):
app.mount("/", StaticFiles(directory=frontend_path, html=True), name="frontend")
@app.get("/api/health")
async def health_check():
return {"status": "ok"}
# 静态文件(前端)- 放在最后
frontend_path = os.path.join(os.path.dirname(__file__), "..", "frontend")
if os.path.exists(frontend_path):
app.mount("/", StaticFiles(directory=frontend_path, html=True), name="frontend")
if __name__ == "__main__":
import uvicorn
uvicorn.run("backend.main:app", host="0.0.0.0", port=8000, reload=True)

View File

@ -48,6 +48,14 @@ class NotifyService:
elif channel.type == "apprise":
return config.get("url", "")
# Bark iOS 推送 - 在 send_notification 中直接处理
elif channel.type == "bark":
base_url = config.get("base_url", "")
device_key = config.get("device_key", "")
if base_url and device_key:
# 返回特殊标记,实际发送在 send_notification 中处理
return "__bark__"
return None
async def send_notification(
@ -101,7 +109,50 @@ class NotifyService:
}
try:
# 发送通知
# Bark 特殊处理 - 使用 requests
if channel.type == "bark":
import requests
import urllib.parse
base_url = channel.config.get("base_url", "").rstrip("/")
device_key = channel.config.get("device_key", "")
# 强制使用 HTTP因为你的 Bark 服务器 SSL 有问题)
if base_url.startswith("https://"):
base_url = base_url.replace("https://", "http://")
elif not base_url.startswith("http://"):
base_url = "http://" + base_url
# 构建 URL
encoded_body = urllib.parse.quote(str(body), safe='')
encoded_title = urllib.parse.quote(str(title), safe='') if title else ""
if encoded_title:
bark_url = f"{base_url}/{device_key}/{encoded_title}/{encoded_body}"
else:
bark_url = f"{base_url}/{device_key}/{encoded_body}"
# 添加参数
params = {}
if priority == "high":
params["level"] = "active"
elif priority == "urgent":
params["level"] = "critical"
try:
headers = {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X)',
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
}
response = requests.get(bark_url, params=params, timeout=10, headers=headers)
result = response.status_code == 200
print(f"Bark response: {response.status_code}, {response.text[:100]}")
except Exception as e:
result = False
print(f"Bark error: {e}")
else:
# 使用 apprise 发送通知
apobj = apprise.Apprise()
apobj.add(apprise_url)