สร้าง Agentic Chat Bot ใน Google Chat ด้วย Apps Script + Agent2Agent (A2A) | Chontechhub

สร้าง Agentic Chat Bot ใน Google Chat ด้วย Apps Script + Agent2Agent (A2A) | Chontechhub
Chontechhub
Codelab · Agentic AI

สร้าง Agentic Chat Bot ใน Google Chat ด้วย Apps Script + Agent2Agent (A2A)

10 เมษายน 2569 14 min read Chontechhub Editorial
User
Google Chat
Chat App
Apps Script
A2A
Orchestrator
Agent Engine
Agent A
Specialist
Architecture — User → Google Chat → Orchestrator (A2A) → Specialist Agents
Chontechhub Editorial
Agentic AI Codelab · qr4tech.com
Codelab
01

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

02

ทำไมต้อง Stack นี้ — ตัวเลขและข้อเปรียบเทียบ

Google Chat มีผู้ใช้งานกว่า 3 พันล้านคนผ่าน Google Workspace ทั่วโลก และองค์กรไทยจำนวนมากที่ใช้ Gmail + Google Drive อยู่แล้วก็มี Google Chat พร้อมใช้งานทันทีโดยไม่ต้องติดตั้งอะไรเพิ่ม เมื่อรวมกับ Vertex AI Agent Engine ที่ deploy ได้ใน region asia-southeast1 (Bangkok) — latency ต่ำ, ข้อมูลอยู่ในประเทศ↗ Google Chat

3B+
Workspace Users
Google Chat built-in
A2A
Open Protocol
Cross-framework
<2s
Agent Response
Gemini Flash + BKK region
$0
Apps Script Cost
Free tier

เปรียบเทียบ: วิธีสร้าง 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 หลายตัว ค้นพบกัน เจรจา และทำงานร่วมกันได้อัตโนมัติ — เหมือนพนักงานใหม่ที่เข้ามาแล้วรู้ทันทีว่าต้องไปถามใคร

03

Build ทีละขั้น — จาก Google Chat สู่ Multi-Agent System

Step 1
สร้าง Google Chat App ด้วย Apps Script
สร้าง Apps Script project ใหม่ → เปิด Google Chat API → ตั้ง manifest เป็น Chat App → deploy เป็น Chat Bot ในองค์กร
Step 2
สร้าง Orchestrator Agent บน Agent Engine
สร้าง agent ใน Vertex AI Agent Engine → กำหนด system instruction ให้เป็น orchestrator → เปิด A2A endpoint
Step 3
สร้าง Specialist Agents + Agent Card
สร้าง agent เฉพาะทาง (เช่น Calendar Agent, HR Agent) แต่ละตัวมี Agent Card ระบุ capability ที่ orchestrator ค้นพบได้
Step 4
เชื่อม Apps Script → Orchestrator ผ่าน A2A
เขียน Apps Script ให้ส่งข้อความจาก Google Chat ไปยัง orchestrator agent ผ่าน A2A protocol แล้วส่งคำตอบกลับ
Step 5
ทดสอบ Multi-Agent Flow ใน Google Chat
ทดสอบสั่งงานภาษาไทย เช่น “จองห้องประชุมพรุ่งนี้บ่ายโมง แล้วส่ง invite ทีม Marketing” → orchestrator กระจายให้ Calendar Agent + Email Agent

Step 1: สร้าง Google Chat App ด้วย Apps Script

Apps Script — ChatApp.gs
// 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

Apps Script — A2AClient.gs
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

JSON — agent-card.json
{
  "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

Apps Script — Cards.gs
// แปลง 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
          }
        }]
      }]
    }
  }];
}
04

เหมาะกับใคร ไม่เหมาะกับใคร

Agentic Chat Bot ด้วย A2A + Agent Engine ฟังดูทรงพลัง — แต่ไม่ได้เหมาะกับทุกกรณี นี่คือ 2 มุมมองที่ต้องชั่งน้ำหนัก

เหมาะมากสำหรับ
องค์กรที่ใช้ Google Workspace — Google Chat พร้อมใช้ทันที ไม่ต้องติดตั้ง
Workflow ข้ามทีม — เช่น HR + Calendar + Inventory ทำงานร่วมกัน
ต้องการ scalability — เพิ่ม agent ใหม่ง่าย ไม่ต้องแก้ orchestrator
ทีมที่มี agents หลาย framework — A2A เป็น protocol กลาง ไม่ผูกกับ tool ใดตัวเดียว
ข้อจำกัดที่ต้องรู้
A2A ยังใหม่มาก — ecosystem ยังเล็ก, documentation อาจไม่ครบ
ต้องมี GCP project — Agent Engine ต้องใช้ Google Cloud (มีค่าใช้จ่าย)
Overkill สำหรับ Bot เดี่ยว — ถ้าต้องการแค่ bot 1 ตัว ใช้ Gemini ตรงดีกว่า
Debugging ซับซ้อน — Multi-agent flow มี moving parts มาก trace ยากกว่า

A2A เหมือนกับ REST API ในยุค 2000 — ตอนแรกคนคิดว่า overkill แต่สุดท้ายกลายเป็นมาตรฐานที่ทุกระบบต้องรองรับ Agent protocol จะเป็นแบบเดียวกัน

— มุมมอง Long-term Optimist
05

ต่อยอดอย่างไร — จาก 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

← Back to Blog