From 977003949b9cbc925dae1547cb4c4888c9fc6ca2 Mon Sep 17 00:00:00 2001 From: Rnbsov Date: Wed, 20 Mar 2024 15:38:07 +0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=BE=20Ability=20to=20add=20labels=20wh?= =?UTF-8?q?en=20forwarding=20a=20message/post?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot.ts | 6 ++++++ src/conversations.ts | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/bot.ts b/bot.ts index b8e3be6..5c925bf 100644 --- a/bot.ts +++ b/bot.ts @@ -8,6 +8,7 @@ import { createConversation } from 'https://deno.land/x/grammy_conversations@v1. import { conversations } from 'https://deno.land/x/grammy_conversations@v1.2.0/mod.ts' import { askApiKey, + forwardPostLabels, saveBunchUrls, setDefaultLabel, updateToken, @@ -37,6 +38,7 @@ bot.use(createConversation(askApiKey)) bot.use(createConversation(saveBunchUrls)) bot.use(createConversation(updateToken)) bot.use(createConversation(setDefaultLabel)) +bot.use(createConversation(forwardPostLabels)) // Menu bot.use(cancelMenu) @@ -111,6 +113,10 @@ You can get new by following this guide [Getting an API token](https://docs.omni await ctx.conversation.enter('updateToken') }) +bot.on('message:text', async ctx => { + await ctx.conversation.enter('forwardPostLabels') +}) + bot.start() bot.catch(err => { diff --git a/src/conversations.ts b/src/conversations.ts index 8dfcbca..9a5781c 100644 --- a/src/conversations.ts +++ b/src/conversations.ts @@ -3,6 +3,8 @@ import { MyContext } from './sessionsHandler.ts' import { mainKeyboardLayout } from './keyboards.ts' import { OmnivoreApi } from './omnivore/api.ts' import { parseUrls } from './utils/parseUrls.ts' +import { Label } from "./types.ts"; +import { getLabels } from "./utils/getLabels.ts"; type MyConversation = Conversation @@ -90,4 +92,39 @@ export async function setDefaultLabel( }) } +export async function forwardPostLabels( + conversation: MyConversation, + ctx: MyContext +) { + const userLabels = (ctx.message?.text || '').trim().split(/\s+/).map(label => ({ name: label })) + const token = conversation.session.apiToken + + const api = new OmnivoreApi(token) + + const urlInput = await conversation.waitFor('message:entities:url') + let url = '' + console.log(urlInput) + if (urlInput.entities('text_link').length > 0) { + // handle case when user sends a message with text formatted link + const linkEntity = urlInput.entities('text_link')[0]; + if (linkEntity && linkEntity.url) { + url = linkEntity.url; + } + } else { + // retrieve the first url from the message/post + const urlEntity = urlInput.entities('url')[0]; + if (urlEntity && urlEntity.text) { + url = urlEntity.text; + } + } + + const defaultLabels = getLabels(urlInput) + const labels: Label[] = [...userLabels, ...defaultLabels] + + api.saveUrl(url, labels) + + await ctx.reply('Successfully added link to Omnivore! πŸ˜ΈπŸ‘', { + reply_markup: mainKeyboardLayout, + }) +}