JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 13
  • Score
    100M100P100Q34224F
  • License ISC

Unofficial TypeScript client for LINE OA Web/Inbox (chat.line.biz)

Package Exports

  • @prakrit_m/lineweb
  • @prakrit_m/lineweb/type

Readme

@prakrit_m/lineweb

Unofficial TypeScript client for LINE OA Web/Inbox (chat.line.biz).

  • Supports custom axios instance/config injection
  • Strongly-typed responses and helpers
  • Convenient pagination helpers for list endpoints

Installation

  • npm: npm i @prakrit_m/lineweb

Quick Start

import { LineWeb, isLineWebError } from "@prakrit_m/lineweb";

// 1) เตรียม cookies เป็น JSON string (จาก browser) รูปแบบ array ของ cookie objects
// หมายเหตุ: ไลบรารีจะกรองเฉพาะโดเมนที่เกี่ยวกับ line.biz ให้เอง
const cookies = "[ {\"name\":\"RSESSION\",\"value\":\"...\",\"domain\":\"account.line.biz\"}, ... ]";

const lw = new LineWeb({ cookies });

try {
  const me = await lw.getMe();
  console.log(me.name);
} catch (err) {
  if (isLineWebError(err)) {
    console.error(err.code, err.message);
  } else {
    console.error(err);
  }
}

การหา webBotId และ webChatId

  • webBotId (33 ตัวอักษร): ดูได้จาก URL เมื่อเปิดห้องแชทใน Inbox
  • webChatId (33 ตัวอักษร): ส่วนท้ายของ URL ตามด้านบน

การตั้งค่า axios คุณสามารถปรับ axios ได้ทั้งแบบส่ง instance เองหรือส่ง config ให้ภายในสร้างให้

import axios from "axios";
import { LineWeb } from "@prakrit_m/lineweb";

const cookies = "[...]";

// ส่ง instance เอง
const ax = axios.create({ baseURL: "https://chat.line.biz", timeout: 8000 });
const lw1 = new LineWeb({ cookies, axios: ax });

// ส่ง config ให้ไลบรารีสร้าง instance ให้
const lw2 = new LineWeb({ cookies, axiosConfig: { timeout: 10000 } });

// เปลี่ยน/อัปเดตหลังสร้างแล้ว
lw1.updateAxiosConfig({ headers: { "X-Debug": "1" } });
lw1.setAxiosInstance(axios.create({ timeout: 5000 }));

หมายเหตุ: ไลบรารีจะใส่ header ที่จำเป็นต่อทุกคำขอให้เองเสมอ เช่น User-Agent, x-oa-chat-client-version, และ Cookie

การใช้งาน API หลัก (LineWeb) ด้านล่างคือเมธอดสาธารณะหลักๆ พร้อมคำอธิบายพารามิเตอร์สำคัญ

คำอธิบายพารามิเตอร์ที่ใช้ร่วมกันบ่อย

  • webChatId: ความยาว 33 ตัวอักษร หาได้จาก URL (ดูด้านบน)
  • webBotId: ความยาว 33 ตัวอักษร หาได้จาก URL (ดูด้านบน)
  • limitPerPage: จำนวนข้อมูลต่อหน้า (take). ถ้าเป็น 0 คือให้ใช้ค่าสูงสุดที่ระบบอนุญาตสำหรับ endpoint นั้นๆ
  • maxPages: จำนวนหน้าสูงสุดที่จะวนดึงมาให้ (ตั้ง 0 เพื่อไม่จำกัด, ค่าปกติในไลบรารีหลายจุดคือ 1)
  • nextToken: โทเคนหน้าถัดไป จาก LINE API กรณีมีหลายหน้า
  • backwardToken: โทเคนย้อนกลับ จาก LINE API บางตัวกรณีมีหลายหน้า
  • webUserIds: อาเรย์ของ user id (แต่ละ id ความยาว 33)
  • bizIds: อาเรย์ของ UUID
  • tagIds: อาเรย์ของ string tag id
  • messageId: สตริงตัวเลข
  • timestamp: สตริงตัวเลข

