Help Center การเชื่อมต่อ Meta และ Instagram API

การเชื่อมต่อ Meta และ Instagram API

เอกสารอ้างอิงสำหรับการสร้างแอป Instagram บน SMSBAT ChatHub Platform: การยืนยันตัวตน การสนทนา Instagram Direct, ความคิดเห็นในโพสต์และ Reels, การตอบกลับ Story, webhook และ polling

แหล่งที่มา

หน้านี้รวมข้อกำหนดภายในของ Meta Comments API เข้ากับคำนิยาม OpenAPI ที่ใช้งานจริงที่ https://chatapi.smsbat.com/swagger/v1/swagger.json และ https://restapi.smsbat.com/swagger/v1/swagger.json หากทั้งสองแหล่งไม่ตรงกัน ความแตกต่างจะถูกระบุไว้ในเนื้อหาและแสดงรายการไว้ในคำถามที่ยังไม่มีคำตอบ


1. Base URL

วัตถุประสงค์URL
Chat API + Meta APIhttps://chatapi.smsbat.com
Swagger UI / OpenAPIhttps://chatapi.smsbat.com/index.html · …/swagger/v1/swagger.json
REST API (องค์กร, callback URL)https://restapi.smsbat.com
REST API Swaggerhttps://restapi.smsbat.com/swagger/v1/swagger.json
แผงควบคุมผู้ปฏิบัติงานบนเว็บhttps://chat.smsbat.com

2. การยืนยันตัวตน

รูปแบบการยืนยันตัวตน ขึ้นอยู่กับกลุ่ม endpoint การใช้ปะปนกันเป็นสาเหตุที่พบบ่อยที่สุดของ 401

