Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add inline keyboard for help command #288

Merged
merged 5 commits into from
Dec 26, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bot.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod commands;
pub mod handler;
pub mod telegram_client;
pub mod update_handler;
169 changes: 165 additions & 4 deletions src/bot/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ use diesel::PgConnection;
use frankenstein::Chat;
use frankenstein::ChatType;
use frankenstein::Message;
use frankenstein::SendMessageParams;
use std::str::FromStr;
use typed_builder::TypedBuilder;

pub use get_filter::GetFilter;
pub use get_global_filter::GetGlobalFilter;
pub use get_global_template::GetGlobalTemplate;
pub use get_template::GetTemplate;
pub use get_timezone::GetTimezone;
pub use help::Help;
pub use help_command_info::HelpCommandInfo;
pub use info::Info;
pub use list_subscriptions::ListSubscriptions;
pub use remove_filter::RemoveFilter;
Expand All @@ -44,6 +47,7 @@ pub mod get_global_template;
pub mod get_template;
pub mod get_timezone;
pub mod help;
pub mod help_command_info;
pub mod info;
pub mod list_subscriptions;
pub mod remove_filter;
Expand Down Expand Up @@ -81,9 +85,11 @@ impl From<Chat> for NewTelegramChat {
}
}

#[derive(Debug)]
pub enum BotCommand {
UnknownCommand(String),
Help,
HelpCommandInfo(String),
Subscribe(String),
Unsubscribe(String),
ListSubscriptions,
Expand Down Expand Up @@ -112,6 +118,10 @@ impl FromStr for BotCommand {
fn from_str(command: &str) -> Result<Self, Self::Err> {
let bot_command = if !command.starts_with('/') {
BotCommand::UnknownCommand(command.to_string())
} else if command.starts_with(HelpCommandInfo::command()) {
let args = parse_args(HelpCommandInfo::command(), command);

BotCommand::HelpCommandInfo(args)
} else if command.starts_with(Help::command()) {
BotCommand::Help
} else if command.starts_with(Subscribe::command()) {
Expand Down Expand Up @@ -200,8 +210,13 @@ fn parse_args(command: &str, command_with_args: &str) -> String {
}
}

pub enum Response {
Simple(String),
Params(SendMessageParams),
}

pub trait Command {
fn response(&self) -> String;
fn response(&self) -> Response;

fn execute(&self, message: &Message) {
info!(
Expand All @@ -210,19 +225,34 @@ pub trait Command {
message.text.as_ref().unwrap()
);

let text = self.response();
self.reply_to_message(message, text)
match self.response() {
Response::Simple(raw_message) => self.reply_to_message(message, raw_message),
Response::Params(params) => self.send_message(params),
}
}

fn reply_to_message(&self, message: &Message, text: String) {
if let Err(error) =
self.api()
.reply_with_text_message(message.chat.id, text, Some(message.message_id))
{
error!("Failed to reply to update {:?} {:?}", error, message);
error!("Failed to reply to a message {:?} {:?}", error, message);
}
}

fn send_message(&self, send_message_params: SendMessageParams) {
if let Err(error) = self.api().send_message_with_params(&send_message_params) {
pxp9 marked this conversation as resolved.
Show resolved Hide resolved
error!(
"Failed to send a message {:?} {:?}",
error, send_message_params
);
}
}

fn remove_message(&self, message: &Message) {
self.api().remove_message(message)
}

fn fetch_db_connection(
&self,
) -> Result<PooledConnection<ConnectionManager<PgConnection>>, String> {
Expand Down Expand Up @@ -286,3 +316,134 @@ pub trait Command {
Ok(filter_words)
}
}

#[derive(TypedBuilder)]
pub struct CommandProcessor {
message: Message,
command: BotCommand,
}

impl CommandProcessor {
pub fn process(&self) {
match &self.command {
BotCommand::Subscribe(args) => Subscribe::builder()
.message(self.message.clone())
.args(args.to_string())
.build()
.run(),

BotCommand::Help => Help::builder().message(self.message.clone()).build().run(),

BotCommand::Unsubscribe(args) => Unsubscribe::builder()
.message(self.message.clone())
.args(args.to_string())
.build()
.run(),

BotCommand::ListSubscriptions => ListSubscriptions::builder()
.message(self.message.clone())
.build()
.run(),

BotCommand::Start => Start::builder().message(self.message.clone()).build().run(),

BotCommand::SetTimezone(args) => SetTimezone::builder()
.message(self.message.clone())
.args(args.to_string())
.build()
.run(),

BotCommand::GetTimezone => GetTimezone::builder()
.message(self.message.clone())
.build()
.run(),

BotCommand::SetFilter(args) => SetFilter::builder()
.message(self.message.clone())
.args(args.to_string())
.build()
.run(),

BotCommand::GetFilter(args) => GetFilter::builder()
.message(self.message.clone())
.args(args.to_string())
.build()
.run(),

BotCommand::RemoveFilter(args) => RemoveFilter::builder()
.message(self.message.clone())
.args(args.to_string())
.build()
.run(),

BotCommand::SetTemplate(args) => SetTemplate::builder()
.message(self.message.clone())
.args(args.to_string())
.build()
.run(),

BotCommand::GetTemplate(args) => GetTemplate::builder()
.message(self.message.clone())
.args(args.to_string())
.build()
.run(),

BotCommand::RemoveTemplate(args) => RemoveTemplate::builder()
.message(self.message.clone())
.args(args.to_string())
.build()
.run(),

BotCommand::SetGlobalTemplate(args) => SetGlobalTemplate::builder()
.message(self.message.clone())
.args(args.to_string())
.build()
.run(),

BotCommand::RemoveGlobalTemplate => RemoveGlobalTemplate::builder()
.message(self.message.clone())
.build()
.run(),

BotCommand::GetGlobalTemplate => GetGlobalTemplate::builder()
.message(self.message.clone())
.build()
.run(),

BotCommand::SetGlobalFilter(args) => SetGlobalFilter::builder()
.message(self.message.clone())
.args(args.to_string())
.build()
.run(),

BotCommand::GetGlobalFilter => GetGlobalFilter::builder()
.message(self.message.clone())
.build()
.run(),

BotCommand::RemoveGlobalFilter => RemoveGlobalFilter::builder()
.message(self.message.clone())
.build()
.run(),

BotCommand::Info => Info::builder().message(self.message.clone()).build().run(),

BotCommand::SetContentFields(args) => SetContentFields::builder()
.message(self.message.clone())
.args(args.to_string())
.build()
.run(),

BotCommand::UnknownCommand(args) => UnknownCommand::builder()
.message(self.message.clone())
.args(args.to_string())
.build()
.run(),
BotCommand::HelpCommandInfo(args) => HelpCommandInfo::builder()
.message(self.message.clone())
.args(args.to_string())
.build()
.run(),
};
}
}
9 changes: 6 additions & 3 deletions src/bot/commands/get_filter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::Command;
use super::Message;
use super::Response;
use diesel::PgConnection;
use typed_builder::TypedBuilder;

Expand Down Expand Up @@ -32,11 +33,13 @@ impl GetFilter {
}

impl Command for GetFilter {
fn response(&self) -> String {
match self.fetch_db_connection() {
fn response(&self) -> Response {
let response = match self.fetch_db_connection() {
Ok(mut connection) => self.get_filter(&mut connection),

Err(error_message) => error_message,
}
};

Response::Simple(response)
}
}
9 changes: 6 additions & 3 deletions src/bot/commands/get_global_filter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::Command;
use super::Message;
use super::Response;
use crate::db::telegram;
use diesel::PgConnection;
use typed_builder::TypedBuilder;
Expand Down Expand Up @@ -34,10 +35,12 @@ impl GetGlobalFilter {
}

impl Command for GetGlobalFilter {
fn response(&self) -> String {
match self.fetch_db_connection() {
fn response(&self) -> Response {
let response = match self.fetch_db_connection() {
Ok(mut connection) => self.get_global_template(&mut connection),
Err(error_message) => error_message,
}
};

Response::Simple(response)
}
}
9 changes: 6 additions & 3 deletions src/bot/commands/get_global_template.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::Command;
use super::Message;
use super::Response;
use crate::db::telegram;
use diesel::PgConnection;
use typed_builder::TypedBuilder;
Expand Down Expand Up @@ -32,10 +33,12 @@ impl GetGlobalTemplate {
}

impl Command for GetGlobalTemplate {
fn response(&self) -> String {
match self.fetch_db_connection() {
fn response(&self) -> Response {
let response = match self.fetch_db_connection() {
Ok(mut connection) => self.get_global_template(&mut connection),
Err(error_message) => error_message,
}
};

Response::Simple(response)
}
}
9 changes: 6 additions & 3 deletions src/bot/commands/get_template.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::Command;
use super::Message;
use super::Response;
use diesel::PgConnection;
use typed_builder::TypedBuilder;

Expand Down Expand Up @@ -32,10 +33,12 @@ impl GetTemplate {
}

impl Command for GetTemplate {
fn response(&self) -> String {
match self.fetch_db_connection() {
fn response(&self) -> Response {
let response = match self.fetch_db_connection() {
Ok(mut connection) => self.get_template(&mut connection),
Err(error_message) => error_message,
}
};

Response::Simple(response)
}
}
9 changes: 6 additions & 3 deletions src/bot/commands/get_timezone.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::Command;
use super::Message;
use super::Response;
use crate::db::telegram;
use diesel::PgConnection;
use typed_builder::TypedBuilder;
Expand Down Expand Up @@ -32,10 +33,12 @@ impl GetTimezone {
}

impl Command for GetTimezone {
fn response(&self) -> String {
match self.fetch_db_connection() {
fn response(&self) -> Response {
let response = match self.fetch_db_connection() {
Ok(mut connection) => self.get_timezone(&mut connection),
Err(error_message) => error_message,
}
};

Response::Simple(response)
}
}
Loading