Skip to content

Commit

Permalink
support topics (#310)
Browse files Browse the repository at this point in the history
* support topics

resolves #304

* big refactoring

* save thread_id

* fix most tests

* fix remaining tests

* update deps

* fix test
  • Loading branch information
ayrat555 authored Feb 18, 2023
1 parent fc93c66 commit 3d97657
Show file tree
Hide file tree
Showing 23 changed files with 417 additions and 555 deletions.
24 changes: 12 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE telegram_subscriptions DROP COLUMN thread_id;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE telegram_subscriptions ADD COLUMN thread_id integer;
16 changes: 11 additions & 5 deletions src/bot/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ pub trait Command {
let message_params = SimpleMessageParams::builder()
.message(text)
.chat_id(message.chat.id)
.message_thread_id(message.message_thread_id)
.build();

if let Err(error) = self.api().reply_with_text_message(&message_params) {
Expand Down Expand Up @@ -359,7 +360,7 @@ pub trait Command {
self.api().remove_message(message)
}

fn simple_keyboard(&self, message: String, back_command: String, chat_id: i64) -> Response {
fn simple_keyboard(&self, text: String, back_command: String, message: &Message) -> Response {
let mut buttons: Vec<Vec<InlineKeyboardButton>> = Vec::new();
let mut row: Vec<InlineKeyboardButton> = Vec::new();

Expand All @@ -376,13 +377,15 @@ pub trait Command {
.inline_keyboard(buttons)
.build();

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

params.message_thread_id = message.message_thread_id;

Response::Params(params)
}

Expand Down Expand Up @@ -441,7 +444,10 @@ pub trait Command {
chat_id: i64,
feed_id: i64,
) -> Option<TelegramSubscription> {
let telegram_subscription = NewTelegramSubscription { chat_id, feed_id };
let telegram_subscription = NewTelegramSubscription::builder()
.chat_id(chat_id)
.feed_id(feed_id)
.build();

telegram::find_subscription(db_connection, telegram_subscription)
}
Expand Down
8 changes: 6 additions & 2 deletions src/bot/commands/commands_keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,15 @@ impl CommandsKeyboard {
.inline_keyboard(buttons)
.build();

SendMessageParams::builder()
let mut params = SendMessageParams::builder()
.chat_id(self.message.chat.id)
.text("Select a command")
.reply_markup(ReplyMarkup::InlineKeyboardMarkup(keyboard))
.build()
.build();

params.message_thread_id = self.message.message_thread_id;

params
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/bot/commands/get_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl GetFilter {
ShowFeedKeyboard::command(),
subscription.external_id
),
self.message.chat.id,
&self.message,
)
} else {
Response::Simple(response)
Expand Down
2 changes: 1 addition & 1 deletion src/bot/commands/get_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Command for GetTemplate {
self.simple_keyboard(
response,
format!("{} {}", ShowFeedKeyboard::command(), self.args),
self.message.chat.id,
&self.message,
)
} else {
Response::Simple(response)
Expand Down
8 changes: 6 additions & 2 deletions src/bot/commands/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,16 @@ impl Help {
.inline_keyboard(buttons)
.build();

SendMessageParams::builder()
let mut params = SendMessageParams::builder()
.chat_id(self.message.chat.id)
.text("In private chats use keyboards to interact with the bot. Send /commands to display the keyboard. \n\nIn channels and groups you will have to type commands directly.\n\nJoin https://t.me/el_monitorro with your feedback, suggestions, found bugs, etc.\n\nSelect a command:")
.reply_markup(ReplyMarkup::InlineKeyboardMarkup(keyboard))
.disable_web_page_preview(true)
.build()
.build();

params.message_thread_id = self.message.message_thread_id;

params
}

pub fn button_row() -> Vec<InlineKeyboardButton> {
Expand Down
6 changes: 1 addition & 5 deletions src/bot/commands/help_command_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ impl Command for HelpCommandInfo {
fn response(&self) -> Response {
let help_for_command = self.command_info();

self.simple_keyboard(
help_for_command,
Help::command().to_string(),
self.message.chat.id,
)
self.simple_keyboard(help_for_command, Help::command().to_string(), &self.message)
}

fn send_message(&self, send_message_params: SendMessageParams) {
Expand Down
8 changes: 6 additions & 2 deletions src/bot/commands/list_subscriptions_keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,15 @@ impl ListSubscriptionsKeyboard {
.inline_keyboard(buttons)
.build();

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

params.message_thread_id = self.message.message_thread_id;

params
}

pub fn command() -> &'static str {
Expand Down
2 changes: 1 addition & 1 deletion src/bot/commands/remove_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Command for RemoveFilter {
self.simple_keyboard(
response,
format!("{} {}", ShowFeedKeyboard::command(), self.args),
self.message.chat.id,
&self.message,
)
} else {
Response::Simple(response)
Expand Down
2 changes: 1 addition & 1 deletion src/bot/commands/remove_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Command for RemoveTemplate {
self.simple_keyboard(
response,
format!("{} {}", ShowFeedKeyboard::command(), self.args),
self.message.chat.id,
&self.message,
)
} else {
Response::Simple(response)
Expand Down
8 changes: 6 additions & 2 deletions src/bot/commands/show_feed_keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,15 @@ impl ShowFeedKeyboard {
.inline_keyboard(buttons)
.build();

SendMessageParams::builder()
let mut params = SendMessageParams::builder()
.chat_id(self.message.chat.id)
.text(feed.link)
.reply_markup(ReplyMarkup::InlineKeyboardMarkup(keyboard))
.build()
.build();

params.message_thread_id = self.message.message_thread_id;

params
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/bot/commands/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ impl Subscribe {
telegram::create_chat(db_connection, (*self.message.chat.clone()).into()).unwrap();
let feed = feeds::create(db_connection, &self.args, feed_type).unwrap();

let new_telegram_subscription = NewTelegramSubscription {
chat_id: chat.id,
feed_id: feed.id,
};
let new_telegram_subscription = NewTelegramSubscription::builder()
.chat_id(chat.id)
.feed_id(feed.id)
.thread_id(self.message.message_thread_id)
.build();

self.check_if_subscription_exists(db_connection, new_telegram_subscription)?;
self.check_number_of_subscriptions(db_connection, chat.id)?;
Expand Down
4 changes: 3 additions & 1 deletion src/bot/commands/unknown_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ impl Command for UnknownCommand {
.inline_keyboard(buttons)
.build();

let params = SendMessageParams::builder()
let mut params = SendMessageParams::builder()
.chat_id(self.message.chat.id)
.text(text)
.reply_markup(ReplyMarkup::InlineKeyboardMarkup(keyboard))
.reply_to_message_id(message.message_id)
.build();

params.message_thread_id = message.message_thread_id;

self.send_message(params)
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/bot/commands/unsubscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ impl Unsubscribe {

let feed = feeds::find(db_connection, subscription.feed_id).unwrap();

let telegram_subscription = NewTelegramSubscription {
chat_id: self.message.chat.id,
feed_id: feed.id,
};
let telegram_subscription = NewTelegramSubscription::builder()
.chat_id(self.message.chat.id)
.feed_id(feed.id)
.build();

match telegram::remove_subscription(db_connection, telegram_subscription) {
Ok(_) => Ok(feed.link),
Expand All @@ -78,7 +78,7 @@ impl Command for Unsubscribe {
self.simple_keyboard(
response,
ListSubscriptionsKeyboard::command().to_string(),
self.message.chat.id,
&self.message,
)
} else {
Response::Simple(response)
Expand Down Expand Up @@ -120,10 +120,10 @@ mod unsubscribe_tests {
let chat = telegram::create_chat(connection, new_chat).unwrap();
let feed = feeds::create(connection, &link, "rss".to_string()).unwrap();

let new_subscription = NewTelegramSubscription {
feed_id: feed.id,
chat_id: chat.id,
};
let new_subscription = NewTelegramSubscription::builder()
.chat_id(chat.id)
.feed_id(feed.id)
.build();

telegram::create_subscription(connection, new_subscription).unwrap();

Expand Down
16 changes: 8 additions & 8 deletions src/bot/telegram_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub struct SimpleMessageParams {
reply_message_id: Option<i32>,
#[builder(default = false)]
preview_enabled: bool,
#[builder(default, setter(into))]
message_thread_id: Option<i32>,
}

impl Api {
Expand Down Expand Up @@ -116,19 +118,17 @@ impl Api {
&self,
simple_params: &SimpleMessageParams,
) -> Result<(), Error> {
let message_params = SendMessageParams::builder()
let mut message_params = SendMessageParams::builder()
.chat_id(simple_params.chat_id)
.text(simple_params.message.clone())
.disable_web_page_preview(!simple_params.preview_enabled)
.parse_mode(ParseMode::Html);

let send_message_params = match simple_params.reply_message_id {
None => message_params.build(),
.parse_mode(ParseMode::Html)
.build();

Some(message_id_value) => message_params.reply_to_message_id(message_id_value).build(),
};
message_params.reply_to_message_id = simple_params.reply_message_id;
message_params.message_thread_id = simple_params.message_thread_id;

self.send_message_with_params(&send_message_params)
self.send_message_with_params(&message_params)
}

pub fn send_message_with_params(
Expand Down
8 changes: 4 additions & 4 deletions src/db/feeds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,10 +792,10 @@ mod tests {
};
let chat = telegram::create_chat(connection, new_chat).unwrap();

let new_subscription = NewTelegramSubscription {
feed_id: feed.id,
chat_id: chat.id,
};
let new_subscription = NewTelegramSubscription::builder()
.chat_id(chat.id)
.feed_id(feed.id)
.build();

telegram::create_subscription(connection, new_subscription).unwrap()
}
Expand Down
Loading

0 comments on commit 3d97657

Please sign in to comment.