โหมด Webhook

โหมด Webhook

ขณะนี้ GPTBots Agent รองรับโหมดการตอบกลับข้อความ 3 แบบ ได้แก่ blocking, streaming และ webhook เมื่อนักพัฒนาเลือกใช้โหมด webhook เพื่อรับข้อความตอบกลับ ทั้ง AI responses และ human responses จะถูกส่งไปยัง URL webhook ที่กำหนดไว้

โหมดการตอบกลับ ประเภทข้อความที่รองรับ
blocking AI responses
streaming AI responses
webhook Human responses, AI responses

API ส่งข้อความจะส่งข้อความตอบกลับไปยัง webhook

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

POST

Endpoint

โปรดกำหนดURL สำหรับรับข้อความของคุณในหน้า Agent - Integration - API - webhook

การตรวจสอบสิทธิ์

รองรับการตรวจสอบสิทธิ์ 2 วิธี ได้แก่ Basic auth และ Bearer auth นักพัฒนาสามารถเลือกวิธีที่เหมาะสมและกำหนดค่าในหน้า Agent - Integration - API - webhook ได้

  • เมื่อเชื่อมต่อ URL webhook หากนักพัฒนากรอกเฉพาะ webhook username GPTBots จะใช้การตรวจสอบสิทธิ์แบบ Bearer เป็นค่าเริ่มต้นเมื่อส่งคำขอไปยัง URL webhook ของนักพัฒนา โดยมีค่าเป็น webhook username
  • เมื่อเชื่อมต่อ URL webhook หากนักพัฒนากรอกทั้ง webhook username และ webhook secret GPTBots จะใช้การตรวจสอบสิทธิ์แบบ Basic โดยใช้ webhook username เป็นชื่อผู้ใช้ และ webhook secret เป็นรหัสผ่าน

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

curl -X POST 'YOUR_API_URL' \ -H 'Authorization: Bearer ${API Key}' \ -H 'Content-Type: application/json' \ -d '{ "create_time": 1679587005, "user_id": "65a4ccfc7ce58e728d5897e0", "anonymous_id": "device_abcdef123456", "conversation_id": "657303a8a764d47094874bbe", "message_id": "65a4ccfC7ce58e728d5897e0", "output": [ { "from_component_branch": "1", "from_component_name": "Component Name", "content": { "text": "Hi, is there anything I can help you?", "audio": [ { "audio": "http://gptbots.ai/example.mp3", "transcript": "The transcribed content of the audio" } ] } } ], "usage": { "tokens": { "total_tokens": 29, "prompt_tokens": 19, "prompt_tokens_details": { "audio_tokens": 0, "text_tokens":0 }, "completion_tokens": 10, "completion_tokens_details": { "reasoning_tokens": 0, "audio_tokens": 0, "text_tokens": 0 } }, "credits": { "total_credits":0.0, //prompt + completion "text_input_credits": 0.0, "text_output_credits": 0.0, "audio_input_credits": 0.0, "audio_output_credits": 0.0 } } }'
                      
                      curl -X POST 'YOUR_API_URL' \
-H 'Authorization: Bearer ${API Key}' \
-H 'Content-Type: application/json' \
-d '{
    "create_time": 1679587005,
    "user_id": "65a4ccfc7ce58e728d5897e0",
    "anonymous_id": "device_abcdef123456",
    "conversation_id": "657303a8a764d47094874bbe",
    "message_id": "65a4ccfC7ce58e728d5897e0",
    "output": [
        {
            "from_component_branch": "1",
            "from_component_name": "Component Name",
            "content": {
                "text": "Hi, is there anything I can help you?",
                "audio": [
                    {
                        "audio": "http://gptbots.ai/example.mp3",
                        "transcript": "The transcribed content of the audio"
                    }
                ]
            }
        }
    ],
    "usage": {
        "tokens": {
           "total_tokens": 29,
            "prompt_tokens": 19,
            "prompt_tokens_details": 
                {  
                    "audio_tokens": 0,
                    "text_tokens":0
                },
            "completion_tokens": 10,
            "completion_tokens_details": 
                {
                    "reasoning_tokens": 0,
                    "audio_tokens": 0,
                    "text_tokens": 0
                }
        },
        "credits": {
            "total_credits":0.0,  //prompt + completion
            "text_input_credits": 0.0,
            "text_output_credits": 0.0,
            "audio_input_credits": 0.0,
            "audio_output_credits": 0.0
        }
    }
}'

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

Request Body

