| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- from fastapi import APIRouter, Query, Depends
- from sqlalchemy.ext.asyncio import AsyncSession
- from app.core.database import get_db
- from app.utils.response import success
- from app.schemas.common import ApiResponse, PageResponse
- from app.schemas.notification_outbox import NotificationOutboxCreate, NotificationOutboxOut, NotificationOutboxUpdate
- from app.schemas.telegram import TelegramIn, TelegramNoTokenIn
- from app.schemas.wechat import WechatIn, WechatNoTokenIn
- from app.schemas.whatsapp import WhatsappIn, WhatsappNoTokenIn
- from app.services.notification_outbox_service import NotificationOutboxService
- from app.services.telegram_service import TelegramService
- from app.services.wechat_service import WechatService
- from app.services.whatsapp_service import WhatsappService
- public_router = APIRouter()
- protected_router = APIRouter()
- admin_required_router = APIRouter()
- @admin_required_router.post("/tg/send_message", summary="推送电报消息", tags=["消息推送接口"], response_model=ApiResponse)
- async def tg_send_message(
- payload: TelegramIn
- ):
- await TelegramService.push_text(payload)
- return success()
- @admin_required_router.post("/tg/send_message_no_token", summary="推送电报消息(无需token)", tags=["消息推送接口"], response_model=ApiResponse)
- async def tg_send_message_no_token(
- payload: TelegramNoTokenIn
- ):
- await TelegramService.push_text_no_token(payload)
- return success()
- @admin_required_router.post("/wechat/send", summary="推送微信文本消息", tags=["消息推送接口"], response_model=ApiResponse)
- async def wechat_send(
- payload: WechatIn
- ):
- await WechatService.push_text(payload.api_token, payload.message)
- return success()
- @admin_required_router.post("/wechat/send_no_token", summary="推送微信文本消息(无需token)", tags=["消息推送接口"], response_model=ApiResponse)
- async def wechat_send_no_token(
- payload: WechatNoTokenIn
- ):
- await WechatService.push_text_no_token(payload.message)
- return success()
- @admin_required_router.post("/wechat/send_markdown_no_token", summary="推送微信Markdown消息(无需token)", tags=["消息推送接口"], response_model=ApiResponse)
- async def wechat_send_markdown_no_token(
- payload: WechatNoTokenIn
- ):
- await WechatService.push_markdown_no_token(payload.message)
- return success()
- @admin_required_router.post("/wechat/send_markdown", summary="推送微信Markdown消息", tags=["消息推送接口"], response_model=ApiResponse)
- async def wechat_send_markdown(
- payload: WechatIn
- ):
- await WechatService.push_markdown(payload.api_token, payload.message)
- return success()
- @admin_required_router.post("/whatsapp/send", summary="推送WhatsApp消息", tags=["消息推送接口"], response_model=ApiResponse)
- async def whatsapp_send(
- payload: WhatsappIn
- ):
- api_base_url = payload.api_base_url or "https://waha.text.skin"
- await WhatsappService.send_text(
- api_base_url=api_base_url,
- session=payload.session,
- chat_id=payload.chat_id,
- message=payload.message,
- api_key=payload.api_key,
- )
- return success()
- @admin_required_router.post("/whatsapp/send_no_token", summary="推送WhatsApp消息(无需token)", tags=["消息推送接口"], response_model=ApiResponse)
- async def whatsapp_send_no_token(
- payload: WhatsappNoTokenIn
- ):
- await WhatsappService.send_text_no_token(
- chat_id=payload.chat_id,
- message=payload.message,
- )
- return success()
- @admin_required_router.post("/notification/outbox/create", summary="创建通知消息", tags=["消息推送接口"], response_model=ApiResponse[NotificationOutboxOut])
- async def notification_outbox_create(
- payload: NotificationOutboxCreate,
- db: AsyncSession = Depends(get_db)
- ):
- obj = await NotificationOutboxService.create(db, payload)
- return success(data=obj)
- @admin_required_router.post("/notification/outbox/update", summary="更新通知消息", tags=["消息推送接口"], response_model=ApiResponse[NotificationOutboxOut])
- async def notification_outbox_update(
- id: int,
- payload: NotificationOutboxUpdate,
- db: AsyncSession = Depends(get_db)
- ):
- obj = await NotificationOutboxService.update(db, id, payload)
- return success(data=obj)
- @admin_required_router.get("/notification/outbox/detail", summary="获取通知详情", tags=["消息推送接口"], response_model=ApiResponse[NotificationOutboxOut])
- async def notification_outbox_detail(
- id: int,
- db: AsyncSession = Depends(get_db)
- ):
- obj = await NotificationOutboxService.get(db, id)
- return success(data=obj)
- @admin_required_router.get("/notification/outbox/list", summary="分页查询通知列表", tags=["消息推送接口"], response_model=ApiResponse[PageResponse[NotificationOutboxOut]])
- async def notification_outbox_list(
- status: str = Query("", description="状态"),
- channel: str = Query("", description="渠道"),
- priority: int = Query(0, description="优先级"),
- msg_id: str = Query("", description="消息ID"),
- page: int = Query(1, description="第几页"),
- size: int = Query(20, description="分页大小"),
- db: AsyncSession = Depends(get_db)
- ):
- obj = await NotificationOutboxService.list(db, status, channel, priority, msg_id, page, size)
- return success(data=obj)
|