สร้าง Agentic Chat Bot ใน Google Chat ด้วย Apps Script + Agent2Agent (A2A)
Agent2Agent (A2A) คืออะไร — ทำไม Google ถึงเปิดตัว Protocol นี้
ลองนึกภาพว่าคุณมี Bot หลายตัวในองค์กร — ตัวหนึ่งจัดการ ปฏิทิน, อีกตัวหนึ่งจัดการ สต็อกสินค้า, อีกตัวตอบคำถาม HR แต่ละตัวเก่งเฉพาะทาง แต่ไม่สามารถ“คุยกัน”ได้ — ผู้ใช้ต้องสลับไปมาระหว่าง Bot เอง
Agent2Agent (A2A) คือ open protocol ที่ Google เปิดตัวเพื่อแก้ปัญหานี้ — มันเป็นภาษากลางที่ให้ AI agent หลายตัว ค้นพบกัน, เจรจา, และทำงานร่วมกันได้อัตโนมัติ โดยไม่สนว่าแต่ละ agent สร้างด้วยเครื่องมือหรือ framework อะไร↗ A2A Protocol
เมื่อผสมกับ Vertex AI Agent Engine (เครื่องมือสร้างและ deploy agent ของ Google Cloud) และ Google Chat (ช่องทางสื่อสารในองค์กร) — เราสามารถสร้าง Agentic Chat Bot ที่ผู้ใช้พิมพ์แค่ประโยคเดียวใน Google Chat แล้ว Orchestrator agent จะกระจายงานให้ specialist agents หลายตัวทำงานพร้อมกัน แล้วรวบรวมคำตอบกลับมาให้↗ Agent Engine
A2A vs Function Calling: Function Calling ให้ AI เรียกฟังก์ชันเดี่ยว ๆ — แต่ A2A ให้ agent ทั้งตัวคุยกับ agent อีกตัวได้ พร้อม context, memory, และ capability negotiation ทำให้เกิด workflow ที่ซับซ้อนกว่ามาก เช่น agent A ถาม agent B แล้วนำผลลัพธ์ไปต่อที่ agent C
ทำไมต้อง Stack นี้ — ตัวเลขและข้อเปรียบเทียบ
Google Chat มีผู้ใช้งานกว่า 3 พันล้านคนผ่าน Google Workspace ทั่วโลก และองค์กรไทยจำนวนมากที่ใช้ Gmail + Google Drive อยู่แล้วก็มี Google Chat พร้อมใช้งานทันทีโดยไม่ต้องติดตั้งอะไรเพิ่ม เมื่อรวมกับ Vertex AI Agent Engine ที่ deploy ได้ใน region asia-southeast1 (Bangkok) — latency ต่ำ, ข้อมูลอยู่ในประเทศ↗ Google Chat
เปรียบเทียบ: วิธีสร้าง Chat Bot ในองค์กร
| Feature | A2A + Agent Engine | Dialogflow CX | Custom (Node.js) |
|---|---|---|---|
| Multi-Agent | Native (A2A) | ต้อง custom | ต้อง custom |
| Deploy | Managed (Agent Engine) | Managed | Self-managed |
| Google Chat Integration | Apps Script (5 นาที) | Built-in | REST API |
| เพิ่ม Agent ใหม่ | Plug & Play (Agent Card) | ต้องแก้ Flow | ต้อง redeploy |
| ภาษาไทย | Gemini native | ต้อง train NLU | ขึ้นกับ model |
จุดแข็งสำคัญของ A2A คือ “Plug & Play” — เมื่อต้องการเพิ่มความสามารถใหม่ แค่สร้าง agent ใหม่แล้ว register Agent Card เข้าระบบ ไม่ต้องแก้โค้ด orchestrator เลย
— A2A Protocol Specification, Google 2025
A2A ทำให้ AI agent หลายตัว ค้นพบกัน เจรจา และทำงานร่วมกันได้อัตโนมัติ — เหมือนพนักงานใหม่ที่เข้ามาแล้วรู้ทันทีว่าต้องไปถามใคร
Build ทีละขั้น — จาก Google Chat สู่ Multi-Agent System
Step 1: สร้าง Google Chat App ด้วย Apps Script
// Google Chat จะเรียกฟังก์ชันนี้ทุกครั้งที่ผู้ใช้ส่งข้อความ function onMessage(event) { const userMessage = event.message.text; const userName = event.user.displayName; const spaceId = event.space.name; // ส่งข้อความไปยัง Orchestrator Agent ผ่าน A2A const agentReply = sendToOrchestrator({ message: userMessage, user: userName, context: { space: spaceId } }); // ส่งคำตอบกลับ Google Chat return { text: agentReply.message, cardsV2: buildResponseCards(agentReply) }; } // ทำงานเมื่อ Bot ถูกเพิ่มเข้า Space function onAddedToSpace(event) { return { text: `สวัสดีครับ! ผมคือ Agentic Bot ?\n` + `สั่งงานได้เลย เช่น:\n` + `• "จองห้องประชุมพรุ่งนี้ 10 โมง"\n` + `• "สรุปลาป่วยทีม Engineering เดือนนี้"\n` + `• "เช็คสต็อก iPhone 16 Pro"` }; }
ตั้ง Manifest: ใน appsscript.json ต้องเพิ่ม "chat": {} object พร้อม onMessage trigger และเปิด Google Chat API ใน GCP project ก่อน จากนั้น deploy → ไปที่ Google Chat → ค้นหาชื่อ Bot ที่ตั้งไว้↗ Chat App Quickstart
Step 4: เชื่อม Apps Script → Orchestrator ผ่าน A2A Protocol
const ORCHESTRATOR_URL = 'https://asia-southeast1-aiplatform.googleapis.com/v1/' + 'projects/YOUR_PROJECT/locations/asia-southeast1/' + 'agents/YOUR_ORCHESTRATOR_ID'; /** * ส่งข้อความไป Orchestrator Agent ผ่าน A2A protocol * Orchestrator จะ route ไปยัง specialist agents อัตโนมัติ */ function sendToOrchestrator(input) { // สร้าง A2A Task ตาม spec const a2aTask = { task: { id: Utilities.getUuid(), message: { role: 'user', parts: [{ type: 'text', text: input.message }] }, metadata: { user: input.user, source: 'google-chat', context: input.context } } }; const options = { method: 'post', contentType: 'application/json', headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() }, payload: JSON.stringify(a2aTask), muteHttpExceptions: true }; const res = UrlFetchApp.fetch( ORCHESTRATOR_URL + '/a2a/tasks:send', options ); const result = JSON.parse(res.getContentText()); // A2A ตอบกลับเป็น Task Result return { message: result.task.artifacts[0].parts[0].text, agents_used: result.task.metadata.agents_invoked, status: result.task.status.state }; }
Step 3: ตัวอย่าง Agent Card — Calendar Specialist
{
"name": "calendar-agent",
"description": "จัดการปฏิทิน จองห้องประชุม สร้าง event",
"url": "https://asia-southeast1-aiplatform.googleapis.com/...",
"capabilities": {
"streaming": false,
"pushNotifications": true
},
"skills": [
{
"id": "book-room",
"name": "จองห้องประชุม",
"description": "จองห้องประชุมตามวัน เวลา จำนวนคน",
"tags": ["calendar", "meeting", "room"]
},
{
"id": "create-event",
"name": "สร้าง Calendar Event",
"description": "สร้าง event ใน Google Calendar พร้อม invite",
"tags": ["calendar", "event", "invite"]
}
],
"authentication": {
"schemes": ["google-oauth"]
}
}
Agent Card คือหัวใจของ A2A: มันคือ “นามบัตร” ของ agent — บอกว่า agent นี้ทำอะไรได้บ้าง (skills), เรียกได้ที่ไหน (url), และต้อง authenticate อย่างไร เมื่อ orchestrator ได้รับคำสั่งจากผู้ใช้ มันจะอ่าน Agent Card ของทุก agent ที่ register ไว้แล้วเลือก route งานไปให้ agent ที่เหมาะสมที่สุด
Bonus: สร้าง Rich Card ใน Google Chat
// แปลง agent response เป็น Google Chat Card function buildResponseCards(agentReply) { if (!agentReply.agents_used) return []; return [{ cardId: 'agent-result', card: { header: { title: 'Agentic Bot Response', subtitle: `Agents used: ${agentReply.agents_used.join(', ')}`, imageUrl: 'https://fonts.gstatic.com/s/i/short-term/release/googlesymbols/smart_toy/default/48px.svg', imageType: 'CIRCLE' }, sections: [{ widgets: [{ decoratedText: { topLabel: 'Status', text: agentReply.status === 'completed' ? '✅ สำเร็จ' : '⏳ กำลังดำเนินการ', } }, { decoratedText: { topLabel: 'Result', text: agentReply.message, wrapText: true } }] }] } }]; }
เหมาะกับใคร ไม่เหมาะกับใคร
Agentic Chat Bot ด้วย A2A + Agent Engine ฟังดูทรงพลัง — แต่ไม่ได้เหมาะกับทุกกรณี นี่คือ 2 มุมมองที่ต้องชั่งน้ำหนัก
A2A เหมือนกับ REST API ในยุค 2000 — ตอนแรกคนคิดว่า overkill แต่สุดท้ายกลายเป็นมาตรฐานที่ทุกระบบต้องรองรับ Agent protocol จะเป็นแบบเดียวกัน
— มุมมอง Long-term Optimist
ต่อยอดอย่างไร — จาก Codelab สู่ Production
Codelab ข้างต้นให้คุณมี working prototype — Google Chat Bot ที่พูดกับ Orchestrator ซึ่งกระจายงานให้ specialist agents ได้ ต่อไปนี้คือแนวทางพัฒนาสู่ production
1. เพิ่ม Specialist Agents ตาม use case จริง
สร้าง agent แยกสำหรับแต่ละทีม: HR Agent (เช็คลาพัก, สวัสดิการ), IT Agent (แจ้งปัญหา, reset password), Finance Agent (เช็คใบเสนอราคา, สถานะ PO) — แต่ละตัวมี Agent Card เป็นของตัวเอง
2. เพิ่ม Memory & Context ด้วย Vertex AI Search
ให้ agents เข้าถึง knowledge base ขององค์กรผ่าน Vertex AI Search — เช่น ค้นหา policy document, คู่มือพนักงาน, หรือ FAQ ที่อยู่ใน Google Drive↗ Vertex AI Search
3. เชื่อม MCP สำหรับ External Tools
ใช้ Model Context Protocol (MCP) ร่วมกับ A2A — A2A ให้ agents คุยกัน ส่วน MCP ให้ agents เข้าถึงเครื่องมือภายนอก (database, API, file system) ได้อย่างปลอดภัย ทั้งสอง protocol ทำงานเสริมกัน
4. ตั้ง Guardrails & Approval Flow
สำหรับ action ที่มีผลกระทบสูง (เช่น อนุมัติค่าใช้จ่าย, แก้ไขข้อมูล HR) — เพิ่ม human-in-the-loop ให้ agent ส่ง Google Chat Card พร้อมปุ่ม Approve/Reject ก่อน execute จริง
สำหรับองค์กรไทย: เริ่มจาก agent 2-3 ตัวก่อน (เช่น Calendar + IT Helpdesk) — ทดสอบกับทีมเล็ก ๆ — เก็บ feedback — แล้วค่อย ๆ เพิ่ม agents ผ่าน A2A ข้อดีคือเพิ่ม agent ใหม่ไม่กระทบ agent เดิมเลย เพราะ A2A เป็น loosely coupled architecture
Insight: อนาคตของ enterprise software ไม่ใช่ app ก้อนเดียวที่ทำทุกอย่าง — แต่เป็น กองทัพ agents เฉพาะทางที่คุยกันผ่าน protocol มาตรฐานอย่าง A2A โดยผู้ใช้ไม่ต้องรู้ด้วยซ้ำว่ามี agent กี่ตัวทำงานอยู่เบื้องหลัง — แค่พิมพ์ภาษาไทยใน Google Chat แล้วทุกอย่างเกิดขึ้นเอง
References
- [1] Google Developers — Agent2Agent (A2A) Protocol Documentation
- [2] Google Cloud — Vertex AI Agent Engine
- [3] Google Developers — Build a Google Chat App with Apps Script
- [4] Google Codelabs — Build an Agentic Google Chat App with A2A
- [5] Google Cloud — Vertex AI Search (Enterprise Knowledge Base)
- [6] Google Workspace — Google Chat for Business
- [7] Google Chat API — REST Reference