ฟิลด์ ประเภท คำอธิบาย
user_id string รหัสผู้ใช้ที่นักพัฒนาผูกไว้ จะเป็น null เมื่อไม่มีผู้ใช้ผูกอยู่
anonymous_id string รหัสผู้ใช้ที่ไม่ระบุตัวตน (อุปกรณ์)
conversation_id string รหัสเฉพาะของการสนทนา
message_id string รหัสเฉพาะของข้อความแต่ละรายการในบทสนทนา
create_time long เวลาที่สร้างข้อความ (timestamp)
output JSON Array เนื้อหาการตอบกลับของ AI Agent
from_component_branch string สาขาของ FlowAgent
from_component_name string ชื่อคอมโพเนนต์ต้นทางใน FlowAgent
content object เนื้อหาข้อความตอบกลับของ AI Agent รองรับทั้ง text และ audio
usage object ข้อมูลการใช้งานและการบริโภคทรัพยากร
tokens JSON Array จำนวนโทเคนที่ Agent ใช้ในบทสนทนานี้ทั้งหมด
total_tokens integer จำนวนโทเคนที่ใช้สำหรับ input + output ในบทสนทนานี้ทั้งหมด
prompt_tokens integer จำนวนโทเคนที่ใช้สำหรับ input ในบทสนทนานี้ทั้งหมด
completion_tokens integer จำนวนโทเคนที่ใช้สำหรับ output ในบทสนทนานี้ทั้งหมด
prompt_tokens_details object รายละเอียดโทเคนที่ใช้สำหรับ input
completion_tokens_details object รายละเอียดโทเคนที่ใช้สำหรับ output
credits object เครดิตที่ Agent ใช้ในบทสนทนานี้ทั้งหมด
text_input_credits double เครดิตที่ใช้สำหรับข้อความ input
text_output__credits double เครดิตที่ใช้สำหรับข้อความ output
audio_input_credits double เครดิตที่ใช้สำหรับข้อความเสียง input
audio_output_credits double เครดิตที่ใช้สำหรับข้อความเสียง output

ข้อกำหนดการตอบกลับ

เมื่อบริการ webhook ของนักพัฒนาได้รับข้อความสำเร็จ จะต้องคืน HTTP status code 200 และในส่วน response body ต้องคืนตัวระบุความสำเร็จในรูปแบบ JSON

{ "code": 200, "msg": "success" }
                      
                      {
  "code": 200,
  "msg": "success"
}

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

การตรวจสอบสถานะบริการ Webhook ของ GPTBots

GPTBots มีอินเทอร์เฟซตรวจสอบสุขภาพ (health-check) สำหรับตรวจสอบบริการ webhook ของตนเอง นักพัฒนาสามารถเรียกใช้อินเทอร์เฟซนี้เพื่อตรวจสอบว่าบริการ webhook ของ GPTBots พร้อมใช้งานหรือไม่ หากอินเทอร์เฟซคืน HTTP status code 200 และ response body เป็นตัวระบุข้อความล้วน (plain text) ที่บอกว่าบริการปกติ (เช่น service is normal) แสดงว่าบริการ webhook ของ GPTBots พร้อมใช้งาน อินเทอร์เฟซนี้ไม่ต้องตรวจสอบสิทธิ์และคืนค่าเป็นข้อความล้วน

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

GET

Endpoint

https://api.gptbots.ai/v1/webhook/service/health

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

curl -X GET 'https://api.gptbots.ai/v1/webhook/service/health'
                      
                      curl -X GET 'https://api.gptbots.ai/v1/webhook/service/health'

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

การตอบกลับ

เมื่อบริการ GPTBots พร้อมใช้งาน จะคืน HTTP status code 200 และ response body เป็นตัวระบุข้อความล้วนที่บอกว่าบริการปกติ

service is normal
                      
                      service is normal

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

คำแนะนำในการประเมินความพร้อมใช้งาน

ผู้เรียกใช้เพียงใช้ HTTP status code เป็นเกณฑ์การตัดสินเท่านั้น:

สถานการณ์ การตัดสิน
คืน 200 และ response body เป็น service is normal บริการ GPTBots พร้อมใช้งาน
คืนค่าที่ไม่ใช่ 200 (เช่น 5xx) / คำขอหมดเวลา / เชื่อมต่อไม่สำเร็จ บริการ GPTBots ไม่พร้อมใช้งาน

แนะนำให้ตั้งค่า timeout ของคำขออย่างเหมาะสม (เช่น 3-5 วินาที) และตรวจสอบด้วยความถี่คงที่ (QPM:3) อินเทอร์เฟซนี้ไม่ต้องตรวจสอบสิทธิ์ จึงไม่ต้องแนบ API Key