Harmonise naming convention across the examples

This commit is contained in:
Hirrolot 2022-02-02 15:40:22 +06:00
parent c7bbfcccfa
commit 62934f29bd
8 changed files with 83 additions and 85 deletions

View file

@ -66,14 +66,14 @@ fn calc_restrict_time(time: u64, unit: UnitOfTime) -> Duration {
type MyBot = AutoSend<Bot>;
// Kick a user with a replied message.
async fn kick_user(bot: MyBot, mes: Message) -> Result<(), Box<dyn Error + Send + Sync>> {
match mes.reply_to_message() {
async fn kick_user(bot: MyBot, msg: Message) -> Result<(), Box<dyn Error + Send + Sync>> {
match msg.reply_to_message() {
Some(replied) => {
// bot.unban_chat_member can also kicks a user from a group chat.
bot.unban_chat_member(mes.chat_id(), replied.from().unwrap().id).await?;
bot.unban_chat_member(msg.chat_id(), replied.from().unwrap().id).await?;
}
None => {
bot.send_message(mes.chat_id(), "Use this command in reply to another message").await?;
bot.send_message(msg.chat_id(), "Use this command in reply to another message").await?;
}
}
Ok(())
@ -82,24 +82,24 @@ async fn kick_user(bot: MyBot, mes: Message) -> Result<(), Box<dyn Error + Send
// Mute a user with a replied message.
async fn mute_user(
bot: MyBot,
mes: Message,
msg: Message,
time: Duration,
) -> Result<(), Box<dyn Error + Send + Sync>> {
match mes.reply_to_message() {
match msg.reply_to_message() {
Some(replied) => {
bot.restrict_chat_member(
mes.chat_id(),
msg.chat_id(),
replied.from().expect("Must be MessageKind::Common").id,
ChatPermissions::default(),
)
.until_date(
DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(mes.date as i64, 0), Utc)
DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(msg.date as i64, 0), Utc)
+ time,
)
.await?;
}
None => {
bot.send_message(mes.chat_id(), "Use this command in a reply to another message!")
bot.send_message(msg.chat_id(), "Use this command in a reply to another message!")
.await?;
}
}
@ -109,23 +109,23 @@ async fn mute_user(
// Ban a user with replied message.
async fn ban_user(
bot: MyBot,
mes: Message,
msg: Message,
time: Duration,
) -> Result<(), Box<dyn Error + Send + Sync>> {
match mes.reply_to_message() {
match msg.reply_to_message() {
Some(replied) => {
bot.kick_chat_member(
mes.chat_id(),
msg.chat_id(),
replied.from().expect("Must be MessageKind::Common").id,
)
.until_date(
DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(mes.date as i64, 0), Utc)
DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(msg.date as i64, 0), Utc)
+ time,
)
.await?;
}
None => {
bot.send_message(mes.chat_id(), "Use this command in a reply to another message!")
bot.send_message(msg.chat_id(), "Use this command in a reply to another message!")
.await?;
}
}
@ -134,16 +134,16 @@ async fn ban_user(
async fn action(
bot: MyBot,
mes: Message,
msg: Message,
command: Command,
) -> Result<(), Box<dyn Error + Send + Sync>> {
match command {
Command::Help => {
bot.send_message(mes.chat_id(), Command::descriptions()).await?;
bot.send_message(msg.chat_id(), Command::descriptions()).await?;
}
Command::Kick => kick_user(bot, mes).await?,
Command::Ban { time, unit } => ban_user(bot, mes, calc_restrict_time(time, unit)).await?,
Command::Mute { time, unit } => mute_user(bot, mes, calc_restrict_time(time, unit)).await?,
Command::Kick => kick_user(bot, msg).await?,
Command::Ban { time, unit } => ban_user(bot, msg, calc_restrict_time(time, unit)).await?,
Command::Mute { time, unit } => mute_user(bot, msg, calc_restrict_time(time, unit)).await?,
};
Ok(())

View file

@ -19,7 +19,7 @@ use teloxide::{
prelude2::*,
};
type BotDialogue = Dialogue<State, SqliteStorage<Json>>;
type MyDialogue = Dialogue<State, SqliteStorage<Json>>;
#[derive(DialogueState, Clone, serde::Serialize, serde::Deserialize)]
#[handler_out(anyhow::Result<()>)]
@ -71,37 +71,37 @@ async fn main() {
async fn handle_start(
bot: AutoSend<Bot>,
mes: Message,
dialogue: BotDialogue,
msg: Message,
dialogue: MyDialogue,
) -> anyhow::Result<()> {
bot.send_message(mes.chat_id(), "Let's start! What's your full name?").await?;
bot.send_message(msg.chat_id(), "Let's start! What's your full name?").await?;
dialogue.update(State::ReceiveFullName).await?;
Ok(())
}
async fn handle_receive_full_name(
bot: AutoSend<Bot>,
mes: Message,
dialogue: BotDialogue,
msg: Message,
dialogue: MyDialogue,
) -> anyhow::Result<()> {
bot.send_message(mes.chat_id(), "How old are you?").await?;
dialogue.update(State::ReceiveAge(mes.text().unwrap().into())).await?;
bot.send_message(msg.chat_id(), "How old are you?").await?;
dialogue.update(State::ReceiveAge(msg.text().unwrap().into())).await?;
Ok(())
}
async fn handle_receive_age(
bot: AutoSend<Bot>,
mes: Message,
dialogue: BotDialogue,
msg: Message,
dialogue: MyDialogue,
full_name: String,
) -> anyhow::Result<()> {
match mes.text().unwrap().parse::<u8>() {
match msg.text().unwrap().parse::<u8>() {
Ok(age) => {
bot.send_message(mes.chat_id(), "What's your location?").await?;
bot.send_message(msg.chat_id(), "What's your location?").await?;
dialogue.update(State::ReceiveLocation(ReceiveLocation { full_name, age })).await?;
}
_ => {
bot.send_message(mes.chat_id(), "Send me a number.").await?;
bot.send_message(msg.chat_id(), "Send me a number.").await?;
}
}
Ok(())
@ -109,14 +109,14 @@ async fn handle_receive_age(
async fn handle_receive_location(
bot: AutoSend<Bot>,
mes: Message,
dialogue: BotDialogue,
msg: Message,
dialogue: MyDialogue,
state: ReceiveLocation,
) -> anyhow::Result<()> {
let location = mes.text().unwrap();
let location = msg.text().unwrap();
let message =
format!("Full name: {}\nAge: {}\nLocation: {}", state.full_name, state.age, location);
bot.send_message(mes.chat_id(), message).await?;
bot.send_message(msg.chat_id(), message).await?;
dialogue.exit().await?;
Ok(())
}

View file

@ -25,12 +25,12 @@ async fn main() {
// Filter allow you to filter updates by some condition.
dptree::filter(
// Note that `async move` is obligatory.
|mes: Message| async move { mes.chat.is_group() || mes.chat.is_supergroup() },
|msg: Message| async move { msg.chat.is_group() || msg.chat.is_supergroup() },
)
// Endpoint is a last message handler.
.endpoint(|mes: Message, bot: AutoSend<Bot>| async move {
.endpoint(|msg: Message, bot: AutoSend<Bot>| async move {
log::info!("Received message from the group chat.");
bot.send_message(mes.chat.id, "This is a group chat.").await?;
bot.send_message(msg.chat.id, "This is a group chat.").await?;
respond(())
}),
)
@ -39,9 +39,9 @@ async fn main() {
.branch(
// There are some `filter` functions on message, that filters events. This
// filter will filter only messages with dices.
Message::filter_dice().endpoint(|mes: Message, bot: AutoSend<Bot>| async move {
bot.send_message(mes.chat.id, "This is a dice!")
.reply_to_message_id(mes.id)
Message::filter_dice().endpoint(|msg: Message, bot: AutoSend<Bot>| async move {
bot.send_message(msg.chat.id, "This is a dice!")
.reply_to_message_id(msg.id)
.await?;
Ok(())
}),
@ -60,19 +60,19 @@ async fn main() {
)
.branch(
// Filter maintainer by used ID.
dptree::filter(|mes: Message, cfg: ConfigParameters| async move {
mes.from().map(|user| user.id == cfg.bot_maintainer).unwrap_or_default()
dptree::filter(|msg: Message, cfg: ConfigParameters| async move {
msg.from().map(|user| user.id == cfg.bot_maintainer).unwrap_or_default()
})
.add_command::<MaintainerCommands>()
.endpoint(
|mes: Message, bot: AutoSend<Bot>, cmd: MaintainerCommands| async move {
|msg: Message, bot: AutoSend<Bot>, cmd: MaintainerCommands| async move {
match cmd {
MaintainerCommands::Rand { from, to } => {
let mut rng = rand::rngs::OsRng::default();
let value: u64 = rng.gen_range(from..=to);
std::mem::drop(rng);
bot.send_message(mes.chat.id, value.to_string()).await?;
bot.send_message(msg.chat.id, value.to_string()).await?;
Ok(())
}
@ -130,21 +130,21 @@ enum MaintainerCommands {
}
async fn simple_commands_handler(
mes: Message,
msg: Message,
bot: AutoSend<Bot>,
cmd: SimpleCommand,
cfg: ConfigParameters,
) -> Result<(), teloxide::RequestError> {
let text = match cmd {
SimpleCommand::Help => {
if mes.from().unwrap().id == cfg.bot_maintainer {
if msg.from().unwrap().id == cfg.bot_maintainer {
format!("{}\n{}", SimpleCommand::descriptions(), MaintainerCommands::descriptions())
} else {
SimpleCommand::descriptions()
}
}
SimpleCommand::Maintainer => {
if mes.from().unwrap().id == cfg.bot_maintainer {
if msg.from().unwrap().id == cfg.bot_maintainer {
"Maintainer is you!".into()
} else {
if let Some(username) = cfg.maintainer_username {
@ -155,10 +155,10 @@ async fn simple_commands_handler(
}
}
SimpleCommand::MyId => {
format!("{}", mes.from().unwrap().id)
format!("{}", msg.from().unwrap().id)
}
};
bot.send_message(mes.chat.id, text).await?;
bot.send_message(msg.chat.id, text).await?;
Ok(())
}

View file

@ -26,8 +26,8 @@ async fn main() {
teloxide::repls2::repl_with_listener(
bot.clone(),
|mes: Message, bot: AutoSend<Bot>| async move {
bot.send_message(mes.chat.id, "pong").await?;
|msg: Message, bot: AutoSend<Bot>| async move {
bot.send_message(msg.chat.id, "pong").await?;
respond(())
},
webhook(bot).await,

View file

@ -26,8 +26,8 @@ async fn main() {
teloxide::repls2::repl_with_listener(
bot.clone(),
|mes: Message, bot: AutoSend<Bot>| async move {
bot.send_message(mes.chat.id, "pong").await?;
|msg: Message, bot: AutoSend<Bot>| async move {
bot.send_message(msg.chat.id, "pong").await?;
respond(())
},
webhook(bot).await,

View file

@ -5,7 +5,7 @@ use teloxide::{
};
use thiserror::Error;
type BotDialogue = Dialogue<DialogueState, RedisStorage<Bincode>>;
type MyDialogue = Dialogue<DialogueState, RedisStorage<Bincode>>;
type StorageError = <RedisStorage<Bincode> as Storage<DialogueState>>::Error;
#[derive(Debug, Error)]
@ -30,12 +30,12 @@ impl Default for DialogueState {
async fn handle_message(
bot: AutoSend<Bot>,
mes: Message,
dialogue: BotDialogue,
msg: Message,
dialogue: MyDialogue,
) -> Result<(), Error> {
match mes.text() {
match msg.text() {
None => {
bot.send_message(mes.chat.id, "Send me a text message.").await?;
bot.send_message(msg.chat.id, "Send me a text message.").await?;
}
Some(ans) => {
let state = dialogue.get_or_default().await?;
@ -44,23 +44,23 @@ async fn handle_message(
if let Ok(number) = ans.parse() {
dialogue.update(DialogueState::HaveNumber(number)).await?;
bot.send_message(
mes.chat.id,
msg.chat.id,
format!("Remembered number {}. Now use /get or /reset", number),
)
.await?;
} else {
bot.send_message(mes.chat.id, "Please, send me a number").await?;
bot.send_message(msg.chat.id, "Please, send me a number").await?;
}
}
DialogueState::HaveNumber(num) => {
if ans.starts_with("/get") {
bot.send_message(mes.chat.id, format!("Here is your number: {}", num))
bot.send_message(msg.chat.id, format!("Here is your number: {}", num))
.await?;
} else if ans.starts_with("/reset") {
dialogue.reset().await?;
bot.send_message(mes.chat.id, "Resetted number").await?;
bot.send_message(msg.chat.id, "Resetted number").await?;
} else {
bot.send_message(mes.chat.id, "Please, send /get or /reset").await?;
bot.send_message(msg.chat.id, "Please, send /get or /reset").await?;
}
}
}

View file

@ -17,9 +17,9 @@ async fn main() {
let bot = Bot::from_env().auto_send();
let handler = Update::filter_message().branch(dptree::endpoint(
|mes: Message, bot: AutoSend<Bot>| async move {
|msg: Message, bot: AutoSend<Bot>| async move {
let previous = MESSAGES_TOTAL.fetch_add(1, Ordering::Relaxed);
bot.send_message(mes.chat.id, format!("I received {} messages in total.", previous))
bot.send_message(msg.chat.id, format!("I received {} messages in total.", previous))
.await?;
respond(())
},

View file

@ -5,10 +5,8 @@ use teloxide::{
};
use thiserror::Error;
type Store = SqliteStorage<Json>;
// FIXME: naming
type MyDialogue = Dialogue<BotDialogue, Store>;
type StorageError = <SqliteStorage<Json> as Storage<BotDialogue>>::Error;
type MyDialogue = Dialogue<DialogueState, SqliteStorage<Json>>;
type StorageError = <SqliteStorage<Json> as Storage<DialogueState>>::Error;
#[derive(Debug, Error)]
enum Error {
@ -19,12 +17,12 @@ enum Error {
}
#[derive(serde::Serialize, serde::Deserialize)]
pub enum BotDialogue {
pub enum DialogueState {
Start,
GotNumber(i32),
}
impl Default for BotDialogue {
impl Default for DialogueState {
fn default() -> Self {
Self::Start
}
@ -32,37 +30,37 @@ impl Default for BotDialogue {
async fn handle_message(
bot: AutoSend<Bot>,
mes: Message,
msg: Message,
dialogue: MyDialogue,
) -> Result<(), Error> {
match mes.text() {
match msg.text() {
None => {
bot.send_message(mes.chat.id, "Send me a text message.").await?;
bot.send_message(msg.chat.id, "Send me a text message.").await?;
}
Some(ans) => {
let state = dialogue.get_or_default().await?;
match state {
BotDialogue::Start => {
DialogueState::Start => {
if let Ok(number) = ans.parse() {
dialogue.update(BotDialogue::GotNumber(number)).await?;
dialogue.update(DialogueState::GotNumber(number)).await?;
bot.send_message(
mes.chat.id,
msg.chat.id,
format!("Remembered number {}. Now use /get or /reset", number),
)
.await?;
} else {
bot.send_message(mes.chat.id, "Please, send me a number").await?;
bot.send_message(msg.chat.id, "Please, send me a number").await?;
}
}
BotDialogue::GotNumber(num) => {
DialogueState::GotNumber(num) => {
if ans.starts_with("/get") {
bot.send_message(mes.chat.id, format!("Here is your number: {}", num))
bot.send_message(msg.chat.id, format!("Here is your number: {}", num))
.await?;
} else if ans.starts_with("/reset") {
dialogue.reset().await?;
bot.send_message(mes.chat.id, "Resetted number").await?;
bot.send_message(msg.chat.id, "Resetted number").await?;
} else {
bot.send_message(mes.chat.id, "Please, send /get or /reset").await?;
bot.send_message(msg.chat.id, "Please, send /get or /reset").await?;
}
}
}
@ -77,7 +75,7 @@ async fn main() {
let storage = SqliteStorage::open("db.sqlite", Json).await.unwrap();
let handler = dptree::entry()
.add_dialogue::<Message, Store, BotDialogue>()
.add_dialogue::<Message, SqliteStorage<Json>, DialogueState>()
.branch(dptree::endpoint(handle_message));
DispatcherBuilder::new(bot, handler)