Merge pull request #716 from teloxide/update-auto-send

Stop using `AutoSend` in the examples and docs

Former-commit-id: 305fddd7e9
This commit is contained in:
Hirrolot 2022-09-29 14:43:32 +06:00 committed by GitHub
commit 4b1a309621
20 changed files with 90 additions and 128 deletions

View file

@ -20,7 +20,7 @@ env:
# - README.md
# - src/lib.rs
# - down below in a matrix
rust_msrv: 1.58.0
rust_msrv: 1.64.0
jobs:
# Depends on all action that are required for a "successful" CI run.
@ -85,7 +85,7 @@ jobs:
toolchain: nightly-2022-09-01
features: "--all-features"
- rust: msrv
toolchain: 1.58.0
toolchain: 1.64.0
features: "--features full"
steps:

View file

@ -57,8 +57,8 @@ full = [
]
[dependencies]
teloxide-core = { version = "0.7.0", default-features = false }
#teloxide-macros = { version = "0.6.3", optional = true }
# teloxide-core = { version = "0.7.0", default-features = false }
# teloxide-macros = { version = "0.6.3", optional = true }
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
@ -66,7 +66,7 @@ serde = { version = "1.0", features = ["derive"] }
dptree = "0.3.0"
# These lines are used only for development.
# teloxide-core = { git = "https://github.com/teloxide/teloxide-core", rev = "b13393d", default-features = false }
teloxide-core = { git = "https://github.com/teloxide/teloxide-core", rev = "00165e6", default-features = false }
teloxide-macros = { git = "https://github.com/teloxide/teloxide-macros", rev = "0e79c37", optional = true }
# dptree = { git = "https://github.com/teloxide/dptree", rev = "df578e4" }

View file

