Skip to main content

การรับ Callback จากระบบ Live Chat

ภาพรวม

Mbox ซึ่งเป็นระบบ live chat จะยิง webhook กลับมาแจ้งความเคลื่อนไหวที่เกิดขึ้นฝั่งเจ้าหน้าที่ ไม่ว่าจะเป็นการตอบข้อความ การสร้าง conversation ใหม่ การปิดบทสนทนา หรือการแก้ไขข้อมูลลูกค้า งานนี้คือ consumer ที่ประมวลผลเหตุการณ์เหล่านั้นเพื่อ ซิงก์สถานะกลับเข้าระบบ LINE Management

ผลลัพธ์หลักมี 3 อย่าง คือ การต่ออายุ session ของเจ้าหน้าที่, การปิดโหมดเจ้าหน้าที่เมื่อ conversation ถูก resolve และการซิงก์ custom attribute ของลูกค้าที่เจ้าหน้าที่แก้ไขใน Mbox กลับมาที่ตาราง line_user

Business Flow

Handler จะแตกการทำงานตามฟิลด์ event ของ payload ซึ่งรับมาเป็น map แบบไม่กำหนดโครงสร้างล่วงหน้า

eventการทำงาน
message_createdสนใจเฉพาะข้อความประเภท outgoing ซึ่งหมายถึงเจ้าหน้าที่เป็นผู้ตอบ จากนั้นต่ออายุ session โดยอัปเดต score ใน agent_sessions, เก็บ conversation ID ลง hash, assign team ในการ callback ครั้งแรก และรีเซ็ต warningSent กลับเป็น 0
conversation_createdผูก conversation ID เข้ากับคีย์ agent_mode และ resolve ว่า inbox นี้เป็นของ OA ใด
conversation_status_changedหากสถานะเปลี่ยนเป็น resolved จะปิดโหมดเจ้าหน้าที่ ด้วยการลบคีย์ agent_mode ถอนออกจาก agent_sessions และส่งข้อความปิดการสนทนาให้ผู้ใช้ทาง LINE
contact_updatedดึง custom attribute ที่เจ้าหน้าที่แก้ไขจาก Mbox มา merge ลง line_user.custom_attribute โดยเติม prefix ให้ พร้อมทั้งสร้างหรืออัปเดต schema ใน attribute_master ให้อัตโนมัติจากการเดาชนิดข้อมูล
conversation_updatedอัปเดตข้อมูลระดับบทสนทนา เช่น custom attribute ของ conversation ในลักษณะเดียวกัน
อื่น ๆไม่ดำเนินการใด แล้ว ack ข้อความทิ้ง

รายละเอียดเพิ่มเติมที่ควรทราบ

  • resolveLineOaID แปลง inbox ของ Mbox กลับเป็น lineOaId โดยอาศัย Redis คีย์ mbox_inbox:<inboxId> ซึ่งถูก warm ไว้ตั้งแต่ตอนประมวลผล webhook
  • inferDataType และ parseableDate ใช้เดาชนิดข้อมูลของ attribute ที่เพิ่งพบใหม่ ว่าเป็น string, number, date หรือ boolean เพื่อสร้าง schema ที่ถูกต้องใน attribute_master
  • หาก decode payload ไม่สำเร็จ ระบบจะจัดเป็นข้อผิดพลาดถาวรและส่งข้อความเข้า DLQ

ไฟล์และฟังก์ชันหลัก

ไฟล์หน้าที่
internal/mbox/service_callback.goMboxCallbackService.ProcessCallback เป็นตัว dispatch ตาม event โดยมี handler ย่อย handleMessageCreated, handleConversationCreated, handleConversationStatusChanged, handleContactUpdated, handleConversationUpdated และฟังก์ชันสนับสนุน assignTeam, ensureMboxAttributeMaster, resolveLineOaID, sendLineMessage, parseSchema, inferDataType, parseableDate, equalsNumber
internal/mbox/consumer.goConsumer.HandleMboxCallback
internal/mboxx/mboxx.goREST client ของ Mbox

คิวที่เกี่ยวข้อง: mbox_callback บน profile cron-scheduler

จุดเชื่อมต่อกับ Service อื่น

  • รับงานจาก endpoint ที่รับ webhook ของ Mbox ทางฝั่ง webhook-go หรือ client-api ซึ่งจะ publish ต่อเข้าคิว
  • Redisagent_mode:<lineOaId>:<userId>, sorted set agent_sessions และ mbox_inbox:<inboxId>
  • ตารางฐานข้อมูลline_user (merge custom_attribute), attribute_master (schema ของ attribute) และ line_oa (token สำหรับ push ข้อความ)
  • Mbox REST API — ใช้ assign team ให้ conversation
  • LINE APIPOST /v2/bot/message/push สำหรับส่งข้อความแจ้งจบการสนทนา
  • ทำงานคู่กับ การโอนแชทให้เจ้าหน้าที่