เมธอด

  • getMe(): Promise

    • ข้อมูลผู้ใช้ปัจจุบัน
  • getBots({ limitPerPage = 1000, maxPages = 1, nextToken } | { webBotId }): Promise<BotsList | Bot>

    • ไม่ส่ง webBotId จะดึงรายการบอททั้งหมดแบบแบ่งหน้า
    • ส่ง webBotId จะดึงข้อมูลเฉพาะบอทนั้น (ห้ามส่ง nextToken คู่กัน)
  • getOwners({ webBotId, bizIds? }): Promise

    • ดึงรายชื่อ owner ของบอท อาจกรองด้วย bizIds
  • getTags({ webBotId, tagIds? }): Promise

    • ดึงรายการแท็ก อาจกรองด้วย tagIds
  • getChats({ webBotId, limitPerPage = 25, maxPages = 1, nextToken? }): Promise

    • ดึงรายการห้องแชทแบบแบ่งหน้า รวมหน้าถึง maxPages
  • getMessages({ webBotId, webChatId, limitPerPage = 25, maxPages = 1, backwardToken? }): Promise

    • ดึงข้อความของห้องแชทแบบแบ่งหน้า และไลบรารีจะรวมให้ตาม maxPages
  • getContactByName({ webBotId, chatName, limitPerPage = 20, maxPages = 1, nextToken?, filterKey?, sortKey?, sortOrder? }): Promise

    • ค้นหา contact ตามชื่อ (มีตัวเลือก filter/sort)
  • getChatMembers({ webBotId, webChatId, limitPerPage = 100, webUserIds?, nextToken?, maxPages = 1 }): Promise

    • รายชื่อสมาชิกในห้อง
  • getFlexMessageContent({ webBotId, webChatId, messageId, timestamp? }): Promise

    • ดึงเนื้อหา Flex Message ในรูปแบบ JSON (Flex)
  • logout(): Promise

    • ออกจากระบบ (เรียก endpoint logoutUri และตามด้วย redirect)

ตัวอย่างเรียกใช้งานบางส่วน

import { LineWeb } from "@prakrit_m/lineweb";

const lw = new LineWeb({ cookies: "[...]" });

// ดึงบอททั้งหมด (ไม่ส่ง webBotId)
const bots = await lw.getBots({ limitPerPage: 500, maxPages: 2 });

// ดึงห้องแชทของบอท
const chats = await lw.getChats({ webBotId: "<33-char-bot-id>", limitPerPage: 50, maxPages: 3 });

// ดึงข้อความของห้องแชท
const messages = await lw.getMessages({
  webBotId: "<33-char-bot-id>",
  webChatId: "<33-char-chat-id>",
  limitPerPage: 50,
  maxPages: 2,
});

// ค้นหา contact ตามชื่อ
const contacts = await lw.getContactByName({ webBotId: "<33-char-bot-id>", chatName: "john" });

การใช้ Type เพียงอย่างเดียว หากต้องการ import เฉพาะ type สามารถใช้ subpath @prakrit_m/lineweb/type

import type { Me, BotsList, Chat, MessageEventType } from "@prakrit_m/lineweb/type";

หรือจาก root (ต้องใช้ TypeScript 5.0+):

import type { Me } from "@prakrit_m/lineweb";

การจัดการ Error

import { LineWebError, isLineWebError } from "@prakrit_m/lineweb";

try {
  // ...
} catch (err) {
  if (isLineWebError(err)) {
    // err.code: INVALID_COOKIE | EXPIRED_COOKIE | INVALID_PARAMETER | AXIOS_ERROR | LOGOUT_FAILURE | NOT_FOUND | UNKNOWN_ERROR
    console.error(err.code, err.status, err.message);
  } else {
    console.error(err);
  }
}

Tips

  • ค่าพารามิเตอร์หลายตัวมีการตรวจสอบ type และรูปแบบ เช่น webChatId, webBotId ต้องยาว 33 ตัวอักษรเป็น a-z, A-Z, 0-9 เท่านั้น
  • limitPerPage บาง endpoint มีขีดจำกัดสูงสุดต่างกัน (เช่น bots สูงสุด 1000, chat/messages ปกติ 1–100/25 ฯลฯ)
  • ถ้า cookies หมดอายุจะได้ LineWebError ด้วยรหัส EXPIRED_COOKIE
  • คุณสามารถใช้ axios-mock-adapter เพื่อทดสอบโดยไม่ส่งคำขอจริง

หากต้องการตัวอย่างเพิ่มเติม แจ้งได้เลยครับ