Skip to content

Commit

Permalink
Add Telegram::download, closes #1703
Browse files Browse the repository at this point in the history
  • Loading branch information
wojpawlik committed Sep 26, 2022
1 parent 02ace96 commit 9eb3dd2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/core/network/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// for https://gist.github.com/2b1b226d52d675ec246c6f8abdab81ef
export type { Update, UserFromGetMe } from 'typegram'
import type { ApiResponse, Typegram } from 'typegram'
import type { ApiResponse, File, Typegram } from 'typegram'
import createDebug from 'debug'

const debug = createDebug('telegraf:client')
Expand Down Expand Up @@ -126,7 +126,17 @@ export function createClient(token: string, { api } = defaultOptions) {
}
return (await res.json()) as ApiResponse<Ret[M]>
}
return { call }

const download = async (file: File) => {
const url = new URL(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
`./file/${api.mode}${token}/${file.file_path!}`,
api.root
)
return await fetch(url)
}

return { call, download }
}

export type Client = ReturnType<typeof createClient>
2 changes: 2 additions & 0 deletions src/telegram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ export type Transformer = EndoFunction<Client['call']>

export class Telegram {
call: Client['call']
readonly download: Client['download']

constructor(client: Client) {
this.call = client.call
this.download = client.download
}

use(transform: Transformer) {
Expand Down

0 comments on commit 9eb3dd2

Please sign in to comment.