Skip to content

Commit

Permalink
Replace heart with emoji (#244)
Browse files Browse the repository at this point in the history
* Enable html in templates (#241)


Co-authored-by: Ayrat Badykov <ayratin555@gmail.com>

* replace <3 with emoji

Telegram can not recogize it

* fix clippy

Co-authored-by: athulkrishnaaei <athulkrishnakdgr@gmail.com>
  • Loading branch information
ayrat555 and athulkrishnaaei authored Aug 28, 2022
1 parent 1a76ff1 commit 36c842b
Show file tree
Hide file tree
Showing 26 changed files with 94 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "el_monitorro"
version = "0.9.0"
authors = ["Ayrat Badykov <ayratin555@gmail.com>"]
edition = "2018"
edition = "2021"
rust-version = "1.62"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
27 changes: 11 additions & 16 deletions src/bot/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use diesel::PgConnection;
use frankenstein::Chat;
use frankenstein::ChatType;
use frankenstein::Message;
use frankenstein::SendMessageParams;
use frankenstein::TelegramApi;

pub mod get_filter;
pub mod get_global_filter;
Expand Down Expand Up @@ -60,8 +58,12 @@ impl From<Chat> for NewTelegramChat {
}

pub trait Command {
fn response(&self, db_pool: Pool<ConnectionManager<PgConnection>>, message: &Message)
-> String;
fn response(
&self,
db_pool: Pool<ConnectionManager<PgConnection>>,
message: &Message,
api: &Api,
) -> String;

fn execute(&self, db_pool: Pool<ConnectionManager<PgConnection>>, api: Api, message: Message) {
info!(
Expand All @@ -70,23 +72,16 @@ pub trait Command {
message.text.as_ref().unwrap()
);

let text = self.response(db_pool, &message);
let text = self.response(db_pool, &message, &api);

self.reply_to_message(api, message, text)
}

fn reply_to_message(&self, api: Api, message: Message, text: String) {
let send_message_params = SendMessageParams::builder()
.chat_id(message.chat.id)
.text(text)
.reply_to_message_id(message.message_id)
.build();

if let Err(err) = api.send_message(&send_message_params) {
error!(
"Failed to send a message {:?}: {:?}",
err, send_message_params
);
if let Err(error) =
api.reply_with_text_message(message.chat.id, text, Some(message.message_id))
{
error!("Failed to reply to update {:?} {:?}", error, message);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/bot/commands/get_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl Command for GetFilter {
&self,
db_pool: Pool<ConnectionManager<PgConnection>>,
message: &Message,
_api: &Api,
) -> String {
match self.fetch_db_connection(db_pool) {
Ok(connection) => {
Expand Down
1 change: 1 addition & 0 deletions src/bot/commands/get_global_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl Command for GetGlobalFilter {
&self,
db_pool: Pool<ConnectionManager<PgConnection>>,
message: &Message,
_api: &Api,
) -> String {
match self.fetch_db_connection(db_pool) {
Ok(connection) => self.get_global_template(&connection, message),
Expand Down
1 change: 1 addition & 0 deletions src/bot/commands/get_global_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl Command for GetGlobalTemplate {
&self,
db_pool: Pool<ConnectionManager<PgConnection>>,
message: &Message,
_api: &Api,
) -> String {
match self.fetch_db_connection(db_pool) {
Ok(connection) => self.get_global_template(&connection, message),
Expand Down
1 change: 1 addition & 0 deletions src/bot/commands/get_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl Command for GetTemplate {
&self,
db_pool: Pool<ConnectionManager<PgConnection>>,
message: &Message,
_api: &Api,
) -> String {
match self.fetch_db_connection(db_pool) {
Ok(connection) => {
Expand Down
1 change: 1 addition & 0 deletions src/bot/commands/get_timezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl Command for GetTimezone {
&self,
db_pool: Pool<ConnectionManager<PgConnection>>,
message: &Message,
_api: &Api,
) -> String {
match self.fetch_db_connection(db_pool) {
Ok(connection) => self.get_timezone(&connection, message),
Expand Down
1 change: 1 addition & 0 deletions src/bot/commands/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl Command for Help {
&self,
_db_pool: Pool<ConnectionManager<PgConnection>>,
_message: &Message,
_api: &Api,
) -> String {
HELP.to_string()
}
Expand Down
3 changes: 2 additions & 1 deletion src/bot/commands/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Command for Info {
message.text.as_ref().unwrap()
);

let text = self.response(db_pool, &message);
let text = self.response(db_pool, &message, &api);

self.reply_to_message(api, message, text)
} else {
Expand All @@ -87,6 +87,7 @@ impl Command for Info {
&self,
db_pool: Pool<ConnectionManager<PgConnection>>,
message: &Message,
_api: &Api,
) -> String {
match self.fetch_db_connection(db_pool) {
Ok(connection) => self.info(&connection, message),
Expand Down
1 change: 1 addition & 0 deletions src/bot/commands/list_subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl Command for ListSubscriptions {
&self,
db_pool: Pool<ConnectionManager<PgConnection>>,
message: &Message,
_api: &Api,
) -> String {
match self.fetch_db_connection(db_pool) {
Ok(connection) => self.list_subscriptions(&connection, message),
Expand Down
1 change: 1 addition & 0 deletions src/bot/commands/remove_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl Command for RemoveFilter {
&self,
db_pool: Pool<ConnectionManager<PgConnection>>,
message: &Message,
_api: &Api,
) -> String {
match self.fetch_db_connection(db_pool) {
Ok(connection) => {
Expand Down
1 change: 1 addition & 0 deletions src/bot/commands/remove_global_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl Command for RemoveGlobalFilter {
&self,
db_pool: Pool<ConnectionManager<PgConnection>>,
message: &Message,
_api: &Api,
) -> String {
match self.fetch_db_connection(db_pool) {
Ok(connection) => self.remove_global_filter(&connection, message),
Expand Down
1 change: 1 addition & 0 deletions src/bot/commands/remove_global_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl Command for RemoveGlobalTemplate {
&self,
db_pool: Pool<ConnectionManager<PgConnection>>,
message: &Message,
_api: &Api,
) -> String {
match self.fetch_db_connection(db_pool) {
Ok(connection) => self.remove_global_template(&connection, message),
Expand Down
1 change: 1 addition & 0 deletions src/bot/commands/remove_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl Command for RemoveTemplate {
&self,
db_pool: Pool<ConnectionManager<PgConnection>>,
message: &Message,
_api: &Api,
) -> String {
match self.fetch_db_connection(db_pool) {
Ok(connection) => {
Expand Down
3 changes: 2 additions & 1 deletion src/bot/commands/set_content_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Command for SetContentFields {
message.text.as_ref().unwrap()
);

let text = self.response(db_pool, &message);
let text = self.response(db_pool, &message, &api);

self.reply_to_message(api, message, text)
} else {
Expand All @@ -91,6 +91,7 @@ impl Command for SetContentFields {
&self,
db_pool: Pool<ConnectionManager<PgConnection>>,
message: &Message,
_api: &Api,
) -> String {
match self.fetch_db_connection(db_pool) {
Ok(connection) => {
Expand Down
1 change: 1 addition & 0 deletions src/bot/commands/set_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl Command for SetFilter {
&self,
db_pool: Pool<ConnectionManager<PgConnection>>,
message: &Message,
_api: &Api,
) -> String {
match self.fetch_db_connection(db_pool) {
Ok(connection) => {
Expand Down
1 change: 1 addition & 0 deletions src/bot/commands/set_global_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl Command for SetGlobalFilter {
&self,
db_pool: Pool<ConnectionManager<PgConnection>>,
message: &Message,
_api: &Api,
) -> String {
match self.fetch_db_connection(db_pool) {
Ok(connection) => {
Expand Down
15 changes: 9 additions & 6 deletions src/bot/commands/set_global_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ impl SetGlobalTemplate {

fn set_global_template(
&self,
api: &Api,
db_connection: &PgConnection,
message: &Message,
template: String,
Expand All @@ -32,15 +33,16 @@ impl SetGlobalTemplate {
};

let example = match render_template_example(&template) {
Ok(example) => example,
Ok(example) => format!("Your messages will look like:\n\n{}", example),
Err(_) => return "The template is invalid".to_string(),
};

if api.send_text_message(message.chat.id, example).is_err() {
return "The template is invalid".to_string();
}

match telegram::set_global_template(db_connection, &chat, Some(template)) {
Ok(_) => format!(
"The global template was updated. Your messages will look like:\n\n{}",
example
),
Ok(_) => "The global template was updated".to_string(),
Err(_) => "Failed to update the template".to_string(),
}
}
Expand All @@ -55,12 +57,13 @@ impl Command for SetGlobalTemplate {
&self,
db_pool: Pool<ConnectionManager<PgConnection>>,
message: &Message,
api: &Api,
) -> String {
match self.fetch_db_connection(db_pool) {
Ok(connection) => {
let text = message.text.as_ref().unwrap();
let argument = self.parse_argument(text);
self.set_global_template(&connection, message, argument)
self.set_global_template(api, &connection, message, argument)
}
Err(error_message) => error_message,
}
Expand Down
15 changes: 9 additions & 6 deletions src/bot/commands/set_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ impl SetTemplate {

fn set_template(
&self,
api: &Api,
db_connection: &PgConnection,
message: &Message,
params: String,
Expand All @@ -41,15 +42,16 @@ impl SetTemplate {
};

let example = match render_template_example(template) {
Ok(example) => example,
Ok(example) => format!("Your messages will look like:\n\n{}", example),
Err(_) => return "The template is invalid".to_string(),
};

if api.send_text_message(message.chat.id, example).is_err() {
return "The template is invalid".to_string();
}

match telegram::set_template(db_connection, &subscription, Some(template.to_string())) {
Ok(_) => format!(
"The template was updated. Your messages will look like:\n\n{}",
example
),
Ok(_) => "The template was updated".to_string(),
Err(_) => "Failed to update the template".to_string(),
}
}
Expand All @@ -64,12 +66,13 @@ impl Command for SetTemplate {
&self,
db_pool: Pool<ConnectionManager<PgConnection>>,
message: &Message,
api: &Api,
) -> String {
match self.fetch_db_connection(db_pool) {
Ok(connection) => {
let text = message.text.as_ref().unwrap();
let argument = self.parse_argument(text);
self.set_template(&connection, message, argument)
self.set_template(api, &connection, message, argument)
}
Err(error_message) => error_message,
}
Expand Down
1 change: 1 addition & 0 deletions src/bot/commands/set_timezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl Command for SetTimezone {
&self,
db_pool: Pool<ConnectionManager<PgConnection>>,
message: &Message,
_api: &Api,
) -> String {
match self.fetch_db_connection(db_pool) {
Ok(connection) => {
Expand Down
1 change: 1 addition & 0 deletions src/bot/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl Command for Start {
&self,
_db_pool: Pool<ConnectionManager<PgConnection>>,
_message: &Message,
_api: &Api,
) -> String {
START.to_string()
}
Expand Down
1 change: 1 addition & 0 deletions src/bot/commands/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ impl Command for Subscribe {
&self,
db_pool: Pool<ConnectionManager<PgConnection>>,
message: &Message,
_api: &Api,
) -> String {
match self.fetch_db_connection(db_pool) {
Ok(connection) => {
Expand Down
3 changes: 2 additions & 1 deletion src/bot/commands/unknown_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ impl Command for UnknownCommand {
&self,
_db_pool: Pool<ConnectionManager<PgConnection>>,
message: &Message,
_api: &Api,
) -> String {
match message.chat.type_field {
ChatType::Private => UNKNOWN_COMMAND_PRIVATE.to_string(),
Expand All @@ -53,7 +54,7 @@ impl Command for UnknownCommand {
);
}

let text = self.response(db_pool, &message);
let text = self.response(db_pool, &message, &api);

if !text.is_empty() {
self.reply_to_message(api, message, text);
Expand Down
1 change: 1 addition & 0 deletions src/bot/commands/unsubscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl Command for Unsubscribe {
&self,
db_pool: Pool<ConnectionManager<PgConnection>>,
message: &Message,
_api: &Api,
) -> String {
match self.fetch_db_connection(db_pool) {
Ok(connection) => {
Expand Down
28 changes: 24 additions & 4 deletions src/bot/telegram_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::config::Config;
use frankenstein::AllowedUpdate;
use frankenstein::ErrorResponse;
use frankenstein::GetUpdatesParams;
use frankenstein::ParseMode;
use frankenstein::SendMessageParams;
use frankenstein::TelegramApi;
use frankenstein::Update;
Expand Down Expand Up @@ -78,10 +79,29 @@ impl Api {
}

pub fn send_text_message(&self, chat_id: i64, message: String) -> Result<(), Error> {
let send_message_params = SendMessageParams::builder()
.chat_id(chat_id)
.text(message)
.build();
self.reply_with_text_message(chat_id, message, None)
}

pub fn reply_with_text_message(
&self,
chat_id: i64,
message: String,
message_id: Option<i32>,
) -> Result<(), Error> {
let send_message_params = match message_id {
None => SendMessageParams::builder()
.chat_id(chat_id)
.text(message)
.parse_mode(ParseMode::Html)
.build(),

Some(message_id_value) => SendMessageParams::builder()
.chat_id(chat_id)
.text(message)
.parse_mode(ParseMode::Html)
.reply_to_message_id(message_id_value)
.build(),
};

match self.send_message(&send_message_params) {
Ok(_) => Ok(()),
Expand Down
Loading

0 comments on commit 36c842b

Please sign in to comment.