this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: Enhance media posting logic to support fetching images from source URL if not cached

+28 -6
+28 -6
bot/telegrambot/helpers/mediaHelper.js
··· 144 144 if (fs.existsSync(mediaData.imageUrl)) { 145 145 await this.bot.sendPhoto( 146 146 config.channelId, 147 - fs.createReadStream(mediaData.imageUrl), // Use file stream for local files 147 + fs.createReadStream(mediaData.imageUrl), 148 148 { 149 149 caption: caption, 150 150 reply_markup: inlineKeyboard 151 151 } 152 152 ); 153 153 return true; 154 - } else { 155 - // Try to post from URL if not in cache 154 + } else if (/^https?:\/\//i.test(mediaData.imageUrl)) { 155 + // Try to post from imageUrl if it is a valid HTTP(S) URL 156 156 try { 157 157 await this.bot.sendPhoto( 158 158 config.channelId, ··· 165 165 return true; 166 166 } catch (imageError) { 167 167 console.error('Error posting image:', imageError); 168 - 169 168 // Attempt to download and reupload if direct linking fails 170 169 try { 171 170 const imageResponse = await axios({ ··· 173 172 url: mediaData.imageUrl, 174 173 responseType: 'stream' 175 174 }); 176 - 177 175 await this.bot.sendPhoto( 178 176 config.channelId, 179 177 imageResponse.data, ··· 182 180 reply_markup: inlineKeyboard 183 181 } 184 182 ); 185 - 186 183 return true; 187 184 } catch (secondError) { 188 185 console.error('Error uploading image after download:', secondError); 189 186 return false; 190 187 } 191 188 } 189 + } else if (mediaData.sourceUrl && /^https?:\/\//i.test(mediaData.sourceUrl)) { 190 + // If cache is missing, try to fetch from sourceUrl 191 + try { 192 + const imageResponse = await axios({ 193 + method: 'GET', 194 + url: mediaData.sourceUrl, 195 + responseType: 'stream' 196 + }); 197 + await this.bot.sendPhoto( 198 + config.channelId, 199 + imageResponse.data, 200 + { 201 + caption: caption, 202 + reply_markup: inlineKeyboard 203 + } 204 + ); 205 + return true; 206 + } catch (sourceError) { 207 + console.error('Error fetching and sending image from sourceUrl:', sourceError); 208 + return false; 209 + } 210 + } else { 211 + // Not a local file and not a valid URL 212 + console.error(`Image file does not exist and is not a valid URL: ${mediaData.imageUrl}`); 213 + return false; 192 214 } 193 215 } catch (error) { 194 216 console.error('Error posting media:', error);