กลุ่มเฮดเดอร์
chatapi.smsbat.com/api/meta/* (posts, comments)X-Authorization-Key: <organization token>
chatapi.smsbat.com/api/chat/*, /api/company/*, /api/operator/*Authorization: Bearer <JWT>
restapi.smsbat.com/*X-Authorization-Key · Authorization: Bearer · Basic Auth

โทเคนองค์กรสำหรับ X-Authorization-Key ออกให้ในแผงควบคุมที่ โปรไฟล์ JWT ของบริษัทและผู้ปฏิบัติงานได้มาจาก /api/company/get-token และ /api/operator/get-token

ความไม่สอดคล้อง

เอกสาร OpenAPI ของ chatapi ประกาศรูปแบบความปลอดภัยเพียงรูปแบบเดียวคือ Bearer และใช้ กับทุก endpoint X-Authorization-Key ไม่ได้ถูกประกาศไว้เลย แม้ว่าข้อกำหนดภายในของ Meta Comments API จะระบุไว้สำหรับ /api/meta/* ก็ตาม เป็นไปได้มากว่าถูกจัดการโดย middleware ที่ไม่ได้สะท้อนอยู่ใน Swagger โปรดยืนยันด้วยการทดสอบจริงก่อนนำขึ้นใช้งานจริง

2.1 โทเคนบริษัท

POST https://chatapi.smsbat.com/api/company/get-token
Content-Type: application/json

{ "login": "company_login", "password": "company_password" }

200 OK ส่งคืนสตริงโทเคนเปล่า

2.2 องค์กร

GET https://chatapi.smsbat.com/api/company/organization
Authorization: Bearer <company_token>
[ { "id": 24, "name": "My Instagram Store" } ]

2.3 ผู้ปฏิบัติงานในองค์กร

GET https://chatapi.smsbat.com/api/operator?organizationId=24
Authorization: Bearer <company_token>
[
  {
    "id": 21,
    "name": "Jane Doe",
    "status": 0,
    "organization": { "id": 24, "name": "My Instagram Store" }
  }
]

สถานะผู้ปฏิบัติงาน: 0 ใช้งานอยู่, 1 ไม่ใช้งาน, 2 ถูกลบ

2.4 เพิ่ม / ซิงโครไนซ์ผู้ปฏิบัติงาน

POST https://chatapi.smsbat.com/api/operator/synchronize
Authorization: Bearer <company_token>
Content-Type: application/json

[ { "organizationId": 24, "name": "John Operator" } ]

200 OK → [ { "id": 21, "status": 0, "name": "John Operator" } ]

2.5 JWT ของผู้ปฏิบัติงาน

POST https://chatapi.smsbat.com/api/operator/get-token
Authorization: Bearer <company_token>
Content-Type: application/json

{ "id": 21, "expiresAt": "2026-12-31T23:59:59.000Z" }

200 OK ส่งคืน JWT เป็นสตริง

2.6 ตรวจสอบโทเคนของผู้ปฏิบัติงาน

POST https://chatapi.smsbat.com/api/operator/validate-token
Authorization: Bearer <company_token>
Content-Type: application/json

"eyJhbGciOi..."
{
  "isValid": true,
  "operatorId": 21,
  "clientId": 0,
  "expiresAt": "2026-12-31T23:59:59.000Z",
  "error": null
}

เมื่อไม่ถูกต้อง: { "isValid": false, "error": "Invalid token" }

2.7 ฝังแผงแชทของผู้ปฏิบัติงาน

<script type="module" id="operator-chat-panel-script"
  src="https://widget.smsbat.com/operator-chat-panel/widget-script.js"
  token="YOUR_OPERATOR_JWT_TOKEN"></script>

ระบบภายนอก (CRM, ERP, เว็บไซต์) สามารถเปิดการสนทนาที่ระบุใน https://chat.smsbat.com/ ได้ ผู้ปฏิบัติงานได้รับการอนุญาตผ่าน JWT ที่ส่งเป็นพารามิเตอร์ query

https://chat.smsbat.com/?chat_raw_id=<chat_id>&token=<jwt>
https://chat.smsbat.com/?phone=<phone>&token=<jwt>
https://chat.smsbat.com/?from=<bm_id>&phone=<phone>&token=<jwt>
https://chat.smsbat.com/?source=7&from=<bm_id>&phone=<phone>&token=<jwt>
พารามิเตอร์คำอธิบาย
chat_raw_idID ของแชท
phoneหมายเลขโทรศัพท์ในรูปแบบสากล
fromตัวระบุแบรนด์ / บัญชีธุรกิจ (bm_id)
sourceแหล่งที่มาของแชท — 7 สำหรับ Instagram ดู §8.1
tokenJWT ของผู้ปฏิบัติงานที่ถูกต้อง ยังไม่หมดอายุ และมีสิทธิ์เข้าถึงแชท

JWT ที่ไม่ถูกต้องจะพาผู้เยี่ยมชมไปยังหน้าจอเข้าสู่ระบบของแผงควบคุมผู้ปฏิบัติงาน


4. การสนทนา Instagram Direct

4.1 แสดงรายการแชท

GET https://chatapi.smsbat.com/api/chat/chats?source=7&page=1&per_page=20
Authorization: Bearer <token>

Note

การแบ่งหน้าที่นี่ใช้ per_page (snake_case) ส่วนภายใต้ /api/meta/* และ endpoint สำหรับ polling จะใช้ perPage (camelCase) นี่ไม่ใช่การพิมพ์ผิด — API ใช้ทั้งสองแบบ

พารามิเตอร์ query ทั้งหมดเป็นตัวเลือก:

พารามิเตอร์ประเภทคำอธิบาย
sourceChatSource7 จำกัดผลลัพธ์เฉพาะ Instagram
entityIdintID บัญชีธุรกิจ ใช้ได้เฉพาะเมื่อระบุร่วมกับ source
instagram_user_idintID ผู้ใช้ Instagram ใน ChatHub
facebook_user_idintID ผู้ใช้ Facebook ใน ChatHub
page / per_pageintการแบ่งหน้า ค่าเริ่มต้น 1 / 20
statusChatStatus[]สถานะแชท ระบุซ้ำได้
searchstringค้นหาข้อความอิสระ (ชื่อ, เบอร์โทร, …)
organizationIdintID องค์กร
operatorIdint[]กรองตามผู้ปฏิบัติงานที่ได้รับมอบหมาย
datestring[]สองค่าขอบเขต: ?date=…&date=…
isChainboolส่งคืนแชทเป็นสายโซ่ โดยรวมข้อความจากแชทก่อนหน้า
isUnread, starMark, isOperator, isAIAgentboolตัวกรองเพิ่มเติม
phone, email, contactId, clientId, tagIds, rate, sortedBy—ตัวกรองอื่นๆ

200 OK ส่งคืน GetChatsResponse:

{
  "total": 1,
  "newMessagesCount": 2,
  "items": [
    {
      "id": 1867,
      "theme": null,
      "messSource": 7,
      "chatStatus": 1,
      "countUnread": 2,
      "metaUserId": "1585775752382460",
      "instaAccount": { "id": 12, "name": "my_instagram_shop", "photo": "https://..." },
      "instagramUser": { "id": 123, "name": "marianna_cat", "photo": "https://..." },
      "operator": { "id": 21, "name": "Jane", "photo": "https://..." },
      "client": { "id": 55, "name": "Marianna", "photo": null },
      "textLastMess": "Hello! Is this product available?",
      "timeLastMess": "2026-08-13T10:15:00Z",
      "authorLastMessage": 1,
      "messageStatus": 6,
      "isMedia": false,
      "phone": null,
      "organizationId": 1,
      "createdAt": "2026-08-13T10:14:00Z",
      "isStarred": false,
      "isBlocked": false,
      "tags": []
    }
  ]
}

ฟิลด์ที่สำคัญสำหรับแอป Instagram:

ฟิลด์ความหมาย
instaAccountบัญชีธุรกิจ Instagram (ร้านค้า) id คือค่าของตัวกรอง entityId; name คือชื่อบัญชีจาก Meta
instagramUserลูกค้า name คือแฮนเดิล Instagram, id คือค่าของตัวกรอง instagram_user_id
metaUserIdID แบบ scoped ของลูกค้าฝั่ง Meta (สตริง)
messSource7 สำหรับ Instagram
phoneโดยปกติเป็น null สำหรับ Instagram — อย่าใช้เป็นคีย์

ChatDTO ยังมี olxUser, promUser, waba, rozetkaUser, facebookAccount, facebookUser, viberAccount, tgBot, tgUser, viberBot, viberBotUser, widget, media, isConnectedAI, isPromo, rate, rateComment, closedBy, draft, lang, starMark, contactId, contactName, block, isInStopList, stopList, isSmsFallbackEnabled และ taggedMessages ด้วย

4.2 ข้อความในแชท

GET https://chatapi.smsbat.com/api/chat/chats/1867/messages?isChain=false
Authorization: Bearer <token>

200 OK ส่งคืนอาร์เรย์ของ ChatMessageDTO:

[
  {
    "id": 9928,
    "chatId": 1867,
    "message": "Hi! Do you have size M in stock?",
    "messageTranslation": null,
    "phone": null,
    "author": 1,
    "source": 7,
    "media": null,
    "replyTo": null,
    "status": 6,
    "date": "2026-08-13T10:14:00Z",
    "operator": null,
    "client": { "id": 123, "name": "marianna_cat", "photo": "https://..." },
    "messageType": 0,
    "isInternal": false,
    "isBroadcast": false,
    "starMark": false,
    "referralGuid": null,
    "postId": null,
    "isConnectedAI": false,
    "organizationId": 1,
    "countUnreadMessages": 0
  }
]

postId จะมีค่าเมื่อข้อความเกี่ยวข้องกับโพสต์หรือ Story บน Instagram — ส่งค่านี้ กลับไปเป็น id / postId ใน Meta API ได้โดยตรง media คือ ChatMediaDTO: { name, format, type, uri, raw, length, isUploaded }.

4.3 ส่งข้อความ (JSON)

POST https://chatapi.smsbat.com/api/chat/1867/message
Authorization: Bearer <token>
Content-Type: application/json

เนื้อหาคำขอ — SendChatMessageDTO:

{
  "textMessage": "Hello! Yes, size M is available.",
  "author": 0,
  "isInternal": false,
  "replyToMessageId": 9928,
  "appGuid": "550e8400-e29b-41d4-a716-446655440000",
  "media": {
    "name": "item.jpg",
    "format": "image/jpeg",
    "dataBase64": "/9j/4AAQSkZJRg...",
    "type": 1
  }
}
ฟิลด์ประเภทคำอธิบาย
textMessagestring?ข้อความ อาจว่างได้เมื่อมี media
authorAuthorMessage?0 ผู้ปฏิบัติงาน, 1 ลูกค้า
isInternalbool?true หมายถึงบันทึกภายในที่ไม่ถูกส่งถึงลูกค้า
replyToMessageIdint?ID ของข้อความที่ตอบกลับ
appGuiduuid?GUID อ้างอิง
mediaMediaDTO?{ name, format, dataBase64, thumbnail, duration, type }

200 OK → { "id": 9930, "messageStatus": 0 }

สามารถส่ง GUID อ้างอิงในพาธได้เช่นกัน: POST /api/chat/{chatId}/{referralGuid}/message (likewise …/message/v1, …/message/v2).

4.4 ส่งไฟล์หรือวิดีโอ (multipart, v2)

POST https://chatapi.smsbat.com/api/chat/1867/message/v2
Authorization: Bearer <token>
Content-Type: multipart/form-data

ชื่อฟิลด์ฟอร์มเป็น PascalCase และใช้เครื่องหมายจุด

textMessage และ media.file จะถูกละเว้นโดยไม่มีการแจ้งเตือน ใช้ชื่อที่ถูกต้องตามด้านล่างนี้

ฟิลด์ฟอร์มประเภทคำอธิบาย
TextMessagestringข้อความ
Authorint0 ผู้ปฏิบัติงาน, 1 ลูกค้า
IsInternalboolบันทึกภายใน
ReplyToMessageIdintข้อความที่ตอบกลับ
AppGuiduuidGUID อ้างอิง
Media.Filebinaryตัวไฟล์
Media.Namestringชื่อไฟล์
Media.Formatstringประเภท MIME (video/mp4, image/png, application/pdf)
Media.TypeMediaTypeดู §8.5
Media.DataBase64stringทางเลือกแทน Media.File
Media.Thumbnailstringเฟรมตัวอย่างวิดีโอแบบ Base64
Media.Durationdoubleความยาววิดีโอเป็นวินาที
curl -X POST "https://chatapi.smsbat.com/api/chat/1867/message/v2" \
  -H "Authorization: Bearer <token>" \
  -F "TextMessage=Here is the price list" \
  -F "Author=0" \
  -F "Media.Type=2" \
  -F "Media.File=@./price.pdf"

200 OK → { "id": 9931, "messageStatus": 0 }

4.5 เปลี่ยนสถานะแชท

PUT https://chatapi.smsbat.com/api/chat/status
Authorization: Bearer <token>
Content-Type: application/json

{ "id": 1867, "status": 4 }

200 OK ส่งคืนอ็อบเจ็กต์ที่อัปเดตแล้วกลับมา

4.6 อัปเดตสถานะข้อความ

PUT https://chatapi.smsbat.com/api/chat/messages/status
Authorization: Bearer <token>
Content-Type: application/json

{ "status": 3, "messageIds": [9928, 9929] }

4.7 ลบแชท

DELETE https://chatapi.smsbat.com/api/chat/chats/1867
Authorization: Bearer <token>

5. โพสต์ Reels และ Stories

พาธฐาน: https://chatapi.smsbat.com/api/meta การยืนยันตัวตน: X-Authorization-Key: <organization token>

5.1 แสดงรายการโพสต์ Reels และ Stories

GET https://chatapi.smsbat.com/api/meta/posts?platform=instagram&mediaType=story&page=1&perPage=20
X-Authorization-Key: <token>
พารามิเตอร์ประเภทจำเป็นคำอธิบาย
pageintไม่หน้า ค่าเริ่มต้น 1
perPageintไม่จำนวนรายการต่อหน้า ค่าเริ่มต้น 20
idintไม่กรองตาม ID โพสต์ภายใน
platformstringไม่instagram หรือ facebook
mediaTypestringไม่post, reel หรือ story หากไม่ระบุจะรวมทุกประเภท
# Every post
curl -H "X-Authorization-Key: <token>" \
  "https://chatapi.smsbat.com/api/meta/posts?page=1&perPage=10"

# Instagram only
curl -H "X-Authorization-Key: <token>" \
  "https://chatapi.smsbat.com/api/meta/posts?platform=instagram"

# A single post by ID
curl -H "X-Authorization-Key: <token>" \
  "https://chatapi.smsbat.com/api/meta/posts?id=42"

# Instagram Stories only
curl -H "X-Authorization-Key: <token>" \
  "https://chatapi.smsbat.com/api/meta/posts?platform=instagram&mediaType=story"

200 OK:

{
  "total": 42,
  "items": [
    {
      "id": 42,
      "metaId": "18113450675314072",
      "text": "Check out our new collection!",
      "imageUrl": "https://dashboard.smsbat.com/api/meta/post/media/7d124c36-1711-4aa8-b21d-f6dbf8f347ef",
      "platform": "instagram",
      "mediaType": "story",
      "createdAt": "2026-07-22T09:35:30Z",
      "story": {
        "id": 42,
        "metaId": "18113450675314072",
        "url": "https://dashboard.smsbat.com/api/meta/post/media/7d124c36-1711-4aa8-b21d-f6dbf8f347ef"
      }
    }
  ]
}

ความไม่สอดคล้อง — ชื่อฟิลด์ตัวนับ

สคีมา Swagger MetaCommentPostListItemDtoPaginationDTO กำหนด total ส่วน ข้อกำหนดภายในระบุ totalCount Swagger สร้างจากโค้ด ดังนั้น total น่าจะเป็นค่าที่ถูกต้องมากกว่า ให้แยกวิเคราะห์ total ?? totalCount จนกว่าจะได้ข้อสรุป

ฟิลด์คำอธิบาย
idID โพสต์ภายใน
metaIdID โพสต์ / Reel / Story ภายนอกใน Meta
textคำบรรยายภาพของโพสต์
imageUrlURL สื่อแบบพร็อกซีที่อ้างอิงด้วย MetaPost.Guid ที่ไม่เรียงลำดับ หรือ null
platformfacebook หรือ instagram
mediaTypepost, reel หรือ story
createdAtวันที่สร้าง (วันที่ของแพลตฟอร์ม หรือวันที่ในฐานข้อมูล)
storyมีอยู่ เฉพาะ เมื่อ mediaType: "story"
story.idID Story ภายใน; เท่ากับ post.id
story.metaIdID Story ภายนอกใน Meta
story.urlURL พร็อกซีที่คงที่ของสื่อ Story ที่จัดเก็บไว้; เป็น null หากบันทึกสื่อไม่สำเร็จ

สื่อของโพสต์ให้บริการผ่านสองเส้นทาง: GET /api/meta/post/media/{id:int} เพื่อความเข้ากันได้ ย้อนหลัง และ GET /api/meta/post/media/{guid:guid} การตอบกลับและ callback ของ API ใหม่ จะสร้างในรูปแบบ GUID เสมอ

5.2 แสดงรายการความคิดเห็น

GET https://chatapi.smsbat.com/api/meta/comments?platform=instagram&postId=42&page=1&perPage=20
X-Authorization-Key: <token>
พารามิเตอร์ประเภทจำเป็นคำอธิบาย
pageintไม่หน้า ค่าเริ่มต้น 1
perPageintไม่จำนวนรายการต่อหน้า ค่าเริ่มต้น 20
postIdintไม่กรองตาม ID โพสต์
parentCommentIdintไม่ความคิดเห็นย่อย (การตอบกลับ) ของความคิดเห็นที่ระบุ
platformstringไม่facebook หรือ instagram

200 OK:

{
  "total": 100,
  "items": [
    {
      "id": 5,
      "metaId": "179000000000005",
      "text": "What is the price?",
      "createdAt": "2026-04-15T10:30:00Z",
      "platform": "instagram",
      "replyStatus": null,
      "author": {
        "type": "meta_user",
        "name": "John Doe",
        "metaUserId": "1585775752382460"
      },
      "post": {
        "id": 1,
        "metaId": "18113450675314072",
        "text": null,
        "imageUrl": "https://dashboard.smsbat.com/api/meta/post/media/7d124c36-…",
        "createdAt": "2026-07-22T09:35:30Z",
        "mediaType": "story",
        "story": {
          "id": 1,
          "metaId": "18113450675314072",
          "url": "https://dashboard.smsbat.com/api/meta/post/media/7d124c36-…"
        }
      },
      "mediaUrl": "https://chatapi.smsbat.com/api/meta/comment/media/5",
      "replyTo": {
        "id": 3,
        "metaId": "179000000000003",
        "text": "Parent comment..."
      }
    }
  ]
}
ฟิลด์คำอธิบาย
idID ความคิดเห็นภายใน
metaIdID ภายนอกใน Meta เป็น null สำหรับการตอบกลับของเราที่รอดำเนินการจนกว่าจะส่ง
textข้อความความคิดเห็น
createdAtวันที่สร้าง
platformfacebook หรือ instagram
replyStatusnull สำหรับความคิดเห็นขาเข้าของผู้ใช้; "pending" / "sent" / "failure" สำหรับการตอบกลับของเรา
author.type"meta_user" ผู้ใช้ภายนอก, "owner" เจ้าของเพจ
author.nameชื่อผู้เขียน
author.metaUserIdID ผู้ใช้แบบ scoped ใน Meta; เป็น null สำหรับ "owner"
postโพสต์ Reel หรือ Story ที่ความคิดเห็นนั้นอยู่
post.mediaTypepost, reel หรือ story
post.storyการอ้างอิง Story { id, metaId, url } เฉพาะ Stories เท่านั้น
mediaUrlสื่อที่แนบกับความคิดเห็น หรือ null
replyToความคิดเห็นหลัก { id, metaId, text }; เป็น null ในระดับบนสุด

ความไม่สอดคล้อง — ประเภทของ `author.type`

ข้อกำหนดภายในระบุสตริง "meta_user" / "owner" ส่วน Swagger กำหนดประเภทของ MetaCommentAuthorType เป็น จำนวนเต็ม พร้อม enum [0, 1] การใช้ JsonStringEnumConverter อาจอธิบายความต่างนี้ได้ แต่ยังไม่ได้ยืนยันกับการตอบกลับจริง ให้เขียน parser ที่รองรับทั้งสองแบบ

5.3 ตอบกลับความคิดเห็น

จัดคิวการตอบกลับเพื่อส่ง

curl -X POST \
  -H "X-Authorization-Key: <token>" \
  -H "Content-Type: application/json" \
  -d '{"text": "Thanks for the feedback!"}' \
  "https://chatapi.smsbat.com/api/meta/comments/5/reply"

เนื้อหาคำขอ: { "text": "Reply text" }

202 Accepted ส่งคืนอ็อบเจ็กต์ความคิดเห็น — รูปแบบเดียวกับ GET /api/meta/comments — พร้อม replyStatus: "pending" และ metaId: null ผลการส่งจะมาถึงภายหลังเป็น callback source: 13 (§6.4)

ความไม่สอดคล้อง — รหัสตอบกลับ

Swagger ประกาศ 200 โดยไม่มี body ส่วนข้อกำหนดภายในประกาศ 202 Accepted พร้อมความคิดเห็นเป็น body เป็นไปได้มากว่า controller ขาดแอตทริบิวต์ ProducesResponseType ทำให้ Swagger ใช้ค่าเริ่มต้น ให้ยอมรับ 2xx ใดๆ และอย่าพึ่งพา body


6. Webhook

SMSBAT ส่งคำขอ POST แบบ application/json ไปยัง URL ของคุณ และคาดหวังให้ตอบกลับ HTTP 200

ฟิลด์ที่เป็น null จะถูกละไว้ทั้งหมด

ฟิลด์ที่มีค่าเป็น null จะไม่ถูกซีเรียไลซ์ลงใน body ของ callback เลย สำหรับ ข้อความที่ไม่ได้มาจาก Facebook หรือ Instagram จะไม่มีคีย์ MetaUserId เลย ให้ถือว่า “ไม่มี” และ null เป็นสิ่งเดียวกัน

6.1 ลงทะเบียน callback URL

curl -X POST 'https://restapi.smsbat.com/organizations/callback_urls' \
  -H 'X-Authorization-Key: <token>' \
  -H 'Content-Type: application/json' \
  -d '{
    "url": "https://your-server.com/webhook",
    "source": 12,
    "headerName": "X-Webhook-Secret",
    "headerValue": "your-secret",
    "channelType": 7,
    "channelEntityId": 12
  }'
ฟิลด์ประเภทคำอธิบาย
urlstringendpoint ของคุณ
sourceSendingSourceCallbackประเภทเหตุการณ์ ดู §8.2
headerName / headerValuestringเฮดเดอร์สำหรับยืนยันตัวตนตามที่คุณกำหนดซึ่งเราจะแนบไปกับคำขอ (ไม่บังคับ)
channelTypeChatSourceช่องทาง 7 สำหรับ Instagram ไม่บังคับ
channelEntityIdintบัญชีธุรกิจที่ระบุ ต้องใช้ร่วมกับ channelType

หากไม่ระบุ channelType URL จะได้รับเหตุการณ์จากทุกช่องทาง

Tip

การครอบคลุมความคิดเห็นอย่างครบถ้วนต้องลงทะเบียน สอง รายการ: source: 12 สำหรับความคิดเห็นใหม่ และ source: 13 สำหรับสถานะการตอบกลับ สำหรับการตอบกลับใน Direct และ Story ให้เพิ่ม source: 3 (and 11 if you want every chat message).

การดำเนินการที่เหลือ:

GET    https://restapi.smsbat.com/organizations/callback_urls?source=12
GET    https://restapi.smsbat.com/organizations/callback_urls/{id}
PUT    https://restapi.smsbat.com/organizations/callback_urls/{id}
DELETE https://restapi.smsbat.com/organizations/callback_urls/{id}

GET ส่งคืน:

[
  {
    "id": 101,
    "url": "https://your-server.com/webhook",
    "source": 12,
    "headerName": "X-Webhook-Secret",
    "headerValue": "your-secret",
    "createdAt": "2026-08-13T09:00:00Z",
    "channelType": 7,
    "channelEntityId": 12
  }
]

6.2 ข้อความใหม่และการตอบกลับ Instagram Story (source: 3, 11)

การตอบกลับของผู้ใช้ต่อ Instagram Story จะมาถึงเป็นข้อความปกติใน callback เหล่านี้ พร้อมบล็อก Story เพิ่มเติมในระดับบนสุด:

{
  "ChatId": 123,
  "MessageId": 456,
  "MessageText": "😍",
  "Username": "Jane Smith",
  "UserId": 789,
  "MetaUserId": "1585775752382460",
  "ShopId": 12,
  "ShopName": "instagram shop name",
  "Author": 0,
  "type_messenger": 7,
  "Story": {
    "Id": 42,
    "MetaId": "18113450675314072",
    "Url": "https://dashboard.smsbat.com/api/meta/post/media/7d124c36-1711-4aa8-b21d-f6dbf8f347ef"
  }
}
ฟิลด์คำอธิบาย
ChatId / MessageIdตัวระบุแชทและข้อความ
Author0 ผู้ใช้, 1 ผู้ปฏิบัติงาน
Usernameชื่อที่แสดงหรือแฮนเดิลของ Instagram / Facebook
UserIdID ผู้ใช้ตัวเลขภายในของ SMSBAT
MetaUserIdID แบบ scoped ของคู่สนทนาใน Meta สำหรับข้อความ ขาออก ของผู้ปฏิบัติงาน ค่านี้ยังคงระบุผู้ใช้ Meta ของแชท ไม่ใช่ผู้ปฏิบัติงาน
ShopIdID ภายในของบัญชีธุรกิจ Instagram / Facebook
ShopNameชื่อบัญชีธุรกิจตามที่ได้รับจาก Meta ณ เวลาที่เชื่อมต่อ
MessageTextข้อความ
MessageMediaURL ของสื่อเมื่อข้อความเป็นสื่อ
type_messengerแหล่งที่มา 7 สำหรับ Instagram
operator_nameชื่อผู้ปฏิบัติงานเมื่อ Author = 1
Storyมี เฉพาะ ในการตอบกลับ Story ขาเข้า
Story.IdID Story ภายใน (MetaPost) — ใช้เป็น id / postId ใน Meta API ได้โดยตรง
Story.MetaIdID Story ภายนอกใน Meta
Story.UrlURL พร็อกซีที่คงที่ของสื่อ Story ที่จัดเก็บไว้ จะไม่มีเมื่อบันทึกสื่อไม่สำเร็จ — บล็อก Story และข้อความยังคงถูกส่งตามปกติ

`Author` กลับค่าตรงข้ามกับ Chat API

ใน ChatMessageDTO.author ค่า 0 หมายถึงผู้ปฏิบัติงานและ 1 หมายถึงลูกค้า ใน callback นี้ เป็นตรงกันข้าม: 0 คือผู้ใช้ และ 1 คือผู้ปฏิบัติงาน อย่าใช้การแมปร่วมกัน

6.3 ความคิดเห็นใหม่ (source: 12)

ทำงานเมื่อผู้ใช้ Meta แสดงความคิดเห็นในโพสต์ Facebook หรือโพสต์ / Reel บน Instagram

Note

การตอบกลับ Story บน Instagram จะไม่ถูกส่งผ่าน source: 12 แต่จะมาถึงเป็นข้อความ ขาเข้าปกติใน source: 3 และ/หรือ 11 พร้อมบล็อก Story — ดู §6.2

{
  "type": "new_comment",
  "platform": "instagram",
  "comment": {
    "id": 10,
    "metaId": "179000000000010",
    "parentCommentId": 5,
    "parentMetaId": "179000000000005",
    "parentCommentText": "The user's previous comment",
    "text": "Great product!",
    "createdAt": "2026-04-16T12:00:00Z",
    "author": {
      "type": "meta_user",
      "name": "Jane Smith",
      "metaUserId": "1585775752382460"
    }
  },
  "post": {
    "id": 1,
    "metaId": "123456789012345",
    "text": "Post description...",
    "imageUrl": "https://dashboard.smsbat.com/api/meta/post/media/7d124c36-…",
    "createdAt": "2026-04-10T12:00:00Z",
    "mediaType": "post"
  }
}

6.4 สถานะการตอบกลับความคิดเห็น (source: 13)

ทำงานหลังจากเราพยายามส่งการตอบกลับ ไม่ว่าจะสำเร็จหรือล้มเหลว

{
  "type": "comment_status",
  "platform": "instagram",
  "comment": {
    "id": 15,
    "metaId": "179000000000015",
    "parentCommentId": 10,
    "parentMetaId": "179000000000010",
    "parentCommentText": "Great product!",
    "text": "Thanks for the feedback!",
    "createdAt": "2026-04-16T12:05:00Z",
    "updatedAt": "2026-04-16T12:05:03Z",
    "replyStatus": "sent",
    "author": {
      "type": "owner",
      "name": "Support Agent"
    }
  },
  "post": {
    "id": 1,
    "metaId": "179999999999999",
    "text": "Reel description...",
    "imageUrl": "https://dashboard.smsbat.com/api/meta/post/media/7d124c36-…",
    "createdAt": "2026-07-22T09:35:30Z",
    "mediaType": "reel"
  }
}

6.5 ฟิลด์ callback ความคิดเห็นที่ใช้ร่วมกัน

callback ความคิดเห็นทั้งสองแบบใช้ รูปแบบ body เดียวกัน และต่างกันที่ type เท่านั้น

ฟิลด์คำอธิบาย
type"new_comment" หรือ "comment_status"
platform"facebook" หรือ "instagram"
comment.idID ความคิดเห็นภายใน
comment.metaIdID ภายนอกใน Meta; เป็น null สำหรับการตอบกลับที่รอดำเนินการก่อนส่ง
comment.parentCommentIdID ความคิดเห็นหลัก ไม่มี สำหรับความคิดเห็นระดับบนสุด
comment.parentMetaIdID ภายนอกของความคิดเห็นหลัก ไม่มีในระดับบนสุด
comment.parentCommentTextข้อความของความคิดเห็นหลัก ไม่มีในระดับบนสุด
comment.textข้อความความคิดเห็น
comment.createdAtวันที่สร้าง
comment.updatedAtการอัปเดตล่าสุด ไม่มีหากความคิดเห็นไม่เคยถูกแก้ไข
comment.replyStatus"pending" / "sent" / "failure" ไม่มี สำหรับความคิดเห็นขาเข้าของผู้ใช้
comment.author.type"meta_user" หรือ "owner"
comment.author.nameชื่อผู้เขียน
comment.author.metaUserIdID ผู้เขียนแบบ scoped ใน Meta ไม่มีสำหรับ "owner"
comment.mediaUrlสื่อของความคิดเห็น ไม่มีเมื่อไม่มีสื่อ
post.idID โพสต์ภายใน
post.metaIdID โพสต์ / Reel / Story ภายนอกใน Meta
post.textข้อความโพสต์
post.imageUrlURL รูปภาพของโพสต์ หรือ null
post.createdAtวันที่สร้างโพสต์
post.mediaTypeใน callback ความคิดเห็น มีเพียง post หรือ reel

6.6 แชทใหม่ (source: 7)

{ "ChatId": 12345, "Phone": "+380501234567", "chat.Source": 7 }

6.7 การเปลี่ยนแปลงสถานะข้อความและแชท (source: 6 / 5)

{ "name": "changed_message_status", "id": 9928, "status": 6 }
{ "name": "changed_chat_status",    "id": 1867, "status": 4 }

6.8 ข้อความถูกแก้ไขหรือลบ (source: 9)

{ "messageId": 9930, "text": "Updated message text" }
{ "messageId": 9931, "delete": true }

6.9 ตัวบ่งชี้การพิมพ์ (source: 8)

{ "chat_id": 1867, "event": "start_typing", "operator_name": "Iryna" }
{ "chat_id": 1867, "event": "end_typing" }

7. การ polling เหตุการณ์

สำหรับสภาพแวดล้อมที่ไม่สามารถรับ HTTP ขาเข้าได้

7.1 ดึงเหตุการณ์

GET https://chatapi.smsbat.com/api/chat/callback-events?page=1&perPage=20
Authorization: Bearer <token>
พารามิเตอร์คำอธิบาย
organizationIdไม่บังคับ ใช้ค่าจากโทเคนเมื่อไม่ระบุ
page / perPageการแบ่งหน้า ค่าเริ่มต้น 1 / 20
{
  "page": 1,
  "perPage": 20,
  "total": 947,
  "items": [
    {
      "ChatId": 1867,
      "MessageId": 9928,
      "MessageText": "Hello there!",
      "MessageMedia": null,
      "Phone": null,
      "Username": "marianna_cat",
      "UserId": 123,
      "ShopId": 12,
      "ShopName": "instagram shop name",
      "Author": 1,
      "type_messenger": 7,
      "message_date": "2026-08-13T12:27:39.724Z",
      "timestamp": "2026-08-13T12:27:39.736Z",
      "event_guid": "ff60129e-c6e4-4876-9d90-badb430c0606",
      "organization_id": 1,
      "callback_type": "3"
    }
  ]
}

ทุกเหตุการณ์มี event_guid, timestamp, organization_id และ callback_type — เป็น สตริงที่ตรงกับค่า source ใน §8.2 ฟิลด์ที่เหลือตรงกับ webhook ที่สอดคล้องกันใน §6

7.2 ยืนยันเหตุการณ์ที่ประมวลผลแล้ว

POST https://chatapi.smsbat.com/api/chat/callback-events/processed
Authorization: Bearer <token>
Content-Type: application/json

[ "ff60129e-c6e4-4876-9d90-badb430c0606" ]

200 OK → { "deleted": 1 }

เหตุการณ์ที่ถูกลบไปแล้วจะไม่ถูกนับรวมใน deleted ลำดับและการลองใหม่เป็นความรับผิดชอบของ ฝั่งคุณ

7.3 ลูปที่แนะนำ

  1. polling GET /api/chat/callback-events ตามกำหนดเวลา
  2. ประมวลผลเหตุการณ์ในบริการของคุณ
  3. ส่งรายการ event_guid ที่ประมวลผลแล้วไปที่ /callback-events/processed
  4. ทำซ้ำ

8. เอกสารอ้างอิง Enum

8.1 ChatSource — ช่องทาง (0–9)

รหัสช่องทาง
0Viber
1ViberBot
2TelegramBot
3Whatsapp
4Widget
5Rozetka
6Facebook
7Instagram
8Prom
9Olx

8.2 SendingSourceCallback — ประเภทเหตุการณ์ callback (0–13)

รหัสเหตุการณ์
3Chat — ข้อความแชทใหม่ รวมถึงการตอบกลับ Story
5สถานะแชทเปลี่ยนแปลง
6สถานะข้อความเปลี่ยนแปลง
7สร้างแชทใหม่
8ตัวบ่งชี้การพิมพ์
9ข้อความถูกอัปเดตหรือลบ
11AnyChatMessage — ข้อความแชทใดๆ
12MetaNewComment — ความคิดเห็นใหม่บน Instagram / Facebook
13MetaCommentStatus — สถานะการส่งของการตอบกลับความคิดเห็นของเรา

enum ครอบคลุม 0–13 ส่วนค่าที่เหลือไม่จำเป็นสำหรับการเชื่อมต่อ Instagram

8.3 ChatStatus (0–4)

0 New, 1 Open, 2 Waiting, 3 OnPause, 4 Closed

8.4 MessageStatus (0–11)

รหัสชื่อ
0NEW
1SUCCESS
2REJECTED
3READ
4UNKNOWN
5PROCESSING
6DELIVERED
7BLOCKED_BY_USER
8USER_NOT_FOUND

enum ครอบคลุม 0–11 ค่า 9, 10 และ 11 มีอยู่ใน API แต่ยังไม่มีเอกสารประกอบ — ให้ถือว่าเป็น UNKNOWN

8.5 MediaType (1–10)

1 Photo, 2 File, 3 Audio, 4 Video, 5 Sticker, 6 StickerAnimated, 7 StickerVideo, 8 Animation, 9 Voice, 10 VideoNote

8.6 AuthorMessage — ผู้เขียนใน Chat API (0–4)

0 Operator, 1 Client, 2 Bot, 3 ViberAccount

enum ครอบคลุม 0–4 ค่า 4 ยังไม่มีเอกสารประกอบ callback “ข้อความใหม่” ใช้ การแมปแบบตรงกันข้าม — ดู §6.2

8.7 ChatMessageType (0–2)

0 Text, 1 Photo, 2 File

8.8 replyStatus ของความคิดเห็น

null ความคิดเห็นขาเข้าของผู้ใช้, "pending" การตอบกลับของเราอยู่ในคิว, "sent" ส่งสำเร็จ, "failure" ส่งล้มเหลว


คำถามที่ยังไม่มีคำตอบ

สามประเด็นที่ข้อกำหนดภายในกับ Swagger ที่สร้างจากโค้ดไม่ตรงกัน คำขอ หนึ่งครั้งด้วยโทเคนจริงจะยุติทั้งหมดได้ จนกว่าจะถึงตอนนั้น ให้เขียนไคลเอนต์แบบป้องกันไว้ก่อน

#คำถามข้อกำหนดSwaggerวิธีตรวจสอบ
1เฮดเดอร์ยืนยันตัวตนสำหรับ /api/meta/*X-Authorization-Keyประกาศเฉพาะ Bearercurl -i -H "X-Authorization-Key: <token>" ".../api/meta/posts?perPage=1" — คาดว่าจะได้ 200 ไม่ใช่ 401
2ฟิลด์ตัวนับในการตอบกลับของ MetatotalCounttotalคำขอเดียวกัน — อ่านคีย์ JSON ระดับราก
3ประเภทของ author.type และรหัสสถานะของ reply"meta_user" / "owner", 202 พร้อม bodyint [0,1], 200 ไม่มี bodycurl -i .../api/meta/comments?perPage=1 พร้อมการตอบกลับทดสอบ

แนวทางชั่วคราว:

  • ตัวนับ — อ่าน total ?? totalCount;
  • author.type — รองรับทั้งสตริงและจำนวนเต็ม (0 ↔ meta_user, 1 ↔ owner การแมปต้องยืนยันอีกครั้ง);
  • reply — ถือว่า 2xx ใดๆ เป็นความสำเร็จ ไม่ต้องการ body และใช้สถานะสุดท้ายจาก callback source: 13

หมายเหตุการติดตั้งใช้งาน

  • การยืนยันตัวตนแตกต่างกันในแต่ละกลุ่ม endpoint — /api/meta/* ใช้ X-Authorization-Key, แชทและ ผู้ปฏิบัติงานใช้ Bearer, restapi รับได้ทั้งสองแบบ
  • การแบ่งหน้าสะกดสองแบบ — per_page ที่ /api/chat/chats, perPage ที่ /api/meta/* และ /api/chat/callback-events
  • ฟิลด์ multipart/form-data เป็น PascalCase พร้อมเครื่องหมายจุด (Media.File, Media.Type)
  • ฟิลด์ที่เป็น null จะถูกละไว้จาก callback — คีย์ที่ไม่มีหมายถึง null
  • phone มักเป็น null บน Instagram ให้ระบุลูกค้าด้วย instagramUser.id / metaUserId และร้านค้าด้วย instaAccount.id (ค่าของตัวกรอง entityId)
  • Story.Id จาก callback ส่งกลับเป็น id / postId ใน Meta API ได้โดยตรง
  • ตรวจสอบ expiresAt ของ JWT ผู้ปฏิบัติงานก่อนใช้ใน deeplink หรือวิดเจ็ต