Skip to content

Commit

Permalink
Publish as lib and add channels
Browse files Browse the repository at this point in the history
  • Loading branch information
felixgborrego committed Jun 4, 2021
1 parent 49c6424 commit a56bb89
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 114 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "telegram-tracker"
version = "0.1.5"
name = "telegram_tracker"
version = "0.1.6"
authors = ["Felix <felix.g.borrego@gmail.com>"]
readme = "README.md"
edition = "2018"
Expand All @@ -15,4 +15,5 @@ colored = "1.9"
regex = "1"
clap = "3.0.0-beta.2"
chrono = "0.4"
futures = "0.3"
futures = "0.3"
#tokio = { version = "1.5.0", features = ["full"] }
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ RUN cmake cmake --install .



FROM rust as telegram-tracker-builder
FROM rust as telegram_tracker-builder
WORKDIR app
# Build dependencies
COPY Cargo.toml /app/Cargo.toml
Expand All @@ -34,7 +34,7 @@ FROM debian:9 as runtime
WORKDIR app
RUN apt -y update && apt install -y g++ ccache openssl && rm -rf /var/lib/apt/lists/*
ENV LD_LIBRARY_PATH="/app/:${LD_LIBRARY_PATH}"
COPY --from=telegram-tracker-builder /app/target/release/telegram-tracker /app
COPY --from=telegram_tracker-builder /app/target/release/telegram_tracker /app
COPY --from=tdlib-builder /td/build/libtd* /app

ENTRYPOINT ["/app/telegram-tracker"]
ENTRYPOINT ["/app/telegram_tracker"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ LD_LIBRARY_PATH=lib cargo build --release
```

* Build with Docker
```docker build -t telegram-tracker:0.1.5 .```
```docker build -t telegram_tracker:0.1.6 .```

## Run
```
Expand Down
40 changes: 0 additions & 40 deletions src/forward_stdin.rs

This file was deleted.

85 changes: 85 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use std::sync::mpsc::SyncSender;

use chrono::{DateTime, Utc};
use log::{debug, error, info};
use rtdlib::types::{FormattedText, InputMessageContent, SendMessage};
use rtdlib::types::InputMessageText;
use telegram_client::api::Api;

mod telegram;
mod tgfn;
mod thelp;

pub fn test(s: String) {
println!("test {}", s);
}

#[derive(Debug, Clone)]
pub struct TelegramMessage {
pub event_info: String,
pub msg_text: String,
pub chat_id: i64,
pub message_id: i64,
pub sender_id: i64,
pub sent_datetime: DateTime<Utc>,
}

impl TelegramMessage {
pub fn msg_lower_case(&self) -> String {
self.msg_text.to_lowercase()
}
}
#[derive(Debug, Clone)]
pub struct TelegramConfig {
pub phone: String,
pub telegram_api_id: String,
pub telegram_api_hash: String,
pub print_outgoing: bool,
pub follow_channel: Option<i64>,
pub send_notifications_to_channel: Option<i64>,
}

pub struct TelegramTrackerClient {
api: Api,
config: TelegramConfig,
}

impl TelegramTrackerClient {
pub fn new(
config: TelegramConfig,
sender: SyncSender<TelegramMessage>,
) -> TelegramTrackerClient {
let api = telegram::start(
config.phone.to_owned(),
config.telegram_api_id.to_owned(),
config.telegram_api_hash.to_owned(),
config.print_outgoing.to_owned(),
config.follow_channel.to_owned(),
sender,
);
TelegramTrackerClient { api, config }
}

pub fn send(&self, msg_to_send: &String) {
info!("SENDING >> 💬 {}", msg_to_send);
if let Some(chanel_id) = self.config.send_notifications_to_channel {
let msg_content = InputMessageContent::InputMessageText(
InputMessageText::builder()
.text(FormattedText::builder().text(msg_to_send).build())
.clear_draft(true)
.build(),
);

let msg: SendMessage = SendMessage::builder()
.chat_id(chanel_id)
.input_message_content(msg_content)
.build();


match self.api.send(msg) {
Ok(ok) => debug!("Message forwarded to Telegram {:?}: {} {}", ok,chanel_id, msg_to_send),
Err(err) => error!("Unable forward to Telegram {:?} ", err),
};
}
}
}
41 changes: 23 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use clap::{AppSettings, Clap};
use log::LevelFilter;
use simple_logger::SimpleLogger;
use std::thread;
mod forward_stdin;
use std::sync::mpsc::sync_channel;
use telegram_tracker::TelegramMessage;

mod telegram;
mod tgfn;
mod thelp;

#[derive(Clap)]
#[clap(
version = "0.1.5",
version = "0.1.6",
author = "Felix G. Borrego <felix.g.borrego@gmail.com>"
)]
#[clap(setting = AppSettings::ColoredHelp)]
Expand All @@ -24,8 +25,6 @@ struct Opts {
print_outgoing: String,
#[clap(long)]
follow_channel_id: Option<String>,
#[clap(long)]
forward_stdin_to_channel_id: Option<String>,
}

fn main() {
Expand All @@ -34,7 +33,7 @@ fn main() {
let opts: Opts = Opts::parse();

SimpleLogger::new()
.with_level(LevelFilter::Info)
.with_level(LevelFilter::Debug)
.with_module_level("telegram_client::api", LevelFilter::Info)
.with_module_level("telegram_client::handler", LevelFilter::Off)
.init()
Expand All @@ -45,11 +44,6 @@ fn main() {
.parse::<i64>()
.expect("Expected a valid chat_id")
});
let forward_channel_id = opts.forward_stdin_to_channel_id.map(|id| {
format!("-100{}", id)
.parse::<i64>()
.expect("Expected a valid chat_id")
});

if let Some(id) = channel_id {
println!(
Expand All @@ -63,19 +57,30 @@ fn main() {
);
}

let api = telegram::start(
let (message_sender, message_receiver) = sync_channel(10);
let _ = telegram::start(
opts.phone,
opts.telegram_api_id,
opts.telegram_api_hash,
opts.print_outgoing.parse().expect("It must be a bool"),
channel_id,
message_sender,
);

if let Some(channel_id) = forward_channel_id {
println!("Stdin to Telegram channel {} is ENABLED", channel_id);
forward_stdin::forward_stdin_to_channel_id(api, channel_id);
} else {
println!("Stdin to Telegram is disabled.");
thread::park();
println!("Stdin to Telegram is disabled.");
for message in message_receiver {
on_new_message_in_room(message);
}
}

fn on_new_message_in_room(message: TelegramMessage) {
println!(
"### chat: {};sender_id: {};message_id: {};time: {:?};event_info: {}; msg:==> {}",
message.chat_id,
message.sender_id,
message.message_id,
message.sent_datetime.to_rfc3339(),
message.event_info,
message.msg_text
);
}
Loading

0 comments on commit a56bb89

Please sign in to comment.