Telegram.Api (telegram v2.1.0)

View Source

Telegram Bot API - HTTP-based interface

Summary

Functions

Download a file.

Send a Telegram Bot API request.

Types

parameter_name()

@type parameter_name() :: atom() | String.t()

parameter_value()

@type parameter_value() ::
  integer()
  | float()
  | String.t()
  | boolean()
  | {:json, json_serialized_object :: term()}
  | {:file, path :: Path.t()}
  | {:file_content, content :: iodata(), filename :: String.t()}

parameters()

@type parameters() :: [{parameter_name(), parameter_value()}]

request_result()

@type request_result() :: {:ok, term()} | {:error, term()}

Functions

file(token, file_path)

Download a file.

Reference: BOT Api

Example:

# send a photo
{:ok, res} = Telegram.Api.request(token, "sendPhoto", chat_id: 12345, photo: {:file, "example/photo.jpg"})
# pick the 'file_obj' with the desired resolution
[file_obj | _] = res["photo"]
# get the 'file_id'
file_id = file_obj["file_id"]

# obtain the 'file_path' to download the file identified by 'file_id'
{:ok, %{"file_path" => file_path}} = Telegram.Api.request(token, "getFile", file_id: file_id)
{:ok, file} = Telegram.Api.file(token, file_path)

request(token, method, parameters \\ [])

Send a Telegram Bot API request.

The request parameters map to the bots API parameters.

  • Integer String Boolean Float: Elixir native data type
  • JSON-serialized: {:json, _} tuple
  • InputFile: {:file, _} or {:file_content, _, _} tuple

Reference: BOT Api