This commit is contained in:
2025-08-26 15:28:28 +04:00
commit e022a85199
9 changed files with 2177 additions and 0 deletions

1
.envrc Normal file
View File

@@ -0,0 +1 @@
use flake

7
.gitignore vendored Normal file
View File

@@ -0,0 +1,7 @@
/target
/.direnv
/db
/.env
/result
/telegram-exporter-arx
/channels.json

1555
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

28
Cargo.toml Normal file
View File

@@ -0,0 +1,28 @@
[package]
name = "telegram-exporter"
version = "0.1.0"
edition = "2024"
[dependencies]
dotenv_codegen = "0.15.0"
dotenvy = "0.15.7"
flate2 = { version = "1.1.2", features = ["zlib"] }
indicatif = "0.18.0"
inquire = "0.7.5"
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.143"
tdlib-rs = { version = "1.1.0", features = ["pkg-config"] }
tokio = { version = "1.47.1", features = ["full"] }
[build-dependencies]
tdlib-rs = { version = "1.1.0", features = ["pkg-config"] }
[package.metadata.system-deps]
tdjson = "1.8.29"
[profile.release]
codegen-units = 1
lto = true
opt-level = "z"
strip = true
panic = "abort"

3
build.rs Normal file
View File

@@ -0,0 +1,3 @@
fn main() {
tdlib_rs::build::build(None);
}

115
flake.lock generated Normal file
View File

