98 lines
2.7 KiB
Rust
98 lines
2.7 KiB
Rust
use frankenstein::{
|
|
Api, GetUpdatesParams, KeyboardButton, ReplyKeyboardMarkup, ReplyMarkup, TelegramApi,
|
|
};
|
|
use futures::Future;
|
|
use std::{error::Error, io, thread::JoinHandle};
|
|
|
|
pub trait Selector {
|
|
fn send_for_review(&mut self, message: String) -> Result<(), Box<dyn Error>>;
|
|
fn collect_selected_samples(&mut self) -> Vec<String>;
|
|
}
|
|
|
|
// pub trait Selector {
|
|
// type Error;
|
|
// type Response: Future<Output = Result<bool, Self::Error>>;
|
|
|
|
// fn review(self, data: String) -> Self::Response;
|
|
// }
|
|
|
|
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) -> Result<(), 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);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
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) -> Result<(), 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()),
|
|
]),
|
|
));
|
|
|
|
self.client
|
|
.send_message(&message_def)
|
|
.expect("TODO handle this properly (doesn't implement std error for some reason)");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
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,
|
|
}
|
|
}
|
|
}
|