|
1 | 1 | const { htmlToMarkdown } = require('./markdown') |
2 | 2 | const HTMLParser = require('node-html-parser') |
3 | 3 | const { markdownv2 } = require('telegram-format') |
4 | | -const { getNoteContents } = require('./onenote') |
| 4 | +const { getNoteContents, extractFirstImage, downloadImage } = require('./onenote') |
5 | 5 |
|
6 | 6 | const fetch = require('superagent') |
7 | 7 |
|
@@ -146,33 +146,69 @@ async function withTelegram(note, messageSettings, login = false) { |
146 | 146 | } = messageSettings |
147 | 147 | const msg = await structureMessage({ note, titlePrefix, isLogin: login }) |
148 | 148 |
|
149 | | - let text = markdownv2.bold(markdownv2.escape(msg.prefix)) |
150 | | - text += markdownv2.escape(`\n\n`) |
151 | | - text += markdownv2.underline(markdownv2.escape(msg.title)) |
152 | | - text += markdownv2.escape(`\n\n`) |
153 | | - text += msg.body |
154 | | - |
155 | | - if(showEditLink) { |
156 | | - // const openInOnenote = markdownv2.url("Open In OneNote", markdownv2.escape(msg.url)) |
157 | | - text += markdownv2.escape(`\n\n`) |
158 | | - text += markdownv2.url("Edit note", msg.webUrl) |
159 | | - } |
160 | | - |
161 | 149 | return new Promise(async(resolve, reject) => { |
162 | 150 | try { |
| 151 | + // Handle image processing independently |
| 152 | + let hasImage = false |
| 153 | + try { |
| 154 | + const noteContent = await getNoteContents(note.url) |
| 155 | + const imageInfo = extractFirstImage(noteContent.content) |
| 156 | + |
| 157 | + if (imageInfo?.imageUrl) { |
| 158 | + console.log('Found image in note, sending photo first') |
| 159 | + |
| 160 | + // Create caption with subject and alt text |
| 161 | + let caption = markdownv2.bold(markdownv2.escape(msg.prefix)) |
| 162 | + caption += markdownv2.escape(`\n\n`) |
| 163 | + caption += markdownv2.underline(markdownv2.escape(msg.title)) |
| 164 | + caption += markdownv2.escape(`\n\n`) |
| 165 | + |
| 166 | + // Use alt text if available, otherwise use preview text |
| 167 | + const captionText = imageInfo.altText || msg.previewText |
| 168 | + caption += markdownv2.escape(captionText) |
| 169 | + |
| 170 | + if(showEditLink) { |
| 171 | + caption += markdownv2.escape(`\n\n`) |
| 172 | + caption += markdownv2.url("Edit note", msg.webUrl) |
| 173 | + } |
| 174 | + |
| 175 | + // Download and send image with size limits |
| 176 | + const imageBuffer = await downloadImage(imageInfo.dataFullresSrc || imageInfo.imageUrl) |
| 177 | + await sendPhotoToTelegram(imageBuffer, caption, channelHandle) |
| 178 | + hasImage = true |
| 179 | + } |
| 180 | + } catch (imageErr) { |
| 181 | + console.warn('Image processing failed, continuing with text message:', imageErr.message) |
| 182 | + // Image processing failure doesn't affect text message flow |
| 183 | + } |
| 184 | + |
| 185 | + // Send note body text |
| 186 | + let text = markdownv2.bold(markdownv2.escape(msg.prefix)) |
| 187 | + text += markdownv2.escape(`\n\n`) |
| 188 | + text += markdownv2.underline(markdownv2.escape(msg.title)) |
| 189 | + text += markdownv2.escape(`\n\n`) |
| 190 | + text += msg.body |
| 191 | + |
| 192 | + if(showEditLink) { |
| 193 | + text += markdownv2.escape(`\n\n`) |
| 194 | + text += markdownv2.url("Edit note", msg.webUrl) |
| 195 | + } |
| 196 | + |
163 | 197 | await sendNoteToTelegram(text, channelHandle, { disable_web_page_preview: disablePreview }); |
| 198 | + |
164 | 199 | resolve({ |
165 | 200 | status: 200, |
166 | 201 | title: msg.title, |
167 | 202 | body: msg.previewText, |
| 203 | + hasImage |
168 | 204 | }) |
169 | 205 | } catch (err) { |
170 | 206 | console.error('Title: ', msg.title) |
171 | | - console.error('withTelegram', err.response.body.description) |
| 207 | + console.error('withTelegram', err.response?.body?.description || err.message) |
172 | 208 | reject({ |
173 | 209 | status: 400, |
174 | 210 | title: msg.title || 'Error', |
175 | | - body: err.response.body.description, |
| 211 | + body: err.response?.body?.description || err.message, |
176 | 212 | }) |
177 | 213 | } |
178 | 214 | }); |
@@ -209,8 +245,45 @@ const sendNoteToTelegram = async(msg, channelHandle, requestArgs, escapeMsg = fa |
209 | 245 | }) |
210 | 246 | } |
211 | 247 |
|
| 248 | +/** |
| 249 | + * Send photo to Telegram channel using buffer |
| 250 | + * @param {Buffer} imageBuffer - Image buffer data |
| 251 | + * @param {string} caption - Photo caption in MarkdownV2 format |
| 252 | + * @param {string} channelHandle - Telegram channel ID |
| 253 | + * @returns {Promise} Telegram API response |
| 254 | + */ |
| 255 | +const sendPhotoToTelegram = async(imageBuffer, caption, channelHandle) => { |
| 256 | + const baseUrl = process.env.TELEGRAM_URL.replace( |
| 257 | + '{NotifyerBotToken}', |
| 258 | + process.env.TELEGRAM_BOT_TOKEN |
| 259 | + ) |
| 260 | + const urlPath = 'sendPhoto'; |
| 261 | + |
| 262 | + return new Promise((resolve, reject) => { |
| 263 | + fetch |
| 264 | + .post(`${baseUrl}/${urlPath}`) |
| 265 | + .field('chat_id', channelHandle) |
| 266 | + .field('caption', caption) |
| 267 | + .field('parse_mode', 'MarkdownV2') |
| 268 | + .attach('photo', imageBuffer, 'image.jpg') |
| 269 | + .timeout({ response: 30000, deadline: 45000 }) |
| 270 | + .then(response => { |
| 271 | + if (response?.ok) { |
| 272 | + console.log('Photo Pushed!') |
| 273 | + resolve(response) |
| 274 | + } else { |
| 275 | + throw new Error(response) |
| 276 | + } |
| 277 | + }) |
| 278 | + .catch(err => { |
| 279 | + reject(err); |
| 280 | + }) |
| 281 | + }) |
| 282 | +} |
| 283 | + |
212 | 284 | module.exports = { |
213 | 285 | withPush: push, |
214 | 286 | withTelegram, |
215 | | - sendNoteToTelegram |
| 287 | + sendNoteToTelegram, |
| 288 | + sendPhotoToTelegram |
216 | 289 | } |
0 commit comments