# Zalo OA & Telegram Chatbot — API Store Hub

> VI: Kiến trúc webhook bất đồng bộ kết nối Zalo OA và Telegram Bot API tới https://apistorehub.com/v1 bằng model tốc độ cao, chống timeout webhook và chặn spam tin nhắn.
> EN: Build responsive Zalo OA and Telegram chatbots backed by https://apistorehub.com/v1 using async webhook acks, fast models, and per-user rate limits.

- Canonical VI: https://apistorehub.com/vi/tich-hop/zalo-oa-telegram-chatbot
- Canonical EN: https://apistorehub.com/en/tich-hop/zalo-oa-telegram-chatbot
- Language: TypeScript
- Runtime: Node.js 20+, Grammy / Telegraf / Zalo OA Webhook
- Protocol: OpenAI Compatible (/v1/chat/completions)
- Install: `npm install openai express`
- Auth Env Var: `API_KEY` (prefix `sk-ash-`)

## Step-by-Step Setup
1. Acknowledge Zalo OA or Telegram webhooks with HTTP 200 within 500ms before calling the LLM.
2. Deduplicate events by `msg_id` or `update_id` inside an async background queue.
3. Invoke `deepseek-v4-flash` or `gemini-3.8-flash` with a bounded rolling window of 6-10 messages.
4. Enforce per-user rate limits and send the generated reply back through the messaging API.

## Production Code Sample
```
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);
}
```

## Quick cURL Check
```bash
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?"}]}'
```

## Recommended Models (Live ST Pricing)
| Model | Live Price | Context | Datasheet |
|---|---|---|---|
| `deepseek-v4-flash` | 1,500 ST / 1M tokens | 1M | https://apistorehub.com/llms/models/deepseek-v4-flash.md |
| `gemini-3.8-flash` | 5,200 ST / 1M tokens | 1M | https://apistorehub.com/llms/models/gemini-3-8-flash.md |
| `claude-haiku-5-5` | 400 ST / 1M tokens | 200K | https://apistorehub.com/llms/models/claude-haiku-5-5.md |

## FAQ
- **How much does 10,000 chatbot replies cost with `deepseek-v4-flash`?**: Fixed per-request or Flash models keep 10,000 replies highly predictable and affordable in ST credit.
- **Can the bot read receipt photos sent by customers?**: Yes. Route image messages to vision-capable models like `gemini-3.8-flash` or `claude-sonnet-5-5`.
- **How do I prevent hallucinations on product prices?**: Ground answers via Function Calling against your live catalog database.
