diff --git a/app/wechat/handler.py b/app/wechat/handler.py index 599bc7e..a9e0902 100644 --- a/app/wechat/handler.py +++ b/app/wechat/handler.py @@ -243,27 +243,26 @@ class WeChatMessageHandler: try: async for db in get_db(): - stmt = ( + result = await db.execute( select(Announcement) - .where(Announcement.keyword_matched == True) # noqa: E712 .order_by(desc(Announcement.publish_date)) - .limit(5) + .limit(6) ) - result = await db.execute(stmt) items = result.scalars().all() if not items: await self.client.send_text("暂无公告", from_user) return None - lines = ["最新公告(最近5条):"] - for i, ann in enumerate(items, 1): - date_str = ann.publish_date.strftime("%m-%d %H:%M") if ann.publish_date else "?" - title = ann.title[:28] + "..." if len(ann.title) > 28 else ann.title - source = ann.source_name or ann.source_code - lines.append(f"{i}. [{date_str}] {source}") - lines.append(f" {title}") - await self.client.send_text("\n".join(lines), from_user) + for ann in items: + title = ann.title if len(ann.title) <= 128 else ann.title[:125] + "..." + purchase_name = ann.purchase_name or "" + if len(purchase_name) > 25: + purchase_name = purchase_name[:22] + "..." + time_str = ann.publish_date.strftime("%Y-%m-%d %H:%M") if ann.publish_date else "时间未知" + source_name = ann.source_name or ann.source_code or "" + description = f"{source_name} | {purchase_name} | {time_str}" + await self.client.send_textcard(title, description, ann.content_url or "", from_user) except Exception as e: logger.error(f"查询最新公告失败: {e}") await self.client.send_text("查询失败,请稍后再试", from_user)