Skip to content

Commit

Permalink
fix: Fix photo url error reading
Browse files Browse the repository at this point in the history
  • Loading branch information
adolphnov committed Aug 22, 2024
1 parent 27afa4a commit 5dab18f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
2 changes: 1 addition & 1 deletion dist/buildinfo.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"sha":"92ed0fc","timestamp":1724323685}
{"sha":"27afa4a","timestamp":1724326940}
20 changes: 14 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,10 @@ function sendMessageToTelegramWithContext(context) {
context._info,
msgType
);
if (!resp.ok) {
console.error(await resp.clone().text());
return resp;
}
return await checkIsNeedTagIds(context, msgType, resp);
};
}
Expand Down Expand Up @@ -694,20 +698,20 @@ async function deleteMessagesFromTelegram(chat_id, bot_token, message_ids) {
}
async function sendPhotoToTelegram(photo, token, context, _info = null) {
try {
let photo_url = photo.url[0];
const url = `${ENV2.TELEGRAM_API_DOMAIN}/bot${token}/sendPhoto`;
let body;
const headers = {};
if (typeof photo.url[0] === "string") {
if (typeof photo_url === "string") {
if (ENV2.TELEGRAPH_IMAGE_ENABLE) {
try {
const new_url = await uploadImageToTelegraph(photo.url[0]);
photo.url = new_url;
photo_url = await uploadImageToTelegraph(photo_url);
} catch (e2) {
console.error(e2.message);
}
}
body = {
photo: photo.url[0]
photo: photo_url
};
for (const key of Object.keys(context)) {
if (context[key] !== void 0 && context[key] !== null) {
Expand Down Expand Up @@ -749,6 +753,10 @@ function sendPhotoToTelegramWithContext(context) {
context.CURRENT_CHAT_CONTEXT,
context._info
);
if (!resp.ok) {
console.error(await resp.clone().text());
return resp;
}
return checkIsNeedTagIds(context, msgType, resp);
};
}
Expand Down Expand Up @@ -1161,9 +1169,9 @@ var Environment = class {
// -- 版本数据 --
//
// 当前版本
BUILD_TIMESTAMP = 1724323685;
BUILD_TIMESTAMP = 1724326940;
// 当前版本 commit id
BUILD_VERSION = "92ed0fc";
BUILD_VERSION = "27afa4a";
// -- 基础配置 --
/**
* @type {I18n | null}
Expand Down
2 changes: 1 addition & 1 deletion dist/timestamp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1724323685
1724326940
36 changes: 22 additions & 14 deletions src/telegram/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,18 @@ export async function sendMessageToTelegram(message, token, context, _info, type
*/
export function sendMessageToTelegramWithContext(context) {
return async (message, msgType = 'chat') => {
const resp = await sendMessageToTelegram(
message,
context.SHARE_CONTEXT.currentBotToken,
context.CURRENT_CHAT_CONTEXT,
context._info,
msgType,
);
return await checkIsNeedTagIds(context, msgType, resp);
const resp = await sendMessageToTelegram(
message,
context.SHARE_CONTEXT.currentBotToken,
context.CURRENT_CHAT_CONTEXT,
context._info,
msgType,
);
if (!resp.ok) {
console.error(await resp.clone().text());
return resp;
}
return await checkIsNeedTagIds(context, msgType, resp);
};
}

Expand Down Expand Up @@ -204,21 +208,22 @@ export async function deleteMessagesFromTelegram(chat_id, bot_token, message_id
*/
export async function sendPhotoToTelegram(photo, token, context, _info = null) {
try {
let photo_url = photo.url[0];
const url = `${ENV.TELEGRAM_API_DOMAIN}/bot${token}/sendPhoto`;
let body;
const headers = {};
if (typeof photo.url[0] === 'string') {
if (typeof photo_url === 'string') {
if (ENV.TELEGRAPH_IMAGE_ENABLE) {
try {
const new_url = await uploadImageToTelegraph(photo.url[0]);
photo.url = new_url;
photo_url = await uploadImageToTelegraph(photo_url);
} catch (e) {
console.error(e.message);
}
}
body = {
photo: photo.url[0],
photo: photo_url,
};

for (const key of Object.keys(context)) {
if (context[key] !== undefined && context[key] !== null) {
body[key] = context[key];
Expand All @@ -231,7 +236,6 @@ export async function sendPhotoToTelegram(photo, token, context, _info = null) {
info = (info ? info + '\n\n' : '') + photo.text;
}
body.caption = '>`' + escape(info) + '`' + `\n[原始图片](${photo.url})`;

body = JSON.stringify(body);
headers['Content-Type'] = 'application/json';
} else {
Expand All @@ -243,14 +247,14 @@ export async function sendPhotoToTelegram(photo, token, context, _info = null) {
}
}
}

return await fetch(url, {
method: 'POST',
headers,
body,
});
} catch (e) {
console.error(e);
// throw new Error('send telegram message failed, please see the log');
}
}

Expand All @@ -267,6 +271,10 @@ export function sendPhotoToTelegramWithContext(context) {
context.CURRENT_CHAT_CONTEXT,
context._info,
);
if (!resp.ok) {
console.error(await resp.clone().text());
return resp;
}
return checkIsNeedTagIds(context, msgType, resp);
};
}
Expand Down

0 comments on commit 5dab18f

Please sign in to comment.