diff --git a/.gitignore b/.gitignore index 239ee4b..3ed9e48 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,6 @@ node_modules/ # Dist folder dist/ -.vscode/ \ No newline at end of file +.vscode/ + +coverage/ diff --git a/package.json b/package.json index a736786..f6234f5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "logseq-local-telegram-bot", - "version": "0.2.13", + "version": "0.2.14", "description": "A local Telegram bot plugin that can handle messages from and share notes with eligible Telegram users", "author": "LelouchHe", "license": "MIT", diff --git a/src/message-handlers.ts b/src/message-handlers.ts index c0fcb0a..f0a3032 100644 --- a/src/message-handlers.ts +++ b/src/message-handlers.ts @@ -213,10 +213,41 @@ function photoHandlerGenerator(bot: Telegraf) { }; } +function documentHandlerGenerator(bot: Telegraf) { + async function handler(ctx: Context, message: Message.DocumentMessage) { + if (!message.document.mime_type?.startsWith("image/")) { + log(`document mime_type is not image: ${message.document.mime_type}`); + return; + } + + const photoUrl = await ctx.telegram.getFileLink(message.document.file_id); + const caption = message.caption ?? DEFAULT_CAPTION; + let text = photoTemplate(caption, message.document.file_id, photoUrl); + if (settings.addTimestamp) { + const receiveDate = new Date(); + receiveDate.setTime(message.date * 1000); + + text = `${getTimestampString(receiveDate)} - ${text}`; + } + + if (!await writeBlock( + settings.pageName, + settings.inboxName, + text)) { + ctx.reply("Failed to write this to Logseq"); + } + } + return { + type: "document", + handler: handler as MessageHandler + }; +} + function setupMessageHandlers(bot: Telegraf) { const messageHandlers: { type: string, handler: MessageHandler }[] = [ textHandlerGenerator(), - photoHandlerGenerator(bot) + photoHandlerGenerator(bot), + documentHandlerGenerator(bot) ]; for (let handler of messageHandlers) {