@@ -0,0 +1,115 @@
{
"nodes": {
"crane": {
"locked": {
"lastModified": 1755993354,
"narHash": "sha256-FCRRAzSaL/+umLIm3RU3O/+fJ2ssaPHseI2SSFL8yZU=",
"owner": "ipetkov",
"repo": "crane",
"rev": "25bd41b24426c7734278c2ff02e53258851db914",
"type": "github"
},
"original": {
"owner": "ipetkov",
"repo": "crane",
"type": "github"
}
},
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1756125398,
"narHash": "sha256-XexyKZpf46cMiO5Vbj+dWSAXOnr285GHsMch8FBoHbc=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "3b9f00d7a7bf68acd4c4abb9d43695afb04e03a5",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"process-compose-wrapper": {
"locked": {
"lastModified": 1747144888,
"narHash": "sha256-qxIPqNf4JS9Gz138MP+UOSk7PAsIniDhW0NvOeaC/Ek=",
"ref": "dev",
"rev": "948180a09c429d24648d283212a09ff0f50b2815",
"revCount": 86,
"type": "git",
"url": "https://git.scug.io/nikkuss/process-compose-wrapper.git"
},
"original": {
"ref": "dev",
"type": "git",
"url": "https://git.scug.io/nikkuss/process-compose-wrapper.git"
}
},
"root": {
"inputs": {
"crane": "crane",
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs",
"process-compose-wrapper": "process-compose-wrapper",
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1756175826,
"narHash": "sha256-cQNnntKWve+vnqo6pGGXl4NFT4dgnMKXl4+bpwLELvU=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "d137b47bde8a6783b961db81254013b454eab46a",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

120
flake.nix Normal file
View File

@@ -0,0 +1,120 @@
{
description = "A very basic flake";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
crane.url = "github:ipetkov/crane";
flake-utils.url = "github:numtide/flake-utils";
process-compose-wrapper = {
url = "git+https://git.scug.io/nikkuss/process-compose-wrapper.git?ref=dev";
};
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
{
nixpkgs,
crane,
flake-utils,
process-compose-wrapper,
rust-overlay,
...
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [
(final: prev: {
tdlib = prev.tdlib.overrideAttrs (_: {
version = "1.8.29";
src = prev.fetchFromGitHub {
owner = "tdlib";
repo = "td";
rev = "af69dd4397b6dc1bf23ba0fd0bf429fcba6454f6";
hash = "sha256-2RhKSxy0AvuA74LHI86pqUxv9oJZ+ZxxDe4TPI5UYxE=";
# hash = "sha256-mbhxuJjrV3nC8Ja7N0WWF9ByHovJLmoLLuuzoU4khjU=";
};
});
})
(import rust-overlay)
];
};
process-compose = process-compose-wrapper.lib.mkLib pkgs;
inherit (pkgs) lib;
craneLib = (crane.mkLib pkgs).overrideToolchain (
p:
p.rust-bin.nightly.latest.default.override {
extensions = [
"rustc-codegen-cranelift-preview"
"rust-analyzer"
"rust-src"
];
}
);
src = lib.cleanSourceWith {
src = ./.;
filter = (
path: type: (craneLib.filterCargoSources path type) || (builtins.match ".*/\.env$" path != null)
);
name = "source";
};
commonArgs = {
inherit src;
strictDeps = true;
nativeBuildInputs = with pkgs; [
pkg-config
];
buildInputs = with pkgs; [
tdlib
openssl
];
};
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
server = craneLib.buildPackage (
commonArgs
// {
inherit cargoArtifacts;
inherit (craneLib.crateNameFromCargoToml { inherit src; }) version;
postInstall = ''
${pkgs.upx}/bin/upx --lzma --best $out/bin/telegram-exporter
'';
}
);
in
{
packages = {
process-compose = process-compose.mkWrapper {
name = "process-compose";
config = (import ./process-compose.nix { inherit pkgs; });
modules = [ ];
};
default = server;
inherit server;
};
devShells.default = craneLib.devShell {
packages = with pkgs; [
mold
llvmPackages.clang
llvmPackages.lld
watchexec
openssl
pkg-config
tdlib
];
};
}
);
}

10
process-compose.nix Normal file
View File

@@ -0,0 +1,10 @@
{ pkgs, ... }:
{
processes = {
backend = {
command = ''
${pkgs.watchexec}/bin/watchexec --restart -w Cargo.toml -w Cargo.lock -- cargo run
'';
};
};
}

338
src/main.rs Normal file
View File

@@ -0,0 +1,338 @@
use std::{
env,
sync::{
Arc,
atomic::{AtomicBool, Ordering},
},
};
use dotenv_codegen::dotenv;
use flate2::write::ZlibEncoder;
use indicatif::{ProgressBar, ProgressStyle};
use inquire::{Password, Text, validator::StringValidator};
use serde::{Deserialize, Serialize};
use tdlib_rs::{
enums::{
self, AuthorizationState, Chat, ChatPhoto, Chats, Supergroup, SupergroupFullInfo, Update,
User,
},
functions,
};
use tokio::sync::mpsc::{self, Receiver, Sender};
async fn handle_update(update: Update, auth_tx: &Sender<AuthorizationState>) {
match update {
Update::AuthorizationState(state) => {
auth_tx.send(state.authorization_state).await.unwrap();
}
_ => {
// println!("Received update: {:?}", update);
}
}
}
#[derive(Debug, Clone)]
struct NumberValidator;
impl NumberValidator {
fn new() -> Self {
Self
}
}
impl StringValidator for NumberValidator {
fn validate(
&self,
input: &str,
) -> Result<inquire::validator::Validation, inquire::CustomUserError> {
if input
.chars()
.all(|c| c.is_ascii_digit() || c == '+' || c == '-')
&& input.len() >= 4
{
Ok(inquire::validator::Validation::Valid)
} else {
Ok(inquire::validator::Validation::Invalid(
"Please enter a valid phone number.".into(),
))
}
}
}
fn ask_user(string: &str) -> String {
println!("{string}");
let mut input = String::new();
std::io::stdin().read_line(&mut input).unwrap();
input.trim().to_string()
}
async fn handle_authorization_state(
client_id: i32,
mut auth_rx: Receiver<AuthorizationState>,
run_flag: Arc<AtomicBool>,
) -> Receiver<AuthorizationState> {
while let Some(state) = auth_rx.recv().await {
match state {
AuthorizationState::WaitTdlibParameters => {
let response = functions::set_tdlib_parameters(
false,
"db".into(),
String::new(),
String::new(),
false,
false,
false,
false,
dotenv!("APP_ID").parse().unwrap(),
dotenv!("APP_HASH").into(),
"en".into(),
"Desktop".into(),
String::new(),
env!("CARGO_PKG_VERSION").into(),
client_id,
)
.await;
if let Err(error) = response {
println!("{}", error.message);
}
}
AuthorizationState::WaitPhoneNumber => loop {
// let input = ask_user("Enter your phone number (include the country calling code):");
let number =
Text::new("Enter your phone number (include the country calling code):")
.with_initial_value("+")
.with_validator(NumberValidator::new())
.prompt()
.unwrap();
let response =
functions::set_authentication_phone_number(number, None, client_id).await;
match response {
Ok(_) => break,
Err(e) => println!("{}", e.message),
}
},
AuthorizationState::WaitOtherDeviceConfirmation(x) => {
println!(
"Please confirm this login link on another device: {}",
x.link
);
}
AuthorizationState::WaitEmailAddress(_x) => {
let email_address = ask_user("Please enter email address: ");
let response =
functions::set_authentication_email_address(email_address, client_id).await;
match response {
Ok(_) => break,
Err(e) => println!("{}", e.message),
}
}
AuthorizationState::WaitEmailCode(_x) => {
let code = ask_user("Please enter email authentication code: ");
let response = functions::check_authentication_email_code(
enums::EmailAddressAuthentication::Code(
tdlib_rs::types::EmailAddressAuthenticationCode { code },
),
client_id,
)
.await;
match response {
Ok(_) => break,
Err(e) => println!("{}", e.message),
}
}
AuthorizationState::WaitCode(_) => loop {
let input = Text::new("Enter the authentication code you received:")
.prompt()
.unwrap();
let response = functions::check_authentication_code(input, client_id).await;
match response {
Ok(_) => break,
Err(e) => println!("{}", e.message),
}
},
AuthorizationState::WaitRegistration(_x) => {
// x useless but contains the TOS if we want to show it
let first_name = ask_user("Please enter your first name: ");
let last_name = ask_user("Please enter your last name: ");
functions::register_user(first_name, last_name, false, client_id)
.await
.unwrap();
}
AuthorizationState::WaitPassword(_x) => {
let password = Password::new("Please enter your password:")
.with_display_mode(inquire::PasswordDisplayMode::Masked)
.without_confirmation()
.prompt()
.unwrap();
functions::check_authentication_password(password, client_id)
.await
.unwrap();
}
AuthorizationState::Ready => {
break;
}
AuthorizationState::Closed => {
// Set the flag to false to stop receiving updates from the
// spawned task
run_flag.store(false, Ordering::Release);
break;
}
_ => (),
}
}
auth_rx
}
#[derive(Debug, Serialize, Deserialize)]
struct ChannelInfo {
id: i64,
name: String,
description: String,
minithumbnail: Option<String>,
member_count: i32,
linked_chat_id: i64,
invite_link: Option<ChannelInviteLink>,
usernames: Option<ChannelUsernameInfo>,
}
#[derive(Debug, Serialize, Deserialize)]
struct ChannelInviteLink {
invite_link: String,
name: String,
is_primary: bool,
is_revoked: bool,
}
#[derive(Debug, Serialize, Deserialize)]
struct ChannelUsernameInfo {
active: Vec<String>,
inactive: Vec<String>,
editable: String,
}
#[derive(Debug, Serialize, Deserialize)]
struct TotalData {
total: usize,
channels: Vec<ChannelInfo>,
}
#[tokio::main]
async fn main() {
dotenvy::dotenv().ok();
let client_id = tdlib_rs::create_client();
let (auth_tx, auth_rx) = mpsc::channel(5);
let run_flag = Arc::new(AtomicBool::new(true));
let run_flag_clone = run_flag.clone();
let handle = tokio::spawn(async move {
while run_flag_clone.load(std::sync::atomic::Ordering::Relaxed) {
let result = tokio::task::spawn_blocking(tdlib_rs::receive)
.await
.unwrap();
if let Some((update, _client_id)) = result {
handle_update(update, &auth_tx).await;
} else {
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
}
});
functions::set_log_verbosity_level(0, client_id)
.await
.unwrap();
// Handle the authorization state to authenticate the client
let auth_rx = handle_authorization_state(client_id, auth_rx, run_flag.clone()).await;
// Run the get_me() method to get user information
let User::User(me) = functions::get_me(client_id).await.unwrap();
println!("logged in as {}", me.first_name);
let chats = functions::get_chats(None, i32::MAX, client_id)
.await
.unwrap();
let Chats::Chats(chats) = chats;
// println!("I have {} chats", chats.chat_ids.len());
let chat_ids = chats
.chat_ids
.iter()
.filter_map(|f| {
f.to_string()
.strip_prefix("-100")
.and_then(|s| s.parse::<i64>().ok())
.map(|a| (a, *f))
})
.collect::<Vec<_>>();
println!(
"{} total chats {} of which are channels",
chats.chat_ids.len(),
chat_ids.len()
);
println!("Fetching channel info...");
let mut chat_vec = vec![];
let progress = ProgressBar::new(chat_ids.len() as u64);
progress.set_style(
ProgressStyle::with_template(
"{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({eta})",
)
.unwrap()
.progress_chars("#>-"),
);
for (chat_id, chat_id_origin) in chat_ids {
let sgfi = functions::get_supergroup_full_info(chat_id, client_id)
.await
.unwrap();
let sg = functions::get_supergroup(chat_id, client_id).await.unwrap();
let chat = functions::get_chat(chat_id_origin, client_id)
.await
.unwrap();
let Supergroup::Supergroup(sg) = sg;
let Chat::Chat(chat) = chat;
let SupergroupFullInfo::SupergroupFullInfo(sgfi) = sgfi;
let photo = sgfi.photo.and_then(|p| p.minithumbnail);
let channel_info = ChannelInfo {
id: chat_id_origin,
name: chat.title,
description: sgfi.description,
member_count: sgfi.member_count,
linked_chat_id: sgfi.linked_chat_id,
invite_link: sgfi.invite_link.map(|il| ChannelInviteLink {
invite_link: il.invite_link,
name: il.name,
is_primary: il.is_primary,
is_revoked: il.is_revoked,
}),
minithumbnail: photo.map(|p| p.data),
usernames: sg.usernames.map(|u| ChannelUsernameInfo {
active: u.active_usernames,
inactive: u.disabled_usernames,
editable: u.editable_username,
}),
};
chat_vec.push(channel_info);
progress.inc(1);
}
println!("Fetched info for {} channels", chat_vec.len());
let data = TotalData {
total: chats.chat_ids.len(),
channels: chat_vec,
};
progress.finish_and_clear();
let data = serde_json::to_string_pretty(&data).unwrap();
let mut encoder = ZlibEncoder::new(Vec::new(), flate2::Compression::best());
std::io::copy(&mut data.as_bytes(), &mut encoder).unwrap();
let compressed_data = encoder.finish().unwrap();
std::fs::write("channels.json", &compressed_data).unwrap();
println!("Written to channels.json, {} bytes", compressed_data.len());
// Tell the client to close
functions::close(client_id).await.unwrap();
println!("Client closed");
// Handle the authorization state to wait for the "Closed" state
handle_authorization_state(client_id, auth_rx, run_flag.clone()).await;
// Wait for the previously spawned task to end the execution
handle.await.unwrap();
println!("Exiting...");
}