Skip to content

Commit

Permalink
👾 Ability to add labels when forwarding a message/post (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rnbsov authored Mar 20, 2024
2 parents 135b2de + 5f86ba3 commit 6a39f4c
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 47 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,
})
}
32 changes: 32 additions & 0 deletions src/utils/getLabels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { getSourceLabel } from './getSourceLabel.ts';
import { getDefaultLabel } from './getDefaultLabel.ts';
import { Label } from '../types.ts';
import { type Filter } from 'https://deno.land/x/[email protected]/mod.ts';
import { MyContext } from '../sessionsHandler.ts';

export function getLabels(ctx: Filter<MyContext, 'message:entities:url'>): Label[] {
const source = ctx.msg?.forward_origin;

// retrieving information from session
const sessionIncludeSource = ctx.session.includeSource;
const sessionDefaultLabel = ctx.session.defaultLabel;

const labels = [];

// add default label
const defaultLabel = getDefaultLabel(sessionDefaultLabel);
if (defaultLabel.name) {
labels.push(defaultLabel);
}

// add source label if includeSource is true
if (source && sessionIncludeSource) {
const sourceLabel = getSourceLabel(source);

if (sourceLabel) {
labels.push(sourceLabel);
}
}

return labels;
}
56 changes: 9 additions & 47 deletions src/utils/getUrlAndLabels.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,17 @@
import { getSourceLabel } from "./getSourceLabel.ts";
import { getDefaultLabel } from "./getDefaultLabel.ts";
import { parseUrls } from "./parseUrls.ts";
import { startsWithUrl } from "./startsWithUrl.ts";
import { Label } from '../types.ts'
import { type Filter } from "https://deno.land/x/[email protected]/mod.ts"
import { type Filter } from 'https://deno.land/x/[email protected]/mod.ts'
import { MyContext } from '../sessionsHandler.ts'
import { getLabels } from "./getLabels.ts";
import { retrieveUrl } from "./retrieveUrl.ts";

export function getUrlAndLabels(ctx: Filter<MyContext, 'message:entities:url'>) {
let url, labels: Label[]

// retrieving information from ctx
const message = ctx.message.text
const source = ctx.msg?.forward_origin

// retrieving information from session
const sessionIncludeSource = ctx.session.includeSource
const sessionDefaultLabel = ctx.session.defaultLabel

// parse url from the message
if (startsWithUrl(message)) {
;({ url, labels } = parseUrls(message)[0])
} else if (ctx.entities('text_link').length > 0) {
// handle case when user sends a message with text formatted link
const linkEntity = ctx.entities('text_link')[0]
if (linkEntity && linkEntity.url) {
url = linkEntity.url
}
labels = []
} else {
// retrieve the first url from the message/post
const urlEntity = ctx.entities('url')[0]
if (urlEntity && urlEntity.text) {
url = urlEntity.text
}
labels = []
}
const { url, labels: customLabels } = retrieveUrl(ctx)

// add default label
const defaultLabel = getDefaultLabel(sessionDefaultLabel);
if (defaultLabel.name) {
labels.push(defaultLabel);
}
// add default label and source label
const defaultAndSourceLabels = getLabels(ctx)

// add source label if includeSource is true
if (source && sessionIncludeSource) {
const sourceLabel = getSourceLabel(source);
const labels = [...customLabels, ...defaultAndSourceLabels]

if (sourceLabel) {
labels.push(sourceLabel);
}
}

return { url, labels };
return { url, labels }
}

30 changes: 30 additions & 0 deletions src/utils/retrieveUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { parseUrls } from './parseUrls.ts';
import { startsWithUrl } from './startsWithUrl.ts';
import { Label } from '../types.ts';
import { type Filter } from 'https://deno.land/x/[email protected]/mod.ts';
import { MyContext } from '../sessionsHandler.ts';

export function retrieveUrl(ctx: Filter<MyContext, 'message:entities:url'>) {
const message = ctx.message.text;

let url: string = '';
let labels: Label[] = [];

if (startsWithUrl(message)) {
; ({ url, labels } = parseUrls(message)[0]);
} else if (ctx.entities('text_link').length > 0) {
// handle case when user sends a message with text formatted link
const linkEntity = ctx.entities('text_link')[0];
if (linkEntity && linkEntity.url) {
url = linkEntity.url;
}
} else {
// retrieve the first url from the message/post
const urlEntity = ctx.entities('url')[0];
if (urlEntity && urlEntity.text) {
url = urlEntity.text;
}
}

return { url, labels };
}

0 comments on commit 6a39f4c

Please sign in to comment.