Skip to content

Commit a622ead

Browse files
authored
Merge pull request #5 from linyinfeng/inline-keyboard
Improve inline keyboard response
2 parents c74c4d7 + 3e01be6 commit a622ead

File tree

2 files changed

+12
-33
lines changed

2 files changed

+12
-33
lines changed

src/error.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,4 @@ impl Error {
112112
.await?;
113113
Ok(())
114114
}
115-
116-
pub async fn report_to_user(
117-
&self,
118-
bot: &Bot,
119-
chat_id: ChatId,
120-
username: &str,
121-
) -> Result<(), teloxide::RequestError> {
122-
log::warn!("report error to chat {}: {:?}", chat_id, self);
123-
bot.send_message(chat_id, format!("@{username} {self}"))
124-
.await?;
125-
Ok(())
126-
}
127115
}

src/main.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use repo::tasks::Task;
3030
use serde::Deserialize;
3131
use serde::Serialize;
3232
use teloxide::dispatching::dialogue::GetChatId;
33+
use teloxide::payloads;
3334
use teloxide::prelude::*;
3435
use teloxide::types::InlineKeyboardButton;
3536
use teloxide::types::InlineKeyboardButtonKind;
@@ -139,27 +140,17 @@ async fn answer(bot: Bot, msg: Message, bc: BCommand) -> ResponseResult<()> {
139140
}
140141

141142
async fn handle_callback_query(bot: Bot, query: CallbackQuery) -> ResponseResult<()> {
142-
match handle_callback_query_command_result(&bot, &query).await {
143-
Ok(msg) => match get_chat_id_and_username_from_query(&query) {
144-
Ok((chat_id, username)) => {
145-
bot.send_message(chat_id, format!("@{username} {msg}"))
146-
.await?;
147-
Ok(())
148-
}
149-
Err(e) => {
150-
log::error!("callback query error: {e}");
151-
Ok(())
152-
}
153-
},
154-
Err(CommandError::Normal(e)) => match get_chat_id_and_username_from_query(&query) {
155-
Ok((chat_id, username)) => e.report_to_user(&bot, chat_id, &username).await,
156-
Err(_e) => {
157-
log::error!("callback query error: {e}");
158-
Ok(())
159-
}
160-
},
161-
Err(CommandError::Teloxide(e)) => Err(e),
162-
}
143+
let result = handle_callback_query_command_result(&bot, &query).await;
144+
let (message, alert) = match result {
145+
Ok(msg) => (msg, false),
146+
Err(CommandError::Normal(e)) => (format!("{e}"), true),
147+
Err(CommandError::Teloxide(e)) => return Err(e),
148+
};
149+
let answer = payloads::AnswerCallbackQuery::new(query.id)
150+
.text(message)
151+
.show_alert(alert);
152+
<Bot as Requester>::AnswerCallbackQuery::new(bot, answer).await?;
153+
Ok(())
163154
}
164155

165156
async fn handle_callback_query_command_result(

0 commit comments

Comments
 (0)