@ -92,9 +92,9 @@ async fn main() {
pretty_env_logger::init();
log::info!("Starting throw dice bot...");
let bot = Bot::from_env().auto_send();
let bot = Bot::from_env();
teloxide::repl(bot, |message: Message, bot: AutoSend<Bot>| async move {
teloxide::repl(bot, |message: Message, bot: Bot| async move {
bot.send_dice(message.chat.id).await?;
respond(())
})
@ -129,7 +129,7 @@ async fn main() {
pretty_env_logger::init();
log::info!("Starting command bot...");
let bot = Bot::from_env().auto_send();
let bot = Bot::from_env();
teloxide::commands_repl(bot, answer, Command::ty()).await;
}
@ -146,7 +146,7 @@ enum Command {
}
async fn answer(
bot: AutoSend<Bot>,
bot: Bot,
message: Message,
command: Command,
) -> Result<(), Box<dyn Error + Send + Sync>> {
@ -190,18 +190,18 @@ use teloxide::{dispatching::dialogue::InMemStorage, prelude::*};
type MyDialogue = Dialogue<State, InMemStorage<State>>;
type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
#[derive(Clone)]
#[derive(Clone, Default)]
pub enum State {
#[default]
Start,
ReceiveFullName,
ReceiveAge { full_name: String },
ReceiveLocation { full_name: String, age: u8 },
}
impl Default for State {
fn default() -> Self {
Self::Start
}
ReceiveAge {
full_name: String,
},
ReceiveLocation {
full_name: String,
age: u8,
},
}
#[tokio::main]
@ -209,7 +209,7 @@ async fn main() {
pretty_env_logger::init();
log::info!("Starting dialogue bot...");
let bot = Bot::from_env().auto_send();
let bot = Bot::from_env();
Dispatcher::builder(
bot,
@ -229,17 +229,13 @@ async fn main() {
.await;
}
async fn start(bot: AutoSend<Bot>, msg: Message, dialogue: MyDialogue) -> HandlerResult {
async fn start(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
dialogue.update(State::ReceiveFullName).await?;
Ok(())
}
async fn receive_full_name(
bot: AutoSend<Bot>,
msg: Message,
dialogue: MyDialogue,
) -> HandlerResult {
async fn receive_full_name(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
match msg.text() {
Some(text) => {
bot.send_message(msg.chat.id, "How old are you?").await?;
@ -254,7 +250,7 @@ async fn receive_full_name(
}
async fn receive_age(
bot: AutoSend<Bot>,
bot: Bot,
msg: Message,
dialogue: MyDialogue,
full_name: String, // Available from `State::ReceiveAge`.
@ -273,7 +269,7 @@ async fn receive_age(
}
async fn receive_location(
bot: AutoSend<Bot>,
bot: Bot,
msg: Message,
dialogue: MyDialogue,
(full_name, age): (String, u8), // Available from `State::ReceiveLocation`.

View file

@ -58,13 +58,11 @@ async fn main() {
pretty_env_logger::init();
log::info!("Starting admin bot...");
let bot = teloxide::Bot::from_env().auto_send();
let bot = teloxide::Bot::from_env();
teloxide::commands_repl(bot, action, Command::ty()).await;
}
type Bot = AutoSend<teloxide::Bot>;
async fn action(
bot: Bot,
msg: Message,

View file

@ -23,7 +23,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
pretty_env_logger::init();
log::info!("Starting buttons bot...");
let bot = Bot::from_env().auto_send();
let bot = Bot::from_env();
let handler = dptree::entry()
.branch(Update::filter_message().endpoint(message_handler))
@ -58,11 +58,7 @@ fn make_keyboard() -> InlineKeyboardMarkup {
/// Parse the text wrote on Telegram and check if that text is a valid command
/// or not, then match the command. If the command is `/start` it writes a
/// markup with the `InlineKeyboardMarkup`.
async fn message_handler(
m: Message,
bot: AutoSend<Bot>,
me: Me,
) -> Result<(), Box<dyn Error + Send + Sync>> {
async fn message_handler(m: Message, bot: Bot, me: Me) -> Result<(), Box<dyn Error + Send + Sync>> {
if let Some(text) = m.text() {
match BotCommands::parse(text, me.username()) {
Ok(Command::Help) => {
@ -86,7 +82,7 @@ async fn message_handler(
async fn inline_query_handler(
q: InlineQuery,
bot: AutoSend<Bot>,
bot: Bot,
) -> Result<(), Box<dyn Error + Send + Sync>> {
let choose_debian_version = InlineQueryResultArticle::new(
"0",
@ -105,10 +101,7 @@ async fn inline_query_handler(
///
/// **IMPORTANT**: do not send privacy-sensitive data this way!!!
/// Anyone can read data stored in the callback button.
async fn callback_handler(
q: CallbackQuery,
bot: AutoSend<Bot>,
) -> Result<(), Box<dyn Error + Send + Sync>> {
async fn callback_handler(q: CallbackQuery, bot: Bot) -> Result<(), Box<dyn Error + Send + Sync>> {
if let Some(version) = q.data {
let text = format!("You chose: {version}");

View file

@ -7,7 +7,7 @@ async fn main() {
pretty_env_logger::init();
log::info!("Starting command bot...");
let bot = Bot::from_env().auto_send();
let bot = Bot::from_env();
teloxide::commands_repl(bot, answer, Command::ty()).await;
}
@ -24,7 +24,7 @@ enum Command {
}
async fn answer(
bot: AutoSend<Bot>,
bot: Bot,
message: Message,
command: Command,
) -> Result<(), Box<dyn Error + Send + Sync>> {

View file

@ -35,7 +35,7 @@ async fn main() {
pretty_env_logger::init();
log::info!("Starting DB remember bot...");
let bot = Bot::from_env().auto_send();
let bot = Bot::from_env();
let storage: MyStorage = if std::env::var("DB_REMEMBER_REDIS").is_ok() {
RedisStorage::open("redis://127.0.0.1:6379", Bincode).await.unwrap().erase()
@ -60,7 +60,7 @@ async fn main() {
.await;
}
async fn start(bot: AutoSend<Bot>, msg: Message, dialogue: MyDialogue) -> HandlerResult {
async fn start(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
match msg.text().map(|text| text.parse::<i32>()) {
Some(Ok(n)) => {
dialogue.update(State::GotNumber(n)).await?;
@ -79,7 +79,7 @@ async fn start(bot: AutoSend<Bot>, msg: Message, dialogue: MyDialogue) -> Handle
}
async fn got_number(
bot: AutoSend<Bot>,
bot: Bot,
msg: Message,
dialogue: MyDialogue,
num: i32,
@ -97,7 +97,7 @@ async fn got_number(
Ok(())
}
async fn invalid_command(bot: AutoSend<Bot>, msg: Message) -> HandlerResult {
async fn invalid_command(bot: Bot, msg: Message) -> HandlerResult {
bot.send_message(msg.chat.id, "Please, send /get or /reset.").await?;
Ok(())
}

View file

@ -37,7 +37,7 @@ async fn main() {
pretty_env_logger::init();
log::info!("Starting dialogue bot...");
let bot = Bot::from_env().auto_send();
let bot = Bot::from_env();
Dispatcher::builder(
bot,
@ -57,17 +57,13 @@ async fn main() {
.await;
}
async fn start(bot: AutoSend<Bot>, msg: Message, dialogue: MyDialogue) -> HandlerResult {
async fn start(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
dialogue.update(State::ReceiveFullName).await?;
Ok(())
}
async fn receive_full_name(
bot: AutoSend<Bot>,
msg: Message,
dialogue: MyDialogue,
) -> HandlerResult {
async fn receive_full_name(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
match msg.text() {
Some(text) => {
bot.send_message(msg.chat.id, "How old are you?").await?;
@ -82,7 +78,7 @@ async fn receive_full_name(
}
async fn receive_age(
bot: AutoSend<Bot>,
bot: Bot,
msg: Message,
dialogue: MyDialogue,
full_name: String, // Available from `State::ReceiveAge`.
@ -101,7 +97,7 @@ async fn receive_age(
}
async fn receive_location(
bot: AutoSend<Bot>,
bot: Bot,
msg: Message,
dialogue: MyDialogue,
(full_name, age): (String, u8), // Available from `State::ReceiveLocation`.

View file

@ -14,7 +14,7 @@ async fn main() {
pretty_env_logger::init();
log::info!("Starting dispatching features bot...");
let bot = Bot::from_env().auto_send();
let bot = Bot::from_env();
let parameters = ConfigParameters {
bot_maintainer: UserId(0), // Paste your ID to run this bot.
@ -37,8 +37,7 @@ async fn main() {
msg.from().map(|user| user.id == cfg.bot_maintainer).unwrap_or_default()
})
.filter_command::<MaintainerCommands>()
.endpoint(
|msg: Message, bot: AutoSend<Bot>, cmd: MaintainerCommands| async move {
.endpoint(|msg: Message, bot: Bot, cmd: MaintainerCommands| async move {
match cmd {
MaintainerCommands::Rand { from, to } => {
let mut rng = rand::rngs::OsRng::default();
@ -48,14 +47,13 @@ async fn main() {
Ok(())
}
}
},
),
}),
)
.branch(
// Filtering allow you to filter updates by some condition.
dptree::filter(|msg: Message| msg.chat.is_group() || msg.chat.is_supergroup())
// An endpoint is the last update handler.
.endpoint(|msg: Message, bot: AutoSend<Bot>| async move {
.endpoint(|msg: Message, bot: Bot| async move {
log::info!("Received a message from a group chat.");
bot.send_message(msg.chat.id, "This is a group chat.").await?;
respond(())
@ -64,14 +62,12 @@ async fn main() {
.branch(
// There are some extension filtering functions on `Message`. The following filter will
// filter only messages with dices.
Message::filter_dice().endpoint(
|msg: Message, dice: Dice, bot: AutoSend<Bot>| async move {
Message::filter_dice().endpoint(|msg: Message, dice: Dice, bot: Bot| async move {
bot.send_message(msg.chat.id, format!("Dice value: {}", dice.value))
.reply_to_message_id(msg.id)
.await?;
Ok(())
},
),
}),
);
Dispatcher::builder(bot, handler)
@ -119,7 +115,7 @@ enum MaintainerCommands {
async fn simple_commands_handler(
msg: Message,
bot: AutoSend<Bot>,
bot: Bot,
cmd: SimpleCommand,
cfg: ConfigParameters,
me: teloxide::types::Me,

View file

@ -27,7 +27,7 @@ async fn main() {
pretty_env_logger::init();
log::info!("Starting Heroku ping-pong bot...");
let bot = Bot::from_env().auto_send();
let bot = Bot::from_env();
// Heroku auto defines a port value
let port: u16 = env::var("PORT")
@ -47,7 +47,7 @@ async fn main() {
teloxide::repl_with_listener(
bot,
|msg: Message, bot: AutoSend<Bot>| async move {
|msg: Message, bot: Bot| async move {
bot.send_message(msg.chat.id, "pong").await?;
respond(())
},

View file

@ -11,10 +11,10 @@ async fn main() {
pretty_env_logger::init();
log::info!("Starting inline bot...");
let bot = Bot::from_env().auto_send();
let bot = Bot::from_env();
let handler = Update::filter_inline_query().branch(dptree::endpoint(
|query: InlineQuery, bot: AutoSend<Bot>| async move {
|query: InlineQuery, bot: Bot| async move {
// First, create your actual response
let google_search = InlineQueryResultArticle::new(
// Each item needs a unique ID, as well as the response container for the

View file

@ -8,7 +8,7 @@ async fn main() {
pretty_env_logger::init();
log::info!("Starting ngrok ping-pong bot...");
let bot = Bot::from_env().auto_send();
let bot = Bot::from_env();
let addr = ([127, 0, 0, 1], 8443).into();
let url = "Your HTTPS ngrok URL here. Get it by `ngrok http 8443`".parse().unwrap();
@ -18,7 +18,7 @@ async fn main() {
teloxide::repl_with_listener(
bot,
|msg: Message, bot: AutoSend<Bot>| async move {
|msg: Message, bot: Bot| async move {
bot.send_message(msg.chat.id, "pong").await?;
respond(())
},

View file

@ -48,7 +48,7 @@ async fn main() {
pretty_env_logger::init();
log::info!("Starting purchase bot...");
let bot = Bot::from_env().auto_send();
let bot = Bot::from_env();
Dispatcher::builder(bot, schema())
.dependencies(dptree::deps![InMemStorage::<State>::new()])
@ -83,34 +83,30 @@ fn schema() -> UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>>
.branch(callback_query_handler)
}
async fn start(bot: AutoSend<Bot>, msg: Message, dialogue: MyDialogue) -> HandlerResult {
async fn start(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
dialogue.update(State::ReceiveFullName).await?;
Ok(())
}
async fn help(bot: AutoSend<Bot>, msg: Message) -> HandlerResult {
async fn help(bot: Bot, msg: Message) -> HandlerResult {
bot.send_message(msg.chat.id, Command::descriptions().to_string()).await?;
Ok(())
}
async fn cancel(bot: AutoSend<Bot>, msg: Message, dialogue: MyDialogue) -> HandlerResult {
async fn cancel(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
bot.send_message(msg.chat.id, "Cancelling the dialogue.").await?;
dialogue.exit().await?;
Ok(())
}
async fn invalid_state(bot: AutoSend<Bot>, msg: Message) -> HandlerResult {
async fn invalid_state(bot: Bot, msg: Message) -> HandlerResult {
bot.send_message(msg.chat.id, "Unable to handle the message. Type /help to see the usage.")
.await?;
Ok(())
}
async fn receive_full_name(
bot: AutoSend<Bot>,
msg: Message,
dialogue: MyDialogue,
) -> HandlerResult {
async fn receive_full_name(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
match msg.text().map(ToOwned::to_owned) {
Some(full_name) => {
let products = ["Apple", "Banana", "Orange", "Potato"]
@ -130,7 +126,7 @@ async fn receive_full_name(
}
async fn receive_product_selection(
bot: AutoSend<Bot>,
bot: Bot,
q: CallbackQuery,
dialogue: MyDialogue,
full_name: String,

View file

@ -12,11 +12,11 @@ async fn main() {
pretty_env_logger::init();
log::info!("Starting shared state bot...");
let bot = Bot::from_env().auto_send();
let bot = Bot::from_env();
let messages_total = Arc::new(AtomicU64::new(0));
let handler = Update::filter_message().endpoint(
|msg: Message, bot: AutoSend<Bot>, messages_total: Arc<AtomicU64>| async move {
|msg: Message, bot: Bot, messages_total: Arc<AtomicU64>| async move {
let previous = messages_total.fetch_add(1, Ordering::Relaxed);
bot.send_message(msg.chat.id, format!("I received {previous} messages in total."))
.await?;

View file

@ -7,9 +7,9 @@ async fn main() {
pretty_env_logger::init();
log::info!("Starting throw dice bot...");
let bot = Bot::from_env().auto_send();
let bot = Bot::from_env();
teloxide::repl(bot, |message: Message, bot: AutoSend<Bot>| async move {
teloxide::repl(bot, |message: Message, bot: Bot| async move {
bot.send_dice(message.chat.id).await?;
respond(())
})

View file

@ -102,13 +102,10 @@
//! -- no problem, reuse [`dptree::Handler::filter`], [`dptree::case!`], and
//! other combinators in the same way!
//!
//! Finally, we define our endpoints via simple `async` functions like this:
//!
//! <details>
//! <summary>Show the endpoints</summary>
//! Finally, we define our endpoints:
//!
//! ```no_run
//! # use teloxide::{Bot, adaptors::AutoSend};
//! # use teloxide::Bot;
//! # use teloxide::types::{Message, CallbackQuery};
//! # use teloxide::dispatching::dialogue::{InMemStorage, Dialogue};
//! # enum State{}
@ -116,32 +113,23 @@
//! type MyDialogue = Dialogue<State, InMemStorage<State>>;
//! type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
//!
//! async fn start(bot: AutoSend<Bot>, msg: Message, dialogue: MyDialogue) -> HandlerResult {
//! async fn start(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
//! todo!()
//! }
//!
//! async fn help(bot: AutoSend<Bot>, msg: Message) -> HandlerResult {
//! async fn help(bot: Bot, msg: Message) -> HandlerResult {
//! todo!()
//! }
//!
//! async fn cancel(bot: AutoSend<Bot>, msg: Message, dialogue: MyDialogue) -> HandlerResult {
//! async fn cancel(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
//! todo!()
//! }
//!
//! async fn invalid_state(bot: AutoSend<Bot>, msg: Message) -> HandlerResult {
//! async fn invalid_state(bot: Bot, msg: Message) -> HandlerResult {
//! todo!()
//! }
//!
//! async fn receive_full_name(
//! bot: AutoSend<Bot>,
//! msg: Message,
//! dialogue: MyDialogue,
//! ) -> HandlerResult {
//! async fn receive_full_name(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
//! todo!()
//! }
//!
//! async fn receive_product_selection(
//! bot: AutoSend<Bot>,
//! bot: Bot,
//! q: CallbackQuery,
//! dialogue: MyDialogue,
//! full_name: String,
@ -150,10 +138,8 @@
//! }
//! ```
//!
//! </details>
//!
//! Each parameter is supplied as a dependency by teloxide. In particular:
//! - `bot: AutoSend<Bot>` comes from the dispatcher (see below)
//! - `bot: Bot` comes from the dispatcher (see below)
//! - `msg: Message` comes from [`Update::filter_message`]
//! - `q: CallbackQuery` comes from [`Update::filter_callback_query`]
//! - `dialogue: MyDialogue` comes from [`dialogue::enter`]
@ -170,7 +156,7 @@
//! # fn schema() -> teloxide::dispatching::UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>> { teloxide::dptree::entry() }
//! #[tokio::main]
//! async fn main() {
//! let bot = Bot::from_env().auto_send();
//! let bot = Bot::from_env();
//!
//! Dispatcher::builder(bot, schema())
//! .dependencies(dptree::deps![InMemStorage::<State>::new()])

View file

@ -38,7 +38,7 @@
//! # type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
//! # #[derive(Clone, Debug)] enum State { ReceiveLocation { full_name: String, age: u8 } }
//! async fn receive_age(
//! bot: AutoSend<Bot>,
//! bot: Bot,
//! msg: Message,
//! dialogue: MyDialogue,
//! full_name: String, // Available from `State::ReceiveAge`.
@ -70,7 +70,7 @@
//! # type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
//! # #[derive(Clone, Debug)] enum State {}
//! async fn receive_location(
//! bot: AutoSend<Bot>,
//! bot: Bot,
//! msg: Message,
//! dialogue: MyDialogue,
//! (full_name, age): (String, u8), // Available from `State::ReceiveLocation`.

View file

@ -6,7 +6,7 @@
| `webhooks-axum` | Enables webhook implementation based on axum framework |
| `macros` | Re-exports macros from [`teloxide-macros`]. |
| `ctrlc_handler` | Enables the [`DispatcherBuilder::enable_ctrlc_handler`] function (**enabled by default**). |
| `auto-send` | Enables the [`AutoSend`](adaptors::AutoSend) bot adaptor (**enabled by default**). |
| `auto-send` | Enables the [`AutoSend`](adaptors::AutoSend) bot adaptor (**enabled by default; DEPRECATED**). |
| `throttle` | Enables the [`Throttle`](adaptors::Throttle) bot adaptor. |
| `cache-me` | Enables the [`CacheMe`](adaptors::CacheMe) bot adaptor. |
| `trace-adaptor` | Enables the [`Trace`](adaptors::Trace) bot adaptor. |

View file

@ -13,9 +13,9 @@
//! pretty_env_logger::init();
//! log::info!("Starting throw dice bot...");
//!
//! let bot = Bot::from_env().auto_send();
//! let bot = Bot::from_env();
//!
//! teloxide::repl(bot, |message: Message, bot: AutoSend<Bot>| async move {
//! teloxide::repl(bot, |message: Message, bot: Bot| async move {
//! bot.send_dice(message.chat.id).await?;
//! respond(())
//! })

View file

@ -15,6 +15,7 @@ pub use teloxide_core::types::{
};
#[cfg(feature = "auto-send")]
#[allow(deprecated)]
pub use crate::adaptors::AutoSend;
#[doc(no_inline)]