Skip to content

Commit

Permalink
CommandsKeyboard improvements (#302)
Browse files Browse the repository at this point in the history
- add `commands` button to `/start` message
- disable `/commands` in groups and channels
  • Loading branch information
ayrat555 authored Feb 5, 2023
1 parent 11f0f30 commit f4e74c9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/bot/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,10 +557,20 @@ impl CommandProcessor {

fn process_regular_command(&self) {
match BotCommand::from_str(&self.text).unwrap() {
BotCommand::CommandsKeyboard => CommandsKeyboard::builder()
.message(self.message.clone())
.build()
.run(),
BotCommand::CommandsKeyboard => {
if let ChatType::Private = self.message.chat.type_field {
CommandsKeyboard::builder()
.message(self.message.clone())
.build()
.run()
} else {
UnknownCommand::builder()
.message(self.message.clone())
.args("/commands".to_string())
.build()
.run()
}
}

BotCommand::Subscribe(args) => Subscribe::builder()
.message(self.message.clone())
Expand Down
36 changes: 35 additions & 1 deletion src/bot/commands/start.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use super::Command;
use super::CommandsKeyboard;
use super::Message;
use super::Response;
use frankenstein::ChatType;
use frankenstein::InlineKeyboardButton;
use frankenstein::InlineKeyboardMarkup;
use frankenstein::ReplyMarkup;
use frankenstein::SendMessageParams;
use typed_builder::TypedBuilder;

static START: &str =
Expand Down Expand Up @@ -33,6 +39,34 @@ impl Start {

impl Command for Start {
fn response(&self) -> Response {
Response::Simple(START.to_string())
let response = START.to_string();

if let ChatType::Private = self.message.chat.type_field {
let mut buttons: Vec<Vec<InlineKeyboardButton>> = Vec::new();
let mut row: Vec<InlineKeyboardButton> = Vec::new();

let button = InlineKeyboardButton::builder()
.text("Commands")
.callback_data(CommandsKeyboard::command())
.build();

row.push(button);
buttons.push(row);

let keyboard = InlineKeyboardMarkup::builder()
.inline_keyboard(buttons)
.build();

let params = SendMessageParams::builder()
.chat_id(self.message.chat.id)
.disable_web_page_preview(true)
.text(response)
.reply_markup(ReplyMarkup::InlineKeyboardMarkup(keyboard))
.build();

Response::Params(params)
} else {
Response::Simple(START.to_string())
}
}
}

0 comments on commit f4e74c9

Please sign in to comment.