Telnyx 推出跨邮件、语音和短信携带上下文的 AI 智能体
AI Agent That Carries Context Across Email, Voice, and SMS
Telnyx 发布跨渠道上下文携带 AI 智能体示例,客户从邮件转到电话或短信时无需重复说明来意。该示例用单个 API key 调用 Telnyx 的 Email。
| name | omnichannel-context-carrying | |||||
|---|---|---|---|---|---|---|
| title | 全渠道上下文携带 Agent | |||||
| description | 一个 AI agent,可在电子邮件、语音和短信之间携带客户上下文,让客户无需重复自己。演示了使用 Telnyx Inference、Email、Messaging 和 Call Control API 进行跨渠道身份解析。 | |||||
| language | python | |||||
| framework | flask | |||||
| telnyx_products |
|
|||||
| channel |
|
全渠道上下文携带 Agent — 客户上下文跨渠道跟随
一个 AI agent,可在电子邮件、语音和短信之间携带客户上下文,让客户无需重复自己。当客户在发送电子邮件后致电时,AI 会这样问候他们:“我看到您联系我们更改预订。您来电是为了同一个请求吗?”
为什么选择 Telnyx
Telnyx 是一个 AI 通信基础设施平台 — 电子邮件、消息、语音和 AI 都运行在一个私有全球网络上。此示例使用单个 API 密钥调用四个 Telnyx API(Email、Messaging、Call Control、AI Inference),以演示通过客户身份解析实现无缝的跨渠道上下文携带。
使用的 Telnyx API 端点
- 发送电子邮件:
POST /v2/emails— API 参考 - 发送消息:
POST /v2/messages— API 参考 - 创建呼叫:
POST /v2/calls— API 参考 - 接听呼叫:
POST /v2/calls/{call_control_id}/actions/answer— API 参考 - 语音播报:
POST /v2/calls/{call_control_id}/actions/speak— API 参考 - 收集语音:
POST /v2/calls/{call_control_id}/actions/gather— API 参考 - AI Inference:
POST /v2/ai/chat/completions— API 参考
Telnyx Webhook 事件
此应用处理以下 webhook 事件:
email.received— 入站电子邮件触发 AI 回复流程call.initiated— 来电触发身份查找和接听call.answered— AI 根据跨渠道历史生成上下文问候语call.speak.ended— TTS 播放完成,开始收集语音call.gather.ended— 捕获客户语音,AI 结合完整上下文进行处理call.hangup— 呼叫结束message.received— 入站短信触发带有跨渠道上下文的 AI 回复
架构
Customer contacts on any channel
│ │ │
Email in Call in SMS in
│ │ │
▼ ▼ ▼
┌─────────────────────────────┐
│ Flask app.py │
│ │
│ ┌───────────────────────┐ │
│ │ Customer Identity │ │
│ │ Registry (SQLite) │ │
│ │ email ↔ phone ↔ id │ │
│ └───────────┬───────────┘ │
│ │ │
│ ┌───────────▼───────────┐ │
│ │ Cross-Channel │ │
│ │ Context Store │ │
│ │ (SQLite) │ │
│ └───────────┬───────────┘ │
│ │ │
│ ┌───────────▼───────────┐ │
│ │ Telnyx AI Inference │ │
│ │ (context-aware │ │
│ │ greeting + reply) │ │
│ └───────────┬───────────┘ │
│ │ │
│ ┌────────┼────────┐ │
│ │ │ │ │
│ Email API SMS API Voice │
└─────────────────────────────┘
│ │ │
▼ ▼ ▼
Customer receives contextual
responses on every channel
关键差异点:当客户切换渠道(例如,先发电子邮件再致电)时,AI 会解析其身份,加载完整的跨渠道对话历史,并生成引用其先前互动的问候语 — 因此他们无需重复自己。
环境变量
将 .env.example 复制为 .env 并填写:
| 变量 | 类型 | 示例 | 必需 | 描述 | 获取位置 |
|---|---|---|---|---|---|
TELNYX_API_KEY |
string |
KEY0123456789ABCDEF |
是 | Telnyx API v2 密钥 | 门户 · CLI:telnyx auth |
TELNYX_FROM_NUMBER |
string |
+15551234567 |
是 | Telnyx 电话号码(短信 + 语音) | 门户 |
TELNYX_EMAIL_FROM |
string |
agent@yourdomain.com |
是 | 已验证的发件人电子邮件 | 门户 |
MESSAGING_PROFILE_ID |
string |
40017b7e-... |
否 | 消息配置文件 ID | 门户 |
CONNECTION_ID |
string |
1494404757140276705 |
是 | Call Control 连接 ID | 门户 |
AI_MODEL |
string |
meta-llama/Llama-3.3-70B-Instruct |
否 | Telnyx Inference 模型 | 模型 |
PORT |
integer |
5000 |
否 | HTTP 服务器端口 | — |
DB_PATH |
string |
conversations.db |
否 | SQLite 数据库路径 | — |
Agent / CLI 访问 — 通过 Telnyx CLI 配置上述资源:
telnyx auth login telnyx available-phone-numbers list --country US --features sms,voice telnyx number-orders create --phone-number +15551234567 telnyx messaging-profiles create --name "Context Carrying Agent"
设置
选项 A — 演示模式(无需凭据)
git clone https://github.com/team-telnyx/telnyx-code-examples.git
cd telnyx-code-examples/omnichannel-context-carrying-python
pip install -r requirements.txt
python demo/demo_server.py该演示展示了“Sarah 的预订变更” — 她发送电子邮件询问重新安排时间,然后致电并听到 AI 结合她电子邮件中的完整上下文向她问候。无需 Telnyx 凭据。
编程 / CLI 设置
# Install CLI — https://developers.telnyx.com/development/cli go install github.com/team-telnyx/telnyx-cli/cmd/telnyx@latest telnyx auth login # Provision resources telnyx available-phone-numbers list --country US --features sms,voice telnyx number-orders create --phone-number +15551234567 telnyx messaging-profiles create --name "Context Carrying Agent"
要进行完整的 API 发现,请将你的 agent 指向 llms-full.txt。
选项 B — 生产模式(使用凭据)
git clone https://github.com/team-telnyx/telnyx-code-examples.git cd telnyx-code-examples/omnichannel-context-carrying-python cp .env.example .env # ← fill in your credentials pip install -r requirements.txt python app.py # starts on http://localhost:5000
注册客户并测试流程:
# Register a customer identity curl -X POST http://localhost:5000/customers/register \ -H "Content-Type: application/json" \ -d '{ "id": "cust_sarah_chen", "name": "Sarah Chen", "email": "sarah@example.com", "phone": "+15551234567" }'
Webhook 配置
-
暴露你的本地服务器:
ngrok http 5000
-
复制 HTTPS URL 并在 Telnyx 门户 中配置:
- 呼叫控制应用 → Webhook URL →
https://<id>.ngrok.io/webhooks/voice - 消息配置文件 → 入站 Webhook URL →
https://<id>.ngrok.io/webhooks/messaging - 电子邮件 → 入站 Webhook URL →
https://<id>.ngrok.io/webhooks/email
- 呼叫控制应用 → Webhook URL →
API 参考
POST /customers/register
注册客户身份以进行跨渠道解析。
curl -X POST http://localhost:5000/customers/register \ -H "Content-Type: application/json" \ -d '{"id": "cust_001", "name": "Sarah Chen", "email": "sarah@example.com", "phone": "+15551234567"}'
GET /customers
列出所有已注册的客户。
curl http://localhost:5000/customers
GET /customers/<id>/context
查看客户的跨渠道上下文。
curl http://localhost:5000/customers/cust_001/context
GET /conversations
查看所有跨渠道对话历史记录,按客户分组。
curl http://localhost:5000/conversations
POST /demo/journey
触发完整的脚本化演示流程(仅限演示服务器)。
curl -X POST http://localhost:5555/demo/journey
GET /health
健康检查端点。
curl http://localhost:5000/health
Webhook 端点
POST /webhooks/email
接收 Telnyx Email webhook 事件。在 email.received 时:通过电子邮件解析客户身份,存储消息,并在后台线程中触发 AI 生成的回复。
POST /webhooks/voice
接收 Telnyx Call Control webhook 事件。在 call.initiated(来电)时:通过电话号码解析客户并接听。在 call.answered 时:加载跨渠道历史记录并生成上下文相关的问候语。在 call.gather.ended 时:结合完整上下文处理客户语音。
POST /webhooks/messaging
接收 Telnyx Messaging webhook 事件。在 message.received(入站)时:通过电话号码解析客户,存储消息,并触发带有跨渠道上下文的 AI 回复。
测试
python -m pytest smoke_test.py -v
故障排除
| 问题 | 原因 | 修复 |
|---|---|---|
401 Unauthorized |
API 密钥无效 | 验证 .env 中的 TELNYX_API_KEY 与 Portal 中的密钥匹配 |
| 电子邮件未发送 | 发件人未验证 | 在 Telnyx Email Portal 中验证您的电子邮件域 |
| 呼叫未连接 | CONNECTION_ID 无效 | 在 Portal 中验证您的呼叫控制应用 |
| 无上下文问候语 | 客户未注册 | 通过 POST /customers/register 使用匹配的电子邮件/电话注册客户 |
| 未收到 Webhook | 服务器无法公开访问 | 使用 ngrok 暴露并在 Portal 中设置 Webhook URL |
| 入站时未知客户 | 电子邮件/电话不在注册表中 | 确保客户的电子邮件/电话与其注册信息匹配 |
代理发现
此示例是 Telnyx Code Examples 目录的一部分。
- 代理注册:telnyx.com/agent-signup.md — 通过代理邮件自动配置账户
- 代理 CLI:github.com/team-telnyx/ai/tree/main/cli
- LLM 优化文档:
llms-full.txt - 示例索引:
llms.txt
相关示例
资源
来源:Hacker News · github.com