Zalo OA 与 Telegram 聊天机器人接入 API Store Hub
基于 Zalo OA 与 Telegram Webhook 构建极速客服机器人,精简对话历史并精准控制 max_tokens。
准备好通过 API Store Hub 的统一 API 密钥、透明计费与多上游容灾把 AI 投入生产了吗?
安装命令
| Language | TypeScript |
|---|---|
| Runtime | Node.js 20+, Grammy / Telegraf / Zalo OA Webhook |
| Protocol | OpenAI Compatible (/v1/chat/completions) |
| 安装命令 | npm install openai express |
3 步快速接入流程
- 在 API Store Hub 创建 API 密钥: 登录 API Store Hub 控制台生成前缀为 sk-ash- 的密钥,并按需配置日/月消费上限。
- 安装 SDK 并配置 Base URL: 安装官方 SDK 包并将环境变量指向 https://apistorehub.com,以通过 /v1/chat/completions 发起请求。
- 调用模型并启用流式与重试: 使用显式超时与带抖动重试调用 deepseek-v4-flash;每次调用均记录 token、TTFT 与精确费用。
生产级示例代码
TypeScript
import OpenAI from "openai";
const ai = new OpenAI({ baseURL: "https://apistorehub.com/v1", apiKey: process.env.API_KEY });
export async function handleChatWebhook(userId: string, text: string, sendReply: (msg: string) => Promise<void>) {
const completion = await ai.chat.completions.create({
model: "deepseek-v4-flash",
max_tokens: 512,
messages: [
{ role: "system", content: "You are a helpful Vietnamese customer support assistant. Keep answers under 4 sentences." },
{ role: "user", content: `[user:${userId}] ${text}` },
],
});
const answer = completion.choices[0]?.message?.content?.trim() || "Vui lòng thử lại sau ít phút.";
await sendReply(answer);
}cURL
curl https://apistorehub.com/v1/chat/completions \
-H "Authorization: Bearer sk-ash-your_key" \
-H "Content-Type: application/json" \
-d '{"model":"deepseek-v4-flash","max_tokens":256,"messages":[{"role":"user","content":"Chính sách đổi trả hàng trong bao lâu?"}]}'推荐模型与实时价格
| 模型 | Token 单价 | 上下文与端点 |
|---|---|---|
| DeepSeek V4 Flash | 1,500 ST / 1M tokens | 1M |
| Gemini 3.8 Flash | 5,200 ST / 1M tokens | 1M |
| Claude Haiku 5.5 | 400 ST / 1M tokens | 200K |
生产环境最佳实践清单
- 请先立即向 Webhook 返回 HTTP 200 再异步调用 AI,避免平台超时重推重复消息。
- 对长输出(> 500 token)始终开启 stream=true,以降低首字延迟(TTFT)并防止连接超时。
- 切勿在前端代码中硬编码密钥;请通过服务端环境变量注入并在 API Store Hub 控制台定期轮换。
- 为 429/502/529 配置带抖动的指数退避,并在同一密钥下准备备用模型实现无缝回退。
常见问题解答
- 将 Zalo OA & Telegram Chatbot 接入 API Store Hub 是否需要重写现有调用逻辑?
- 不需要。API Store Hub 原生兼容 OpenAI(https://apistorehub.com/v1)与 Anthropic Messages(https://apistorehub.com/v1/messages)协议,只需替换 Base URL 与 sk-ash- 密钥。
- 一个 API 密钥可以同时支持流式输出、函数调用和向量嵌入吗?
- 可以。API Store Hub 的单一密钥即可调用全部 187 个模型,完整支持 SSE 流式、严格 JSON Schema、工具调用与向量嵌入。
- 费用与速率限制如何控制?
- 每次请求均按 token 或按次透明计费并可在 API Store Hub 控制台查询,绝不透支余额,且支持为单个密钥设置消费上限。