Skip to content

Commit

Permalink
👾 Ability to add labels when forwarding a message/post
Browse files Browse the repository at this point in the history
  • Loading branch information
Rnbsov committed Mar 20, 2024
1 parent 08692e2 commit 9770039
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createConversation } from 'https://deno.land/x/grammy_conversations@v1.
import { conversations } from 'https://deno.land/x/[email protected]/mod.ts'
import {
askApiKey,
forwardPostLabels,
saveBunchUrls,
setDefaultLabel,
updateToken,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 => {
Expand Down
37 changes: 37 additions & 0 deletions src/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyContext>

Expand Down Expand Up @@ -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,
})
}

0 comments on commit 9770039

Please sign in to comment.