ตั้งค่า User ID

ตั้งค่า User ID

GPTBots ช่วยให้นักพัฒนาตั้งค่ารหัสประจำตัวผู้ใช้ (UserId) ที่ไม่ซ้ำกันสำหรับผู้ใช้ Agent ในแต่ละช่องทาง (เช่น เว็บไซต์, แอป, LiveChat) โดย UserId นี้จะเชื่อมโยงตัวตนผู้ใช้ข้ามช่องทางต่าง ๆ เพื่อรวมข้อมูลตัวตน, ค้นหาข้อมูลธุรกิจผ่าน Tools และดูแลข้อมูลคุณสมบัติผู้ใช้และประวัติการสนทนา ตัวอย่างการใช้งาน UserId เช่น

  • Tools: เมื่อ AI Agent เรียกใช้ Tools เพื่อส่งคำขอไปยัง API ของธุรกิจ นักพัฒนาจะได้รับ UserId ใน header เพื่อระบุตัวผู้ใช้
  • คุณสมบัติผู้ใช้: หลังตั้งค่า UserId ข้อมูลคุณสมบัติของผู้ใช้จะถูกผูกกับ UserId นี้
  • ประวัติการสนทนา: หลังตั้งค่า UserId ประวัติการสนทนาระหว่างผู้ใช้กับ Agent จะถูกผูกกับ UserId นี้
  • Event Callback: หลังตั้งค่า UserId ข้อมูล Event Callback ที่เกิดใน iframe/widget และรายงานไปยัง GA4/webhook จะมี UserId แนบไปด้วย

⚠️ User ID (UserId) ควรเป็นรหัสประจำตัวที่ไม่ซ้ำกันของผู้ใช้ในระบบธุรกิจของนักพัฒนา โดย UserId นี้สามารถนำไปใช้ค้นหาข้อมูลธุรกิจ เช่น ระดับ VIP, แท็กผู้ใช้, ข้อมูลคำสั่งซื้อ ฯลฯ

วิธีตั้งค่า User ID

API นี้ช่วยให้นักพัฒนาตั้งค่า User ID โดยใช้การรวมกันของ anonymous ID + ประเภทการสนทนา + source ID เพื่อจัดการข้อมูลตัวตนผู้ใช้

วิธีการร้องขอ

POST

Endpoint

https://api.${endpoint}/v1/user/set-userid

ตัวอย่างการร้องขอ

curl -X POST 'https://api.${endpoint}/v1/user/set-userid' \ -H 'Authorization: Bearer ${token}' \ -H 'Content-Type: application/json' \ -d '{ "user_id": "67b58121035e5b152b0419ee", "anonymous_ids": [ { "anonymous_id": "6a0dnyvi3jc32flk7enw", "conversation_type": "SHARE" }, { "anonymous_id": "6a0dnyvi3jc32flk7enw", "conversation_type": "TELEGRAM", "source_id": "bot_029392" } ] }'
                      
                      curl -X POST 'https://api.${endpoint}/v1/user/set-userid' \
-H 'Authorization: Bearer ${token}' \
-H 'Content-Type: application/json' \
-d '{
    "user_id": "67b58121035e5b152b0419ee",
    "anonymous_ids": [
        {
            "anonymous_id": "6a0dnyvi3jc32flk7enw",
            "conversation_type": "SHARE"
        },
        {
            "anonymous_id": "6a0dnyvi3jc32flk7enw",
            "conversation_type": "TELEGRAM",
            "source_id": "bot_029392"
        }
    ]
}'

                    
บล็อกโค้ดนี้ในหน้าต่างลอย

user_id หนึ่งรายการ สามารถผูกกับ anonymous_id ได้สูงสุด 100 รายการ หากเกินระบบจะลบ binding ที่มี updateTime เก่าที่สุดโดยอัตโนมัติ
นักพัฒนาสามารถดึง anonymous ID ของผู้ใช้ปัจจุบันได้จากตัวแปร global anonymous_id และประเภทการสนทนาจาก conversation_type

Request Headers

ฟิลด์ ประเภท คำอธิบาย
Authorization Bearer ${token} ใช้ Authorization: Bearer ${token} สำหรับการยืนยันตัวตน กรุณาขอรับ token จากหน้า API Key
Content-Type application/json ประเภทข้อมูล ค่าคือ: application/json

