# Downloading Channel Data

## Introduction

This vignette demonstrates how to download channel data with `telegramR` using the high‑level helpers.

## Offline Demo (No Telegram Connection)

To keep the vignette reproducible, the outputs below come from a bundled sample dataset. The commands shown are the real calls you would use against Telegram.

## Setup and Authentication

    library(telegramR)

    # Replace these with your own API ID and Hash
    api_id <- 123456
    api_hash <- "0123456789abcdef0123456789abcdef"

    client <- TelegramClient$new("my_session", api_id, api_hash)
    client$start()

## Download Messages

    # Download the most recent 200 messages from a channel
    msgs <- download_channel_messages(
      client,
      "telegram",
      limit = 200
    )

### Example Output (Bundled Sample)

    msgs
    #> # A tibble: 11,605 × 18
    #>    message_id channel_id channel_username     channel_title  date
    #>         <dbl>      <dbl> <chr>                <chr>          <dttm>
    #>  1      18225 1463721328 V_Zelenskiy_official Zelenskiy / O… 2026-03-06 11:55:27
    #>  2      18224 1463721328 V_Zelenskiy_official Zelenskiy / O… 2026-03-06 11:06:59
    #>  3      18223 1463721328 V_Zelenskiy_official Zelenskiy / O… 2026-03-06 11:06:58
    #>  4      18222 1463721328 V_Zelenskiy_official Zelenskiy / O… 2026-03-06 11:06:58
    #>  5      18221 1463721328 V_Zelenskiy_official Zelenskiy / O… 2026-03-06 11:06:58
    #>  6      18220 1463721328 V_Zelenskiy_official Zelenskiy / O… 2026-03-06 11:06:58
    #>  7      18219 1463721328 V_Zelenskiy_official Zelenskiy / O… 2026-03-06 11:06:58
    #>  8      18218 1463721328 V_Zelenskiy_official Zelenskiy / O… 2026-03-06 11:06:58
    #>  9      18217 1463721328 V_Zelenskiy_official Zelenskiy / O… 2026-03-06 11:06:58
    #> 10      18216 1463721328 V_Zelenskiy_official Zelenskiy / O… 2026-03-06 11:06:58
    #> # ℹ 11,595 more rows
    #> # ℹ 13 more variables: text <chr>, views <dbl>, forwards <dbl>, replies <dbl>,
    #> #   reactions_total <dbl>, reactions_json <chr>, media_type <chr>,
    #> #   is_forward <lgl>, forward_from_id <dbl>, forward_from_name <chr>,
    #> #   reply_to_msg_id <dbl>, edit_date <dttm>, post_author <chr>

The returned tibble includes a compact, analysis‑ready schema:

- `message_id`, `date`, `text`
- `views`, `forwards`, `replies`
- `reactions_total`, `reactions_json`
- `media_type`, `is_forward`, `forward_from_id`, `forward_from_message_id`, `forward_saved_from_message_id`, `forward_from_name`
- `reply_to_msg_id`, `edit_date`, `post_author`
- `channel_id`, `channel_username`, `channel_title`

## Download Channel Info

    info <- download_channel_info(client, "telegram")

    info
    #> # A tibble: 1 × 13
    #>   channel_id username          title description subscribers region channel_type
    #>        <dbl> <chr>             <chr> <chr>             <dbl> <chr>  <chr>
    #> 1 1463721328 V_Zelenskiy_offi… Zele… Офіційний …      684326 <NA>   channel
    #> # ℹ 6 more variables: created_date <dttm>, is_verified <lgl>,
    #> #   is_restricted <lgl>, linked_chat_id <dbl>, download_date <dttm>,
    #> #   last_message_id <dbl>

## Estimate Total Posts (Approx.)

This returns the latest message id as an upper‑bound estimate.

    estimate <- estimate_channel_post_count(client, "telegram")

    estimate
    #> # A tibble: 1 × 2
    #>   last_message_id note
    #>             <dbl> <chr>
    #> 1           18225 Approximate: uses latest message id as upper bound; gaps/dele…

## Download Media

    # Find the first message with media
    first_media <- msgs[which(!is.na(msgs$media_type) & msgs$media_type != ""), ][1, ]

    if (nrow(first_media) > 0) {
      # You can still use download_media on the underlying message object
      # if you need the original media data.
    }

## Disconnect

    client$disconnect()

    # Download a date range (UTC)
    msgs_range <- download_channel_messages(
      client,
      "telegram",
      start_date = "2025-01-01",
      end_date   = "2025-02-01",
      limit      = Inf
    )

## Download Reactions

    reactions <- download_channel_reactions(
      client,
      "telegram",
      limit = 500
    )

    reactions
    #> # A tibble: 200 × 7
    #>    message_id channel_id channel_username     channel_title  date
    #>         <dbl>      <dbl> <chr>                <chr>          <dttm>
    #>  1      18225 1463721328 V_Zelenskiy_official Zelenskiy / O… 2026-03-06 11:55:27
    #>  2      18224 1463721328 V_Zelenskiy_official Zelenskiy / O… 2026-03-06 11:06:59
    #>  3      18223 1463721328 V_Zelenskiy_official Zelenskiy / O… 2026-03-06 11:06:58
    #>  4      18222 1463721328 V_Zelenskiy_official Zelenskiy / O… 2026-03-06 11:06:58
    #>  5      18221 1463721328 V_Zelenskiy_official Zelenskiy / O… 2026-03-06 11:06:58
    #>  6      18220 1463721328 V_Zelenskiy_official Zelenskiy / O… 2026-03-06 11:06:58
    #>  7      18219 1463721328 V_Zelenskiy_official Zelenskiy / O… 2026-03-06 11:06:58
    #>  8      18218 1463721328 V_Zelenskiy_official Zelenskiy / O… 2026-03-06 11:06:58
    #>  9      18217 1463721328 V_Zelenskiy_official Zelenskiy / O… 2026-03-06 11:06:58
    #> 10      18216 1463721328 V_Zelenskiy_official Zelenskiy / O… 2026-03-06 11:06:58
    #> # ℹ 190 more rows
    #> # ℹ 2 more variables: reactions_total <dbl>, reactions_json <chr>

## Download Replies (Comments)

    # Fetch replies for the latest 20 posts
    replies <- download_channel_replies(
      client,
      "telegram",
      message_limit = 20,
      reply_limit = Inf
    )

    replies
    #> # A tibble: 0 × 0

## Download Members

    members <- download_channel_members(
      client,
      "telegram",
      limit = 500
    )

    members
    #> # A tibble: 0 × 0

## Numeric Channel IDs

All helpers accept a numeric channel id if it is available in your session cache:

    msgs_by_id <- download_channel_messages(client, 1234567890, limit = 100)