Skip to content

Commit

Permalink
πŸ‘Ύ Bunch of features for sending urls with labels (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rnbsov committed Mar 9, 2024
2 parents 93f3b8e + 8160869 commit 6b6e447
Show file tree
Hide file tree
Showing 16 changed files with 259 additions and 96 deletions.
29 changes: 21 additions & 8 deletions bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ import {
Bot,
GrammyError,
HttpError,
} from 'https://deno.land/x/grammy@v1.20.3/mod.ts'
} from 'https://deno.land/x/grammy@v1.21.1/mod.ts'
import { createConversation } from 'https://deno.land/x/grammy_conversations@v1.2.0/conversation.ts'
import { conversations } from 'https://deno.land/x/grammy_conversations@v1.2.0/mod.ts'
import {
askApiKey,
saveBunchUrls,
setDefaultLabel,
updateToken,
} from './src/conversations.ts'
import { cancelMenu } from './src/menus.ts'
import { OmnivoreApi } from './src/omnivore/api.ts'
import { MyContext, sessionHandler } from './src/sessionsHandler.ts'
import { inlineQuery } from "./src/inlineQuery.ts";
import { slashCommandsListener } from './src/slashCommands.ts'
import { cancelMenuAndResetLabel } from "./src/menus.ts";
import { getUrlAndLabels } from "./src/utils/getUrlAndLabels.ts";
import { includeSourceChoiceMenu } from "./src/menus.ts";

await load({ export: true })

Expand All @@ -30,26 +35,34 @@ bot.use(conversations())
bot.use(createConversation(askApiKey))
bot.use(createConversation(saveBunchUrls))
bot.use(createConversation(updateToken))
bot.use(createConversation(setDefaultLabel))

// Menu
bot.use(cancelMenu)
bot.use(cancelMenuAndResetLabel)
bot.use(includeSourceChoiceMenu)

// inline query
bot.use(inlineQuery)

// slash commands handler
bot.use(slashCommandsListener)

// handlers
bot.on('message:entities:url', async ctx => {
// retrieve stuff from session
const token = ctx.session.apiToken

const api = new OmnivoreApi(token)

await api.saveUrl(ctx.message.text || '')
const {url, labels} = getUrlAndLabels(ctx)

if (api.addedEntriesCount === 1) {
await ctx.reply('Successfully added link to Omnivore! πŸ˜ΈπŸ‘')
} else {
await ctx.reply('Failed to add the link. 😿')
}
await api.saveUrl(url, labels)

const feedback = api.addedEntriesCount === 1
? 'Successfully added link to Omnivore! πŸ˜ΈπŸ‘'
: 'Failed to add the link. 😿'

await ctx.reply(feedback)
})

bot.command('start', async ctx => {
Expand Down
100 changes: 34 additions & 66 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion src/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export async function saveBunchUrls(

const api = new OmnivoreApi(token)

await api.processUrls(urlsArray)
await api.processUrls({urls: urlsArray})
await ctx.reply(
`Successfully added ${api.addedEntriesCount} of ${urlsArray.length} links!\nFailed to add ${api.failedEntriesCount} links.`,
{
Expand All @@ -75,3 +75,19 @@ export async function updateToken(
reply_markup: mainKeyboardLayout,
})
}

export async function setDefaultLabel(
conversation: MyConversation,
ctx: MyContext
) {
const newCtx = await conversation.waitFor('msg:text')
const label = newCtx.message?.text

conversation.session.defaultLabel = label || ctx.session.defaultLabel

await ctx.reply(`You've Successfully set the label ${label} πŸŽ‰`, {
reply_markup: mainKeyboardLayout,
})
}


2 changes: 1 addition & 1 deletion src/inlineQuery.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Composer } from 'https://deno.land/x/grammy@v1.20.3/mod.ts'
import { Composer } from 'https://deno.land/x/grammy@v1.21.1/mod.ts'
import { OmnivoreApi } from './omnivore/api.ts'
import { MyContext } from './sessionsHandler.ts'

Expand Down
2 changes: 1 addition & 1 deletion src/keyboards.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Keyboard } from 'https://deno.land/x/grammy@v1.20.3/mod.ts'
import { Keyboard } from 'https://deno.land/x/grammy@v1.21.1/mod.ts'

export const mainKeyboardLayout = new Keyboard()
.text('πŸ‘Ύ Save a bunch of urls')
Expand Down
37 changes: 33 additions & 4 deletions src/menus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,39 @@ import { mainKeyboardLayout } from './keyboards.ts'
import { MyContext } from './sessionsHandler.ts'

// Cancel menu button
export const cancelMenu = new Menu<MyContext>('Cancel')
.text('Cancel', ctx => {
export const cancelMenu = new Menu<MyContext>('Cancel').text(
'Cancel',
ctx => {
ctx.reply('canceled πŸ‘Œ', { reply_markup: mainKeyboardLayout })
ctx.conversation.exit()
}
)
.row()
)

// cancel + reset button for set_default_label command
export const cancelMenuAndResetLabel = new Menu<MyContext>(
'CancelAndReset'
)
.addRange(cancelMenu)
.text('Reset label', ctx => {
ctx.session.defaultLabel = ''

ctx.reply('πŸƒ from now on there is no default label for links')
})

export const includeSourceChoiceMenu = new Menu<MyContext>(
'includeSourceChoiceMenu'
)
.text('yes', ctx => {
ctx.session.includeSource = true

ctx.reply(`Now your links sended to me, will include the source where they came from πŸ˜ΈπŸ‘`, {
reply_markup: mainKeyboardLayout,
})
})
.text('no', ctx => {
ctx.session.includeSource = false

ctx.reply(`Your links won't include the source, and that's absolutely normal πŸ˜ΈπŸ‘`, {
reply_markup: mainKeyboardLayout,
})
})
Loading

0 comments on commit 6b6e447

Please sign in to comment.