Request Body

พารามิเตอร์ ประเภท คำอธิบาย จำเป็น
user_id string User ID ที่นักพัฒนากำหนดเอง ใช่
anonymous_ids array รายการ anonymous ID ที่สร้างโดยแพลตฟอร์ม GPTBots โดยอิงจากรหัสเฉพาะของแต่ละแพลตฟอร์ม สามารถดึงได้จาก anonymous_id ใน global variables ของ Agent ใช่
anonymous_ids[].anonymous_id string Anonymous ID ใช่
anonymous_ids[].conversation_type string แพลตฟอร์มต้นทางของ anonymous ID เทียบเท่ากับ platform ใน 'Agent-Integration' เช่น WHATSAPP, LINE ฯลฯ สามารถดูได้จาก conversation_type ใน user overview ใช่
anonymous_ids[].source_id string Channel ID จากแพลตฟอร์มต้นทาง เช่น หากเชื่อมต่อกับ TELEGRAM และเพิ่ม TG Bot สองตัว แต่ละ Bot จะมี Source ID ของตัวเอง ไม่จำเป็น

ตรรกะการผูกข้อมูล:

  1. ความสัมพันธ์การผูกจะถูกกำหนดโดย anonymous_id + conversation_type + source_id ที่ไม่ซ้ำกัน
  2. หาก combination นี้ถูกผูกกับ user_id ปัจจุบันอยู่แล้ว จะอัปเดตเฉพาะเวลาการผูก (updateTime)
  3. หาก combination นี้ยังไม่ผูกกับ user_id ใด จะสร้างความสัมพันธ์ใหม่
  4. หาก combination นี้ผูกกับ user_id อื่นอยู่ก่อน จะยกเลิก binding เก่าแล้วผูกกับ user_id ปัจจุบัน
  5. หาก binding ของ user_id เดียวเกิน 100 รายการ ระบบจะลบ binding ที่มี updateTime เก่าที่สุดโดยอัตโนมัติ

Response

ตัวอย่าง Response

{ "code": 0, "message": "OK", "data": { "user_id": "67b58121035e5b152b0419ee", "anonymous_ids": [ { "anonymous_id": "6a0dnyvi3jc32flk7enw", "conversation_type": "SHARE", "source_id": null }, { "anonymous_id": "6a0dnyvi3jc32flk7enw", "conversation_type": "TELEGRAM", "source_id": "bot_029392" } ] } }
                      
                      {
    "code": 0,
    "message": "OK",
    "data": {
        "user_id": "67b58121035e5b152b0419ee",
        "anonymous_ids": [
            {
                "anonymous_id": "6a0dnyvi3jc32flk7enw",
                "conversation_type": "SHARE",
                "source_id": null
            },
            {
                "anonymous_id": "6a0dnyvi3jc32flk7enw",
                "conversation_type": "TELEGRAM",
                "source_id": "bot_029392"
            }
        ]
    }
}

                    
บล็อกโค้ดนี้ในหน้าต่างลอย

คำอธิบาย Response ที่สำเร็จ

ฟิลด์ ประเภท คำอธิบาย
user_id string User ID
anonymous_ids array รายการ anonymous ID ทั้งหมดที่ผูกกับ User ID นี้ พร้อมประเภทการสนทนา
anonymous_ids[].anonymous_id string Anonymous ID
anonymous_ids[].conversation_type string ค่าประเภทการสนทนา (conversation type) ตามที่ระบุใน "Agent-Integration"
anonymous_ids[].source_id string Channel ID จากแพลตฟอร์มต้นทาง เช่น หากเชื่อมต่อกับ TELEGRAM และเพิ่ม TG Bot สองตัว แต่ละ Bot จะมี Source ID ของตัวเอง

Response ที่ผิดพลาด

ฟิลด์ ประเภท คำอธิบาย
code int รหัสข้อผิดพลาด
message string ข้อความข้อผิดพลาด

รหัสสถานะ (Status Codes)

Status Code คำอธิบาย
200 สำเร็จ
400 พารามิเตอร์ไม่ถูกต้อง
401 ไม่ได้รับอนุญาต
403 ถูกปฏิเสธ
500 ข้อผิดพลาดของเซิร์ฟเวอร์