Skip to content

Commit

Permalink
handle image as document
Browse files Browse the repository at this point in the history
  • Loading branch information
LelouchHe committed Mar 16, 2023
1 parent df6fa5b commit 2f26d10
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ node_modules/
# Dist folder
dist/

.vscode/
.vscode/

coverage/
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
33 changes: 32 additions & 1 deletion src/message-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,41 @@ function photoHandlerGenerator(bot: Telegraf<Context>) {
};
}

function documentHandlerGenerator(bot: Telegraf<Context>) {
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<Context>) {
const messageHandlers: { type: string, handler: MessageHandler }[] = [
textHandlerGenerator(),
photoHandlerGenerator(bot)
photoHandlerGenerator(bot),
documentHandlerGenerator(bot)
];

for (let handler of messageHandlers) {
Expand Down

0 comments on commit 2f26d10

Please sign in to comment.