完成Help Scout渠道的完整功能实现,包含API客户端、webhook处理、轮询同步、自动回复、工单标签分配、客户资料管理等核心能力,附带完整的配置Schema与插件元信息
88 lines
3.1 KiB
Python
88 lines
3.1 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.protocols import ChannelAccountSnapshot
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class HelpScoutStatus:
|
|
default_runtime = None
|
|
|
|
async def probe(self, account: dict | None = None) -> bool:
|
|
if not account or not account.get("app_id") or not account.get("app_secret"):
|
|
logger.warning("Help Scout probe: account not configured")
|
|
return False
|
|
|
|
mailbox_id = int(account.get("mailbox_id", 0))
|
|
if mailbox_id == 0:
|
|
return False
|
|
|
|
auth = HelpScoutAuth(account["app_id"], account["app_secret"])
|
|
client = HelpScoutClient(auth)
|
|
|
|
try:
|
|
await auth.ensure_token()
|
|
mailbox = await client.get_mailbox(mailbox_id)
|
|
logger.info(
|
|
"Help Scout probe OK: mailbox=%s status=active",
|
|
mailbox.get("name", mailbox_id),
|
|
)
|
|
return True
|
|
except Exception as e:
|
|
logger.warning("Help Scout probe failed: %s", e)
|
|
return False
|
|
finally:
|
|
await client.close()
|
|
|
|
def build_summary(self, snapshot: object) -> dict:
|
|
return {
|
|
"account_id": getattr(snapshot, "account_id", ""),
|
|
"status": "ok" if getattr(snapshot, "probe_ok", False) else "error",
|
|
}
|
|
|
|
def build_account_snapshot(
|
|
self,
|
|
account: dict,
|
|
config: dict,
|
|
runtime: object | None = None,
|
|
probe_result: object | None = None,
|
|
audit: object | None = None,
|
|
) -> ChannelAccountSnapshot:
|
|
status_state = "connected" if probe_result else "not-connected"
|
|
return ChannelAccountSnapshot(
|
|
account_id=account.get("account_id", "default"),
|
|
name=account.get("mailbox_name", "Help Scout"),
|
|
enabled=config.get("enabled", False),
|
|
configured=bool(
|
|
account.get("app_id") and account.get("app_secret") and int(account.get("mailbox_id", 0)) > 0
|
|
),
|
|
status_state=status_state,
|
|
dm_policy=account.get("auto_reply_mode", "draft"),
|
|
probe_ok=bool(probe_result) if probe_result is not None else None,
|
|
)
|
|
|
|
def collect_status_issues(self, accounts: list) -> list:
|
|
issues = []
|
|
for acct in accounts:
|
|
if not getattr(acct, "configured", False):
|
|
issues.append(
|
|
{
|
|
"account_id": acct.account_id,
|
|
"severity": "error",
|
|
"message": "Help Scout: appId/appSecret/mailboxId 未配置",
|
|
}
|
|
)
|
|
elif not getattr(acct, "probe_ok", True):
|
|
issues.append(
|
|
{
|
|
"account_id": acct.account_id,
|
|
"severity": "error",
|
|
"message": "Help Scout API 无法连接: OAuth Token 或 Mailbox ID 无效",
|
|
}
|
|
)
|
|
return issues
|