90 lines
2.5 KiB
Rust
90 lines
2.5 KiB
Rust
|
use frankenstein::{
|
||
|
Api, GetUpdatesParams, KeyboardButton, ReplyKeyboardMarkup, ReplyMarkup, TelegramApi,
|
||
|
};
|
||
|
use std::{error::Error, io, thread::JoinHandle};
|
||
|
|
||
|
pub trait Selector {
|
||
|
fn send_for_review(&mut self, message: String) -> Option<Box<dyn Error>>;
|
||
|
fn collect_selected_samples(&mut self) -> Vec<String>;
|
||
|
}
|
||
|
|
||
|
pub struct TelegramSelector {
|
||
|
client: frankenstein::Api,
|
||
|
dest_chat_id: String,
|
||
|
listener_handle: Option<JoinHandle<()>>,
|
||
|
}
|
||
|
|
||
|
pub struct ConsoleSelector {
|
||
|
selected_samples: Vec<String>,
|
||
|
}
|
||
|
|
||
|
impl Selector for ConsoleSelector {
|
||
|
fn send_for_review(&mut self, message: String) -> Option<Box<dyn Error>> {
|
||
|
println!("generated sample [y+enter to accept]: {}", &message);
|
||
|
let mut choice = String::new();
|
||
|
io::stdin().read_line(&mut choice).expect("cum");
|
||
|
if choice.to_lowercase().contains("y") {
|
||
|
println!("accepted");
|
||
|
self.selected_samples.push(message);
|
||
|
}
|
||
|
return None;
|
||
|
}
|
||
|
|
||
|
fn collect_selected_samples(&mut self) -> Vec<String> {
|
||
|
let cloned_samples = self.selected_samples.to_owned();
|
||
|
self.selected_samples = Vec::new();
|
||
|
cloned_samples
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl ConsoleSelector {
|
||
|
pub fn new() -> ConsoleSelector {
|
||
|
Self {
|
||
|
selected_samples: Vec::new(),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
const KEEP_BUTTON: &str = "Keep";
|
||
|
const TOSS_BUTTON: &str = "Toss";
|
||
|
|
||
|
impl Selector for TelegramSelector {
|
||
|
fn send_for_review(&mut self, message: String) -> Option<Box<dyn Error>> {
|
||
|
todo!();
|
||
|
if !self.listener_handle.is_none() {
|
||
|
todo!();
|
||
|
}
|
||
|
let mut message_def = frankenstein::SendMessageParams::new(
|
||
|
frankenstein::ChatId::String(self.dest_chat_id.clone()),
|
||
|
message,
|
||
|
);
|
||
|
message_def.reply_markup = Some(ReplyMarkup::ReplyKeyboardMarkup(
|
||
|
ReplyKeyboardMarkup::new(vec![
|
||
|
KeyboardButton::new(KEEP_BUTTON.to_string()),
|
||
|
KeyboardButton::new(TOSS_BUTTON.to_string()),
|
||
|
]),
|
||
|
));
|
||
|
let result = match self.client.send_message(&message_def) {
|
||
|
Ok(_) => None,
|
||
|
Err(_) => todo!(),
|
||
|
};
|
||
|
|
||
|
result
|
||
|
}
|
||
|
|
||
|
fn collect_selected_samples(&mut self) -> Vec<String> {
|
||
|
todo!()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl TelegramSelector {
|
||
|
pub fn new(token: String, dest_chat_id: String) -> TelegramSelector {
|
||
|
let api = Api::new(&token);
|
||
|
Self {
|
||
|
client: api,
|
||
|
dest_chat_id: dest_chat_id,
|
||
|
listener_handle: None,
|
||
|
}
|
||
|
}
|
||
|
}
|