@racla-dev/node-iris
Version:
TypeScript port of Python irispy-client module for KakaoTalk bot development
88 lines • 3.22 kB
JavaScript
"use strict";
/**
* ChatImage class for handling image messages
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChatImage = void 0;
const logger_1 = require("../../utils/logger");
const utils_1 = require("./utils");
class ChatImage {
constructor(message) {
this.logger = new logger_1.Logger('ChatImage');
this.url = this.getPhotoUrl(message);
}
async getImg() {
if (this._img !== undefined) {
return this._img;
}
if (!this.url || this.url.length === 0) {
this._img = null;
return null;
}
try {
const axios = require('axios');
const images = [];
for (const url of this.url) {
const response = await axios.get(url, {
responseType: 'arraybuffer',
headers: { 'User-Agent': 'Mozilla/5.0 (compatible; node-iris)' },
});
images.push(Buffer.from(response.data));
}
this._img = images;
return this._img;
}
catch (error) {
this.logger.error('Failed to load chat images:', error);
this._img = null;
return null;
}
}
getPhotoUrl(message) {
try {
const urls = [];
if (message.type === 71) {
// 최신 멀티포토 메시지 (71번)
if ((0, utils_1.isNewMultiPhotoAttachment)(message.attachment)) {
const thl = message.attachment.C?.THL;
if (Array.isArray(thl)) {
for (const item of thl) {
if (item.TH?.THU) {
urls.push(item.TH.THU);
}
}
}
}
}
else if (message.type === 27) {
// 레거시 멀티포토 메시지 (27번)
if ((0, utils_1.isMultiPhotoAttachment)(message.attachment)) {
const imageUrls = message.attachment.imageUrls;
if (Array.isArray(imageUrls)) {
urls.push(...imageUrls);
}
}
}
else if (message.type === 2) {
// 레거시 단일 포토 메시지 (2번)
if ((0, utils_1.isPhotoAttachment)(message.attachment)) {
// imageUrl 속성이 있는 경우 (레거시 형식)
if ('imageUrl' in message.attachment && message.attachment.imageUrl) {
urls.push(message.attachment.imageUrl);
}
// url 속성이 있는 경우 (기본 형식)
else if ('url' in message.attachment && message.attachment.url) {
urls.push(message.attachment.url);
}
}
}
return urls;
}
catch (error) {
this.logger.error('Failed to parse image URLs:', error);
return [];
}
}
}
exports.ChatImage = ChatImage;
//# sourceMappingURL=chat-image.js.map