> This is a page from the ElevenLabs documentation. For a complete page index, fetch https://elevenlabs.io/docs/llms.txt. For the full documentation in a single file, fetch https://elevenlabs.io/docs/llms-full.txt.

# Supabase

接待员可以通过 Supabase 的 REST API 将通话详情保存到 Supabase 表中，并按电话号码查找来电者。以下设置使用项目的公开密钥及行级安全策略，因此接待员只能执行你允许的操作。

> **Warning**
>
> 切勿在 webhook 工具中使用 `service_role` 或密钥。这些密钥会绕过行级安全策略，
> 并授予数据库完整访问权限。

## 开始前

在 Supabase 控制台中，打开 **Project Settings** → **API Keys**，并复制 **publishable** 密钥或旧版 **anon** 密钥。项目 URL 为 `https://PROJECT_REF.supabase.co`。

## 准备数据库

在 **SQL Editor** 中运行以下内容。它会创建一张接待员只能插入数据的表，以及一个按精确电话号码返回单个客户的函数。该函数假设已有一个包含 `name`、`plan`、`notes` 和 `phone` 列的 `customers` 表，因此请根据架构调整名称。

来电者无法使用该函数列出客户：除非知道精确电话号码，否则函数不会返回任何内容。

```sql
create table callback_requests (
  id bigint generated always as identity primary key,
  created_at timestamptz not null default now(),
  caller_name text not null,
  phone text not null,
  reason text not null
);

alter table callback_requests enable row level security;

create policy "Receptionist can add callback requests"
  on callback_requests for insert to anon
  with check (true);

create function find_customer_by_phone(phone text)
returns table (name text, plan text, notes text)
language sql stable security definer set search_path = public
as $$
  select c.name, c.plan, c.notes from customers c where c.phone = find_customer_by_phone.phone limit 1;
$$;

revoke execute on function find_customer_by_phone from public;
grant execute on function find_customer_by_phone to anon;
```

## 创建工具

对于两个工具，前往 **Integrations**，选择 **Add integration**，选择 **Webhook Tool**，然后添加以下请求头：

| 请求头             | 值                                  |
| --------------- | ---------------------------------- |
| `apikey`        | 公开密钥或 anon 密钥                      |
| `Authorization` | `Bearer ` 后接 anon 密钥。使用公开密钥时请省略此项。 |

### 保存回电请求

| 字段         | 值                                                           |
| ---------- | ----------------------------------------------------------- |
| **名称**     | `save_callback_request`                                     |
| **描述**     | “只要承诺会后续联系来电者，就保存回电请求。”                                     |
| **方法**     | POST                                                        |
| **URL**    | `https://PROJECT_REF.supabase.co/rest/v1/callback_requests` |
| **请求体参数**  | `caller_name`、`phone` 和 `reason`（均为字符串，必填）                  |
| **分配给智能体** | **Receptionist**                                            |

### 查找客户

| 字段         | 值                                                                    |
| ---------- | -------------------------------------------------------------------- |
| **名称**     | `find_customer`                                                      |
| **描述**     | “按电话号码查找来电者账户。当来电者询问套餐或账户时使用。”                                       |
| **方法**     | POST                                                                 |
| **URL**    | `https://PROJECT_REF.supabase.co/rest/v1/rpc/find_customer_by_phone` |
| **请求体参数**  | `phone`（字符串，必填）：“采用 E.164 格式的来电者电话号码，例如 +14155550123。”               |
| **分配给智能体** | **Receptionist**                                                     |

在每个工具上选择 **Enable**，然后通过回电请求[测试接待员](/docs/zh/reception-ai/receptionist/testing)。该行会显示在 **Table Editor** 中。

## 故障排除

| 问题                                           | 检查内容                             |
| -------------------------------------------- | -------------------------------- |
| `new row violates row-level security policy` | 插入策略必须以 `anon` 角色为目标。            |
| `Could not find the function`                | 请求体参数必须与函数参数完全同名，此处为 `phone`。    |
| 401 `Invalid API key`                        | 使用公开密钥时，请移除 `Authorization` 请求头。 |
| 未找到客户                                        | 请以 E.164 格式存储电话号码，与接待员发送的格式一致。   |