# 客户消息

连接商户渠道，记录客户同意，发送邮件、短信、语音或推送并读取回执。

更新于 2026-11-06

[网页版本](https://www.longplayai.com/zh-hans/docs/guides/messaging)

<a id="setup"></a>

## 连接商户渠道

向已同意接收消息的 Customer 发送邮件、短信、语音或推送。先安装匹配当前服务的 SDK，并确认 API 地址、App 和环境。

- 在 Console 的 接入 → 消息渠道（`/workspace/push-channels`）配置商户自己的服务商账号、发送身份和回调，并完成渠道验证。
- Email 使用 Resend，SMS 和 Voice 使用 Twilio，Push 使用 APNs 或 FCM。服务商直接向商户收费。
- 在后端使用具有 `messaging:write` 的同范围 Server Key。Customer Token 只能管理该客户的消息接收同意和设备，不能发送消息。

<a id="consent"></a>

## 记录客户同意

- 先认证你的用户，再绑定对应 Customer Token。用 `sdk.messaging.preference(channel)` 读取当前渠道同意状态和 revision；只有客户明确选择后，才用保留的 requestId、channel、state 和 expectedRevision 调用 `sdk.messaging.setPreference(...)`。
- Email 需要当前邮箱，SMS／Voice 需要 E.164 格式的 phone。未取得客户同意时不能发送；更换联系方式后，旧地址的同意状态不自动转移。
- Push 还需要受支持原生宿主登记安装、同步 provider token 和系统权限。系统通知权限、营销消息接收同意和退订／无效地址抑制分别检查。

- [Customer 与会话接入](https://www.longplayai.com/zh-hans/docs/guides/customers.md)

<a id="send"></a>

## 从后端发送并读取结果

下例向已同意接收消息的 Customer 发送一封邮件，再读取该消息。customerId 必须是当前 App／环境的 Customer UUID；先在你的业务记录中保存 requestId 和未来的 expiresAt，重试时沿用原始参数。

**messaging.server.ts**

```ts
import { createLongplayServer } from "@tinkerlandai/longplay-sdk/server";

const longplay = createLongplayServer({
  baseUrl: process.env.LONGPLAY_API_URL!,
  apiKey: process.env.LONGPLAY_SERVER_KEY!,
  scope: { projectId: process.env.LONGPLAY_PROJECT_ID!, environment: "sandbox" },
});

export async function sendUpdate(
  customerId: string,
  requestId: string,
  expiresAt: string,
) {
  const { message } = await longplay.sendMessage({
    requestId,
    customerId,
    expiresAt,
    message: {
      channel: "email",
      content: { subject: "Your offers", text: "Your saved offers are ready." },
    },
  });
  return longplay.readMessage({ messageId: message.messageId });
}
```

省略 connectionId 时，Email／SMS／Voice 使用该范围的主渠道；Push 使用各安装绑定的已验证渠道。发送请求不接受原始服务商密钥或任意目标地址。Python 使用 send_message／read_message，Go 使用 SendMessage／ReadMessage；字段和限制见各接口参考。

<a id="recovery"></a>

## 区分受理、送达与未知结果

- 保存返回的 messageId。sendMessage 的 accepted 只表示受理；用 readMessage 查看各 delivery 的状态和已验证 receipts。消息处理完成不代表服务商已送达。
- 请求超时后，用相同 requestId 和原始内容重试，取回同一条受理结果；不要换新 ID 再发。delivery 为 unknown 时，等待原发送的回执或对账结果。
- 渠道停用、联系方式变化、客户撤回同意、抑制、频率或过期都可能阻止后续派发。Email 退订和 SMS STOP 会改变对应渠道的同意状态；其他渠道不会自动获得客户同意。

<a id="broadcasts"></a>

## 群发、Funnel 与商户通知

在 Console 的 营销 → 电子邮件、短信、语音或推送（`/workspace/marketing/push`）创建单渠道群发，选择受众、内容和排期。Funnel 也可调用同一消息能力。两者都遵守当前客户同意状态与发送规则。

客户消息向客户发送消息。商户通知与 Webhook 则把购买、用量或任务结果发给你的后端；接入方式见独立指南。

- [商户通知与 Webhook](https://www.longplayai.com/zh-hans/docs/guides/notifications.md)
- [错误与恢复](https://www.longplayai.com/zh-hans/docs/api/errors.md)

<a id="operations"></a>

## 相关接口

- **客户消息与设备 (7)**:  [sendMessage](https://www.longplayai.com/zh-hans/docs/api/send-message.md); [readMessage](https://www.longplayai.com/zh-hans/docs/api/read-message.md); [bindPushInstallation](https://www.longplayai.com/zh-hans/docs/api/bind-push-installation.md); [readPushInstallation](https://www.longplayai.com/zh-hans/docs/api/read-push-installation.md); [updatePushAddress](https://www.longplayai.com/zh-hans/docs/api/update-push-address.md); [readMessagingPreference](https://www.longplayai.com/zh-hans/docs/api/read-messaging-preference.md); [setMessagingPreference](https://www.longplayai.com/zh-hans/docs/api/set-messaging-preference.md)

- [路径、凭据与权限见全部接口](https://www.longplayai.com/zh-hans/docs/api/operations.md#messaging)
