Now altering messages where possible
This commit is contained in:
@ -28,23 +28,28 @@ where
|
||||
let text = match msg.text() {
|
||||
Some(text) => text.trim(),
|
||||
None => {
|
||||
let msg = bot.send_message(msg.chat.id, no_text_message).await?;
|
||||
handler.previous = MessageIds::from(&msg);
|
||||
handler
|
||||
.previous
|
||||
.alter_message(&bot, no_text_message, None, None)
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
if text == "/cancel" {
|
||||
dialogue.exit().await?;
|
||||
bot.send_message(msg.chat.id, "Successfully cancelled")
|
||||
.reply_markup(deletion_markup())
|
||||
handler
|
||||
.previous
|
||||
.alter_message(&bot, "Successfully cancelled", deletion_markup(), None)
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Some(text) = check(&msg, &db, text).await? {
|
||||
let failure_message = bot.send_message(msg.chat.id, text).await?;
|
||||
handler.previous = MessageIds::from(&failure_message);
|
||||
handler
|
||||
.previous
|
||||
.alter_message(&bot, text, None, None)
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
|
@ -33,8 +33,9 @@ pub async fn get_existing_name(
|
||||
|
||||
if text == "/cancel" {
|
||||
dialogue.exit().await?;
|
||||
bot.send_message(msg.chat.id, "Successfully cancelled")
|
||||
.reply_markup(deletion_markup())
|
||||
handler
|
||||
.previous
|
||||
.alter_message(&bot, "Successfully cancelled", deletion_markup(), None)
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
@ -48,12 +49,10 @@ pub async fn get_existing_name(
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let previous = handler.previous;
|
||||
let func = handler.func.take().unwrap();
|
||||
drop(handler);
|
||||
let text = text.to_owned();
|
||||
|
||||
if let Err(err) = func(bot, msg, db, dialogue.clone(), previous, text).await {
|
||||
if let Err(err) = func(bot, msg, db, dialogue.clone(), handler.previous, text).await {
|
||||
let _ = dialogue.exit().await;
|
||||
return Err(err);
|
||||
}
|
||||
|
@ -147,8 +147,9 @@ pub async fn get_user(
|
||||
|
||||
if let Some("/cancel") = msg.text().map(str::trim) {
|
||||
dialogue.exit().await?;
|
||||
bot.send_message(msg.chat.id, "Successfully cancelled")
|
||||
.reply_markup(deletion_markup())
|
||||
handler
|
||||
.previous
|
||||
.alter_message(&bot, "Successfully cancelled", deletion_markup(), None)
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
@ -156,8 +157,10 @@ pub async fn get_user(
|
||||
let file = match validate_document(msg.document()) {
|
||||
Ok(document) => &document.file,
|
||||
Err(text) => {
|
||||
let msg = bot.send_message(msg.chat.id, text).await?;
|
||||
handler.previous = MessageIds::from(&msg);
|
||||
handler
|
||||
.previous
|
||||
.alter_message(&bot, text, None, None)
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
@ -175,15 +178,17 @@ pub async fn get_user(
|
||||
let user = match spawn_blocking(move || user_from_vec(data, existing_names)).await? {
|
||||
Ok(Ok(user)) => user,
|
||||
Ok(Err(error_text)) => {
|
||||
let msg = bot.send_message(msg.chat.id, error_text).await?;
|
||||
handler.previous = MessageIds::from(&msg);
|
||||
handler
|
||||
.previous
|
||||
.alter_message(&bot, error_text, None, None)
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
Err(_) => {
|
||||
let msg = bot
|
||||
.send_message(msg.chat.id, "Error parsing the json file. Try again")
|
||||
handler
|
||||
.previous
|
||||
.alter_message(&bot, "Error parsing the json file. Try again", None, None)
|
||||
.await?;
|
||||
handler.previous = MessageIds::from(&msg);
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
@ -1,17 +1,62 @@
|
||||
use crate::prelude::*;
|
||||
use futures::future::BoxFuture;
|
||||
use std::sync::Arc;
|
||||
use teloxide::types::{InlineKeyboardMarkup, MessageId};
|
||||
use tokio::sync::Mutex;
|
||||
use std::{mem, sync::Arc};
|
||||
use teloxide::{
|
||||
requests::HasPayload,
|
||||
types::{InlineKeyboardMarkup, MessageId, ParseMode},
|
||||
RequestError,
|
||||
};
|
||||
use tokio::{join, sync::Mutex};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct MessageIds(pub ChatId, pub MessageId);
|
||||
|
||||
impl MessageIds {
|
||||
#[inline]
|
||||
pub async fn delete(&self, bot: &Throttle<Bot>) {
|
||||
pub async fn delete(self, bot: &Throttle<Bot>) {
|
||||
let _ = bot.delete_message(self.0, self.1).await;
|
||||
}
|
||||
|
||||
/// Tries to alter the message or sends a new one
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns true if the message was edited successfully. Returns false otherwise
|
||||
#[inline]
|
||||
pub async fn alter_message(
|
||||
&mut self,
|
||||
bot: &Throttle<Bot>,
|
||||
text: impl Into<String>,
|
||||
markup: impl Into<Option<InlineKeyboardMarkup>>,
|
||||
parse_mode: impl Into<Option<ParseMode>> + Copy,
|
||||
) -> crate::Result<()> {
|
||||
let mut edit = bot.edit_message_text(self.0, self.1, text);
|
||||
edit.parse_mode = parse_mode.into();
|
||||
|
||||
let mut markup = markup.into();
|
||||
if let Some(markup) = markup {
|
||||
edit = edit.reply_markup(markup)
|
||||
}
|
||||
|
||||
match edit.send_ref().await {
|
||||
Ok(msg) => return Ok(()),
|
||||
Err(RequestError::Api(_)) => (),
|
||||
Err(err) => return Err(err.into()),
|
||||
};
|
||||
|
||||
let text = mem::take(&mut edit.text);
|
||||
markup = mem::take(&mut edit.reply_markup);
|
||||
|
||||
let mut send = bot.send_message(self.0, text);
|
||||
send.payload_mut().parse_mode = parse_mode.into();
|
||||
if let Some(markup) = markup {
|
||||
send = send.reply_markup(markup);
|
||||
}
|
||||
|
||||
let msg = join!(self.delete(bot), send.send()).1?;
|
||||
*self = Self::from(&msg);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Message> for MessageIds {
|
||||
|
Reference in New Issue
Block a user