Skip to content

Commit

Permalink
Merge branch 'main' into pdfs-uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
Rnbsov authored Mar 9, 2024
2 parents 3657fe7 + 6b6e447 commit 02d123d
Show file tree
Hide file tree
Showing 18 changed files with 316 additions and 58 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Deno lint

on: [push, pull_request]

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Setup repo
uses: actions/checkout@v3

- name: Setup Deno
uses: denoland/setup-deno@v1
with:
deno-version: v1.x

- name: Run linter
run: deno lint
22 changes: 22 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Deno tests

on: [push, pull_request]

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Setup repo
uses: actions/checkout@v3

- name: Setup Deno
uses: denoland/setup-deno@v1
with:
deno-version: v1.x

- name: Run tests
run: deno test -A
29 changes: 21 additions & 8 deletions bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ 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/[email protected]/conversation.ts'
import { conversations } from 'https://deno.land/x/[email protected]/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 { fileListener } from "./src/files.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 @@ -31,29 +36,37 @@ 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)

// file handling
bot.use(fileListener)

// 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
68 changes: 34 additions & 34 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,
})
})
18 changes: 13 additions & 5 deletions src/omnivore/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
} from './graphql.ts'
import { InlineQueryResultBuilder } from 'https://deno.land/x/[email protected]/mod.ts'
import { File } from "https://deno.land/x/[email protected]/message.ts";
import { InlineQueryResultBuilder } from 'https://deno.land/x/[email protected]/mod.ts'
import { Label, ProcessUrlsParams } from "../types.ts";

interface OmnivoreApiInterface {
apiToken: string
Expand All @@ -29,12 +31,13 @@ export class OmnivoreApi implements OmnivoreApiInterface {
this.apiToken = apiToken
}

async saveUrl(url: string) {
async saveUrl(url: string, labels: Label[]) {
const variables = {
input: {
clientRequestId: globalThis.crypto.randomUUID(),
source: 'api',
url,
labels
},
}

Expand Down Expand Up @@ -70,13 +73,18 @@ export class OmnivoreApi implements OmnivoreApiInterface {
}
}

async processUrls(urls: string[], startIndex = 0) {
async processUrls({ urls, additionalLabels, startIndex = 0 }: ProcessUrlsParams) {
const batchSize = 50
const remainingUrls = urls.slice(startIndex)

for (let i = 0; i <= batchSize && i < remainingUrls.length; i++) {
const url = remainingUrls[i]
await this.saveUrl(url)
const url = remainingUrls[i].url
const urlLabels = remainingUrls[i].labels

// add additional labels to each url
additionalLabels?.map((label) => urlLabels.push(label))

await this.saveUrl(url, urlLabels)
}

const nextIndex = startIndex + batchSize + 1
Expand All @@ -85,7 +93,7 @@ export class OmnivoreApi implements OmnivoreApiInterface {
await new Promise(resolve =>
setTimeout(resolve, OmnivoreApi.delayBetweenRequests)
)
await this.processUrls(urls, nextIndex)
await this.processUrls({urls, startIndex: nextIndex})
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/sessionsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
Context,
session,
SessionFlavor,
} from 'https://deno.land/x/grammy@v1.20.3/mod.ts'
} from 'https://deno.land/x/grammy@v1.21.1/mod.ts'
import { createClient } from 'https://esm.sh/@supabase/[email protected]'
import { supabaseAdapter } from 'https://deno.land/x/[email protected]/supabase/src/mod.ts'
import { load } from 'https://deno.land/[email protected]/dotenv/mod.ts'
Expand All @@ -22,6 +22,8 @@ const storage = supabaseAdapter({

interface SessionData {
apiToken: string
defaultLabel: string
includeSource: boolean
}

export type MyContext = Context &
Expand All @@ -31,6 +33,8 @@ export type MyContext = Context &
function initial(): SessionData {
return {
apiToken: '',
defaultLabel: '',
includeSource: false
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/slashCommands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Composer } from 'https://deno.land/x/[email protected]/mod.ts'
import { MyContext } from './sessionsHandler.ts'
import { cancelMenuAndResetLabel, includeSourceChoiceMenu } from "./menus.ts";

export const slashCommandsListener = new Composer<MyContext>()

slashCommandsListener.hears('/set_default_label', async ctx => {
ctx.reply(
'Send the label you want to be added for all of the links you send me',
{
reply_markup: cancelMenuAndResetLabel,
}
)

await ctx.conversation.enter('setDefaultLabel')
})

slashCommandsListener.hears('/set_include_source', ctx => {
ctx.reply(
'Do you want to include label from what channel or contact you\'ve saved the article?', {
reply_markup: includeSourceChoiceMenu
})
})
Loading

1 comment on commit 02d123d

@deno-deploy
Copy link

@deno-deploy deno-deploy bot commented on 02d123d Mar 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed to deploy:

BOOT_FAILURE

Uncaught SyntaxError: Identifier 'InlineQueryResultBuilder' has already been declared
    at file:///src/src/omnivore/api.ts:4:10

Please sign in to comment.