Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🍒 Save pdfs via bot #7

Merged
merged 7 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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";
Expand Down Expand Up @@ -45,6 +46,9 @@ bot.use(includeSourceChoiceMenu)
// inline query
bot.use(inlineQuery)

// file handling
bot.use(fileListener)

// slash commands handler
bot.use(slashCommandsListener)

Expand Down
47 changes: 47 additions & 0 deletions src/files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Composer } from 'https://deno.land/x/[email protected]/mod.ts'
import { MyContext } from './sessionsHandler.ts'
import { OmnivoreApi } from './omnivore/api.ts'

export const fileListener = new Composer<MyContext>()

fileListener.on('message:document', async (ctx, next) => {
try {
const file = ctx.msg.document
const fileType = file.mime_type
const fileSize = file.file_size

const limit = 10_485_760

if (
fileType !== 'application/pdf' &&
fileType !== 'application/epub+zip'
) {
return await next()
}

if (fileSize && fileSize > limit) {
return await ctx.reply(
'Your file size is exceeding our limit of 10mb 😿'
)
}

const fileInfo = await ctx.getFile()

const token = ctx.session.apiToken
const api = new OmnivoreApi(token)

await ctx.reply(
`Started processing your file 😸👍 \n\n >${file.file_name}`,
{ parse_mode: 'MarkdownV2' }
)

await api.uploadFile(fileInfo, fileType)

await ctx.reply(`File successfully uploaded 🤩✨`)
} catch (error) {
console.log(error)
await ctx.reply(
'Something went wrong, please try again after a while'
)
}
})
73 changes: 73 additions & 0 deletions src/omnivore/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
graphqlEndpoint,
searchQuery,
} 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";

Expand Down Expand Up @@ -156,4 +158,75 @@ export class OmnivoreApi implements OmnivoreApiInterface {
}
}
}

// private async getSignedUploadUrl(path) {

// return uploadSignedUrl
// }

async uploadFile(file: File, fileType: any) {
// get file blob
const fileDownloadUrl = `https://api.telegram.org/file/bot${Deno.env.get('BOT_TOKEN')}/${file.file_path}`

const fileDownloadUrlResponse = await fetch(fileDownloadUrl)

if (!fileDownloadUrlResponse.ok) {
throw new Error('Something went wrong, please try again after a while')
}

const pdfBlob = await fileDownloadUrlResponse.blob();

// getSignedUploadUrl
const response = await fetch(graphqlEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'fb518687-1ade-497d-968a-f3cc2d4d3fa3',
},
body: JSON.stringify({
query: `
mutation UploadFileRequest($input: UploadFileRequestInput!) {
uploadFileRequest(input: $input) {
... on UploadFileRequestError {
errorCodes
}
... on UploadFileRequestSuccess {
id
uploadSignedUrl
createdPageId
}
}
}
`,
variables: {
input: {
url: `file://local/${file.file_id}/${file.file_path}`,
contentType: fileType,
createPageEntry: true,
clientRequestId: globalThis.crypto.randomUUID()
}
}
}),
})

const result = await response.json();

if(result.data.uploadFileRequest?.errorCodes) {
console.log(result.data.uploadFileRequest.errorCodes)

throw new Error('Failed to get signedUrl')
}

const { uploadSignedUrl } = result?.data?.uploadFileRequest

const fileUploadResponse = await fetch(uploadSignedUrl, {
method: 'PUT',
headers: {
'Content-Type': fileType,
},
body: pdfBlob
})

console.log(await fileUploadResponse.text())
}
}
15 changes: 15 additions & 0 deletions src/omnivore/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,18 @@ export const searchQuery = `
}
}
`

export const uploadFileMutation = `
mutation UploadFileRequest($input: UploadFileRequestInput!) {
uploadFileRequest(input: $input) {
... on UploadFileRequestError {
errorCodes
}
... on UploadFileRequestSuccess {
id
uploadSignedUrl
createdPageId
}
}
}
`
Loading