Skip to content

Commit

Permalink
feat: implement receiving file link
Browse files Browse the repository at this point in the history
  • Loading branch information
bondiano committed Jul 28, 2024
1 parent ddbf21a commit e483928
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/telega/api.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import gleam/string
import telega/log
import telega/model.{
type AnswerCallbackQueryParameters, type BotCommand, type BotCommandParameters,
type EditMessageTextParameters, type EditMessageTextResult,
type EditMessageTextParameters, type EditMessageTextResult, type File,
type ForwardMessageParameters, type Message as ModelMessage,
type SendDiceParameters, type SendMessageParameters, type User,
type WebhookInfo,
Expand Down Expand Up @@ -325,6 +325,15 @@ fn build_url(config: TelegramApiConfig, path: String) -> String {
config.tg_api_url <> config.token <> "/" <> path
}

pub fn get_file(
config config: TelegramApiConfig,
file_id file_id: String,
) -> Result(File, String) {
new_get_request(config, path: "getFile", query: Some([#("file_id", file_id)]))
|> fetch(config)
|> map_resonse(model.decode_file)
}

fn new_post_request(
config config: TelegramApiConfig,
path path: String,
Expand Down
30 changes: 30 additions & 0 deletions src/telega/model.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2146,6 +2146,36 @@ pub fn encode_set_webhook_parameters(params: SetWebhookParameters) -> Json {
])
}

// File --------------------------------------------------------------------------------------------------------------

/// https://core.telegram.org/bots/api#file
pub type File {
/// This object represents a file ready to be downloaded. The file can be downloaded via the link `https://api.telegram.org/file/bot<token>/<file_path>`. It is guaranteed that the link will be valid for at least 1 hour. When the link expires, a new one can be requested by calling [getFile](https://core.telegram.org/bots/api#getfile).
///
/// **Official reference:** [File](https://core.telegram.org/bots/api#file)
File(
/// Unique identifier for this file
file_id: String,
/// _Optional_. Unique identifier for this file, which is supposed to be the same over time and for different bots. Can't be used to download or reuse the file
file_unique_id: Option(String),
/// _Optional_. File size, if known
file_size: Option(Int),
/// _Optional_. File path. Use `https://api.telegram.org/file/bot<token>/<file_path>` to get the file
file_path: Option(String),
)
}

pub fn decode_file(json: Dynamic) -> Result(File, dynamic.DecodeErrors) {
json
|> dynamic.decode4(
File,
dynamic.field("file_id", dynamic.string),
dynamic.optional_field("file_unique_id", dynamic.string),
dynamic.optional_field("file_size", dynamic.int),
dynamic.optional_field("file_path", dynamic.string),
)
}

// Common ------------------------------------------------------------------------------------------------------------

pub type IntOrString {
Expand Down
21 changes: 21 additions & 0 deletions src/telega/reply.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gleam/option.{type Option}
import gleam/result
import telega/api
import telega/bot.{type Context}
import telega/model.{
Expand Down Expand Up @@ -90,3 +91,23 @@ pub fn answer_callback_query(
) -> Result(Bool, String) {
api.answer_callback_query(ctx.config.api, parameters)
}

/// Get download link for the file.
pub fn with_file_link(
ctx ctx: Context(session),
file_id file_id: String,
) -> Result(String, String) {
use file <- result.try(api.get_file(ctx.config.api, file_id))
use file_path <- result.try(option.to_result(
file.file_path,
"File path is missing",
))

Ok(
ctx.config.api.tg_api_url
<> "/file/bot"
<> ctx.config.secret_token
<> "/"
<> file_path,
)
}

0 comments on commit e483928

Please sign in to comment.