API Store Hub での Zalo OA・Telegram チャットボット連携
Zalo OA・Telegram の Webhook 向けに、会話履歴の最適化と max_tokens 制御を備えた即応サポートボットを構築できます。
API Store Hub の単一 API キー・透明な料金・マルチプロバイダー切替で本番運用を始めましょう。
インストールコマンド
| 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 を設定: 公式パッケージをインストールし、/v1/chat/completions 向けの環境変数で https://apistorehub.com を指定します。
- ストリーミングとリトライ付きで実行: 明示的なタイムアウトとジッター付きリトライで deepseek-v4-flash を呼び出します。トークン数・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?"}]}'推奨モデルと最新料金
| モデル | トークン単価 | コンテキストとエンドポイント |
|---|---|---|
| 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 トークン超)では stream=true を有効化し、TTFT 短縮とタイムアウト防止を徹底してください。
- クライアントコードに API キーを埋め込まず、サーバー側の環境変数から読み込み API Store Hub で定期ローテーションしてください。
- 429/502/529 にはジッター付き指数バックオフを実装し、同一 API キーでフォールバックモデルを用意してください。
よくある質問
- 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- キーの変更だけで動作します。
- 1 つの API キーでストリーミング・Function Calling・Embeddings を併用できますか?
- はい。API Store Hub の 1 つのキーで全 187 モデル(SSE ストリーミング・厳格 JSON Schema・ツール呼び出し・Embeddings)を利用できます。
- 料金とレート制限はどのように管理されますか?
- すべてのリクエストは API Store Hub コンソールでトークンまたはリクエスト単位で透明に課金され、残高がマイナスにならずキー別の上限も設定可能です。