client.js 7.76 KB
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const http_1 = require("./http");
const Types = require("./types");
const exceptions_1 = require("./exceptions");
function toArray(maybeArr) {
    return Array.isArray(maybeArr) ? maybeArr : [maybeArr];
}
function ensureJSON(raw) {
    if (typeof raw === "object") {
        return raw;
    }
    else {
        throw new exceptions_1.JSONParseError("Failed to parse response body as JSON", raw);
    }
}
const API_HOST = process.env.API_BASE_URL || "https://api.line.me/v2/";
const BOT_BASE_URL = process.env.API_BASE_URL || `${API_HOST}bot/`;
const OAUTH_BASE_URL = `${API_HOST}oauth/`;
class Client {
    constructor(config) {
        if (!config.channelAccessToken) {
            throw new Error("no channel access token");
        }
        this.config = config;
        this.http = new http_1.default({
            baseURL: BOT_BASE_URL,
            defaultHeaders: {
                Authorization: "Bearer " + this.config.channelAccessToken,
            },
            responseParser: this.parseHTTPResponse.bind(this),
        });
    }
    parseHTTPResponse(response) {
        const { LINE_REQUEST_ID_HTTP_HEADER_NAME } = Types;
        let resBody = Object.assign({}, response.data);
        if (response.headers[LINE_REQUEST_ID_HTTP_HEADER_NAME]) {
            resBody[LINE_REQUEST_ID_HTTP_HEADER_NAME] =
                response.headers[LINE_REQUEST_ID_HTTP_HEADER_NAME];
        }
        return resBody;
    }
    pushMessage(to, messages, notificationDisabled = false) {
        return this.http.post("/message/push", {
            messages: toArray(messages),
            to,
            notificationDisabled,
        });
    }
    replyMessage(replyToken, messages, notificationDisabled = false) {
        return this.http.post("/message/reply", {
            messages: toArray(messages),
            replyToken,
            notificationDisabled,
        });
    }
    async multicast(to, messages, notificationDisabled = false) {
        return this.http.post("/message/multicast", {
            messages: toArray(messages),
            to,
            notificationDisabled,
        });
    }
    async broadcast(messages, notificationDisabled = false) {
        return this.http.post("/message/broadcast", {
            messages: toArray(messages),
            notificationDisabled,
        });
    }
    async getProfile(userId) {
        const profile = await this.http.get(`/profile/${userId}`);
        return ensureJSON(profile);
    }
    async getChatMemberProfile(chatType, chatId, userId) {
        const profile = await this.http.get(`/${chatType}/${chatId}/member/${userId}`);
        return ensureJSON(profile);
    }
    async getGroupMemberProfile(groupId, userId) {
        return this.getChatMemberProfile("group", groupId, userId);
    }
    async getRoomMemberProfile(roomId, userId) {
        return this.getChatMemberProfile("room", roomId, userId);
    }
    async getChatMemberIds(chatType, chatId) {
        let memberIds = [];
        let start;
        do {
            const res = await this.http.get(`/${chatType}/${chatId}/members/ids`, start ? { start } : null);
            ensureJSON(res);
            memberIds = memberIds.concat(res.memberIds);
            start = res.next;
        } while (start);
        return memberIds;
    }
    async getGroupMemberIds(groupId) {
        return this.getChatMemberIds("group", groupId);
    }
    async getRoomMemberIds(roomId) {
        return this.getChatMemberIds("room", roomId);
    }
    async getMessageContent(messageId) {
        return this.http.getStream(`/message/${messageId}/content`);
    }
    leaveChat(chatType, chatId) {
        return this.http.post(`/${chatType}/${chatId}/leave`);
    }
    async leaveGroup(groupId) {
        return this.leaveChat("group", groupId);
    }
    async leaveRoom(roomId) {
        return this.leaveChat("room", roomId);
    }
    async getRichMenu(richMenuId) {
        const res = await this.http.get(`/richmenu/${richMenuId}`);
        return ensureJSON(res);
    }
    async createRichMenu(richMenu) {
        const res = await this.http.post("/richmenu", richMenu);
        return ensureJSON(res).richMenuId;
    }
    async deleteRichMenu(richMenuId) {
        return this.http.delete(`/richmenu/${richMenuId}`);
    }
    async getRichMenuIdOfUser(userId) {
        const res = await this.http.get(`/user/${userId}/richmenu`);
        return ensureJSON(res).richMenuId;
    }
    async linkRichMenuToUser(userId, richMenuId) {
        return this.http.post(`/user/${userId}/richmenu/${richMenuId}`);
    }
    async unlinkRichMenuFromUser(userId) {
        return this.http.delete(`/user/${userId}/richmenu`);
    }
    async linkRichMenuToMultipleUsers(richMenuId, userIds) {
        return this.http.post("/richmenu/bulk/link", {
            richMenuId,
            userIds,
        });
    }
    async unlinkRichMenusFromMultipleUsers(userIds) {
        return this.http.post("/richmenu/bulk/unlink", {
            userIds,
        });
    }
    async getRichMenuImage(richMenuId) {
        return this.http.getStream(`/richmenu/${richMenuId}/content`);
    }
    async setRichMenuImage(richMenuId, data, contentType) {
        return this.http.postBinary(`/richmenu/${richMenuId}/content`, data, contentType);
    }
    async getRichMenuList() {
        const res = await this.http.get(`/richmenu/list`);
        return ensureJSON(res).richmenus;
    }
    async setDefaultRichMenu(richMenuId) {
        return this.http.post(`/user/all/richmenu/${richMenuId}`);
    }
    async getDefaultRichMenuId() {
        const res = await this.http.get("/user/all/richmenu");
        return ensureJSON(res).richMenuId;
    }
    async deleteDefaultRichMenu() {
        return this.http.delete("/user/all/richmenu");
    }
    async getLinkToken(userId) {
        const res = await this.http.post(`/user/${userId}/linkToken`);
        return ensureJSON(res).linkToken;
    }
    async getNumberOfSentReplyMessages(date) {
        const res = await this.http.get(`/message/delivery/reply?date=${date}`);
        return ensureJSON(res);
    }
    async getNumberOfSentPushMessages(date) {
        const res = await this.http.get(`/message/delivery/push?date=${date}`);
        return ensureJSON(res);
    }
    async getNumberOfSentMulticastMessages(date) {
        const res = await this.http.get(`/message/delivery/multicast?date=${date}`);
        return ensureJSON(res);
    }
    async getTargetLimitForAdditionalMessages() {
        const res = await this.http.get("/message/quota");
        return ensureJSON(res);
    }
    async getNumberOfMessagesSentThisMonth() {
        const res = await this.http.get("/message/quota/consumption");
        return ensureJSON(res);
    }
    async getNumberOfSentBroadcastMessages(date) {
        const res = await this.http.get(`/message/delivery/broadcast?date=${date}`);
        return ensureJSON(res);
    }
    async getNumberOfMessageDeliveries(date) {
        const res = await this.http.get(`/insight/message/delivery?date=${date}`);
        return ensureJSON(res);
    }
    async getNumberOfFollowers(date) {
        const res = await this.http.get(`/insight/followers?date=${date}`);
        return ensureJSON(res);
    }
    async getFriendDemographics() {
        const res = await this.http.get(`/insight/demographic`);
        return ensureJSON(res);
    }
}
exports.default = Client;
class OAuth {
    constructor() {
        this.http = new http_1.default({
            baseURL: OAUTH_BASE_URL,
        });
    }
    issueAccessToken(client_id, client_secret) {
        return this.http.postForm("/accessToken", {
            grant_type: "client_credentials",
            client_id,
            client_secret,
        });
    }
    revokeAccessToken(access_token) {
        return this.http.postForm("/revoke", { access_token });
    }
}
exports.OAuth = OAuth;