Skip to content

Commit

Permalink
feat: 添加图片质量参数TELEGRAM_PHOTO_SIZE_OFFSET
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Aug 1, 2024
1 parent f986aa6 commit 71b1b0a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/config/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ class Environment {
DEFAULT_PARSE_MODE = 'Markdown';
// 最小stream模式消息间隔,小于等于0则不限制
TELEGRAM_MIN_STREAM_INTERVAL = 0;
// 图片尺寸偏移 0为第一位,-1为最后一位, 越靠后的图片越大
TELEGRAM_PHOTO_SIZE_OFFSET = -2;


// -- 权限相关 --
//
Expand Down
9 changes: 8 additions & 1 deletion src/telegram/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,14 @@ async function msgChatWithLLM(message, context) {
*/
const params = { message: content };
if (message.photo && message.photo.length > 0) {
const fileId = message.photo[message.photo.length - 1].file_id;
let sizeIndex = 0;
if (ENV.TELEGRAM_PHOTO_SIZE_OFFSET >= 0) {
sizeIndex = ENV.TELEGRAM_PHOTO_SIZE_OFFSET;
} else if (ENV.TELEGRAM_PHOTO_SIZE_OFFSET < 0) {
sizeIndex = message.photo.length + ENV.TELEGRAM_PHOTO_SIZE_OFFSET;
}
sizeIndex = Math.max(0, Math.min(sizeIndex, message.photo.length - 1));
const fileId = message.photo[sizeIndex].file_id;
const url = await getFileLink(fileId, context.SHARE_CONTEXT.currentBotToken);
params.images = [url];
}
Expand Down
9 changes: 9 additions & 0 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,12 @@ export async function makeResponse200(resp) {
}
}

/**
* @param url
* @returns {Promise<string>}
*/
export async function urlToBase64String(url) {
return fetch(url)
.then(resp => resp.arrayBuffer())
.then(buffer => Buffer.from(buffer).toString('base64'));
}

0 comments on commit 71b1b0a

Please sign in to comment.