完成Help Scout渠道的完整功能实现,包含API客户端、webhook处理、轮询同步、自动回复、工单标签分配、客户资料管理等核心能力,附带完整的配置Schema与插件元信息
92 lines
2.7 KiB
Python
92 lines
2.7 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from yuxi.channel.extensions.helpscout.auth import HelpScoutAuth
|
|
from yuxi.channel.extensions.helpscout.client import HelpScoutClient
|
|
from yuxi.channel.extensions.helpscout.config import resolve_account
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class HelpScoutSatisfaction:
|
|
def __init__(self):
|
|
self._ratings: list[dict] = []
|
|
|
|
async def record_rating(
|
|
self,
|
|
payload: dict,
|
|
) -> dict | None:
|
|
rating = payload.get("rating", {})
|
|
conv_id = rating.get("conversationId")
|
|
score = rating.get("rating", 0)
|
|
|
|
record = {
|
|
"id": rating.get("id", ""),
|
|
"conversation_id": conv_id,
|
|
"rating": score,
|
|
"feedback": rating.get("feedback", ""),
|
|
"created_at": rating.get("createdAt", ""),
|
|
}
|
|
self._ratings.append(record)
|
|
|
|
level = "positive" if score == 1 else "negative"
|
|
logger.info(
|
|
"Help Scout satisfaction: conv=%s rating=%s feedback=%s",
|
|
conv_id,
|
|
level,
|
|
rating.get("feedback", ""),
|
|
)
|
|
|
|
return record
|
|
|
|
async def get_stats(
|
|
self,
|
|
mailbox_id: int | None = None,
|
|
account_id: str | None = None,
|
|
) -> dict:
|
|
all_ratings = self._ratings
|
|
positive = sum(1 for r in all_ratings if r["rating"] == 1)
|
|
total = len(all_ratings)
|
|
satisfaction_rate = positive / total * 100 if total > 0 else 0.0
|
|
|
|
return {
|
|
"total": total,
|
|
"positive": positive,
|
|
"negative": total - positive,
|
|
"satisfaction_rate": round(satisfaction_rate, 2),
|
|
}
|
|
|
|
async def log_rating_to_note(
|
|
self,
|
|
conversation_id: int,
|
|
rating: int,
|
|
feedback: str = "",
|
|
account_id: str | None = None,
|
|
) -> None:
|
|
account = resolve_account(account_id or "default")
|
|
client = self._build_client(account)
|
|
|
|
try:
|
|
level = "Satisfied" if rating == 1 else "Not Satisfied"
|
|
note = f"Customer Satisfaction: {level}"
|
|
if feedback:
|
|
note += f"\nFeedback: {feedback}"
|
|
await client.create_note(conversation_id=conversation_id, text=note)
|
|
logger.debug(
|
|
"Help Scout satisfaction note added: conv=%d",
|
|
conversation_id,
|
|
)
|
|
except Exception:
|
|
logger.exception(
|
|
"Help Scout satisfaction note failed: conv=%d",
|
|
conversation_id,
|
|
)
|
|
finally:
|
|
await client.close()
|
|
|
|
@staticmethod
|
|
def _build_client(account: dict) -> HelpScoutClient:
|
|
auth = HelpScoutAuth(account["app_id"], account["app_secret"])
|
|
return HelpScoutClient(auth)
|