mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 22:46:39 +01:00
Stop using AutoSend
in the examples and docs
This commit is contained in:
parent
8272d4139a
commit
00efbe163a
19 changed files with 87 additions and 115 deletions
|
@ -57,7 +57,7 @@ full = [
|
||||||
]
|
]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
teloxide-core = { version = "0.7.0", default-features = false }
|
# teloxide-core = { version = "0.7.0", default-features = false }
|
||||||
# teloxide-macros = { version = "0.6.3", optional = true }
|
# teloxide-macros = { version = "0.6.3", optional = true }
|
||||||
|
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
@ -66,7 +66,7 @@ serde = { version = "1.0", features = ["derive"] }
|
||||||
dptree = "0.3.0"
|
dptree = "0.3.0"
|
||||||
|
|
||||||
# These lines are used only for development.
|
# 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 }
|
teloxide-macros = { git = "https://github.com/teloxide/teloxide-macros", rev = "0e79c37", optional = true }
|
||||||
# dptree = { git = "https://github.com/teloxide/dptree", rev = "df578e4" }
|
# dptree = { git = "https://github.com/teloxide/dptree", rev = "df578e4" }
|
||||||
|
|
||||||
|
|
40
README.md
40
README.md
|
@ -92,9 +92,9 @@ async fn main() {
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
log::info!("Starting throw dice bot...");
|
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?;
|
bot.send_dice(message.chat.id).await?;
|
||||||
respond(())
|
respond(())
|
||||||
})
|
})
|
||||||
|
@ -129,7 +129,7 @@ async fn main() {
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
log::info!("Starting command bot...");
|
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;
|
teloxide::commands_repl(bot, answer, Command::ty()).await;
|
||||||
}
|
}
|
||||||
|
@ -146,7 +146,7 @@ enum Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn answer(
|
async fn answer(
|
||||||
bot: AutoSend<Bot>,
|
bot: Bot,
|
||||||
message: Message,
|
message: Message,
|
||||||
command: Command,
|
command: Command,
|
||||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||||
|
@ -190,18 +190,18 @@ use teloxide::{dispatching::dialogue::InMemStorage, prelude::*};
|
||||||
type MyDialogue = Dialogue<State, InMemStorage<State>>;
|
type MyDialogue = Dialogue<State, InMemStorage<State>>;
|
||||||
type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
|
type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Default)]
|
||||||
pub enum State {
|
pub enum State {
|
||||||
|
#[default]
|
||||||
Start,
|
Start,
|
||||||
ReceiveFullName,
|
ReceiveFullName,
|
||||||
ReceiveAge { full_name: String },
|
ReceiveAge {
|
||||||
ReceiveLocation { full_name: String, age: u8 },
|
full_name: String,
|
||||||
}
|
},
|
||||||
|
ReceiveLocation {
|
||||||
impl Default for State {
|
full_name: String,
|
||||||
fn default() -> Self {
|
age: u8,
|
||||||
Self::Start
|
},
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
@ -209,7 +209,7 @@ async fn main() {
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
log::info!("Starting dialogue bot...");
|
log::info!("Starting dialogue bot...");
|
||||||
|
|
||||||
let bot = Bot::from_env().auto_send();
|
let bot = Bot::from_env();
|
||||||
|
|
||||||
Dispatcher::builder(
|
Dispatcher::builder(
|
||||||
bot,
|
bot,
|
||||||
|
@ -229,17 +229,13 @@ async fn main() {
|
||||||
.await;
|
.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?;
|
bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
|
||||||
dialogue.update(State::ReceiveFullName).await?;
|
dialogue.update(State::ReceiveFullName).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn receive_full_name(
|
async fn receive_full_name(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||||
bot: AutoSend<Bot>,
|
|
||||||
msg: Message,
|
|
||||||
dialogue: MyDialogue,
|
|
||||||
) -> HandlerResult {
|
|
||||||
match msg.text() {
|
match msg.text() {
|
||||||
Some(text) => {
|
Some(text) => {
|
||||||
bot.send_message(msg.chat.id, "How old are you?").await?;
|
bot.send_message(msg.chat.id, "How old are you?").await?;
|
||||||
|
@ -254,7 +250,7 @@ async fn receive_full_name(
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn receive_age(
|
async fn receive_age(
|
||||||
bot: AutoSend<Bot>,
|
bot: Bot,
|
||||||
msg: Message,
|
msg: Message,
|
||||||
dialogue: MyDialogue,
|
dialogue: MyDialogue,
|
||||||
full_name: String, // Available from `State::ReceiveAge`.
|
full_name: String, // Available from `State::ReceiveAge`.
|
||||||
|
@ -273,7 +269,7 @@ async fn receive_age(
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn receive_location(
|
async fn receive_location(
|
||||||
bot: AutoSend<Bot>,
|
bot: Bot,
|
||||||
msg: Message,
|
msg: Message,
|
||||||
dialogue: MyDialogue,
|
dialogue: MyDialogue,
|
||||||
(full_name, age): (String, u8), // Available from `State::ReceiveLocation`.
|
(full_name, age): (String, u8), // Available from `State::ReceiveLocation`.
|
||||||
|
|
|
@ -58,13 +58,11 @@ async fn main() {
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
log::info!("Starting admin bot...");
|
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;
|
teloxide::commands_repl(bot, action, Command::ty()).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
type Bot = AutoSend<teloxide::Bot>;
|
|
||||||
|
|
||||||
async fn action(
|
async fn action(
|
||||||
bot: Bot,
|
bot: Bot,
|
||||||
msg: Message,
|
msg: Message,
|
||||||
|
|
|
@ -23,7 +23,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
log::info!("Starting buttons bot...");
|
log::info!("Starting buttons bot...");
|
||||||
|
|
||||||
let bot = Bot::from_env().auto_send();
|
let bot = Bot::from_env();
|
||||||
|
|
||||||
let handler = dptree::entry()
|
let handler = dptree::entry()
|
||||||
.branch(Update::filter_message().endpoint(message_handler))
|
.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
|
/// 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
|
/// or not, then match the command. If the command is `/start` it writes a
|
||||||
/// markup with the `InlineKeyboardMarkup`.
|
/// markup with the `InlineKeyboardMarkup`.
|
||||||
async fn message_handler(
|
async fn message_handler(m: Message, bot: Bot, me: Me) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||||
m: Message,
|
|
||||||
bot: AutoSend<Bot>,
|
|
||||||
me: Me,
|
|
||||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
|
||||||
if let Some(text) = m.text() {
|
if let Some(text) = m.text() {
|
||||||
match BotCommands::parse(text, me.username()) {
|
match BotCommands::parse(text, me.username()) {
|
||||||
Ok(Command::Help) => {
|
Ok(Command::Help) => {
|
||||||
|
@ -86,7 +82,7 @@ async fn message_handler(
|
||||||
|
|
||||||
async fn inline_query_handler(
|
async fn inline_query_handler(
|
||||||
q: InlineQuery,
|
q: InlineQuery,
|
||||||
bot: AutoSend<Bot>,
|
bot: Bot,
|
||||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||||
let choose_debian_version = InlineQueryResultArticle::new(
|
let choose_debian_version = InlineQueryResultArticle::new(
|
||||||
"0",
|
"0",
|
||||||
|
@ -105,10 +101,7 @@ async fn inline_query_handler(
|
||||||
///
|
///
|
||||||
/// **IMPORTANT**: do not send privacy-sensitive data this way!!!
|
/// **IMPORTANT**: do not send privacy-sensitive data this way!!!
|
||||||
/// Anyone can read data stored in the callback button.
|
/// Anyone can read data stored in the callback button.
|
||||||
async fn callback_handler(
|
async fn callback_handler(q: CallbackQuery, bot: Bot) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||||
q: CallbackQuery,
|
|
||||||
bot: AutoSend<Bot>,
|
|
||||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
|
||||||
if let Some(version) = q.data {
|
if let Some(version) = q.data {
|
||||||
let text = format!("You chose: {version}");
|
let text = format!("You chose: {version}");
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ async fn main() {
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
log::info!("Starting command bot...");
|
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;
|
teloxide::commands_repl(bot, answer, Command::ty()).await;
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ enum Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn answer(
|
async fn answer(
|
||||||
bot: AutoSend<Bot>,
|
bot: Bot,
|
||||||
message: Message,
|
message: Message,
|
||||||
command: Command,
|
command: Command,
|
||||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||||
|
|
|
@ -35,7 +35,7 @@ async fn main() {
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
log::info!("Starting DB remember bot...");
|
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() {
|
let storage: MyStorage = if std::env::var("DB_REMEMBER_REDIS").is_ok() {
|
||||||
RedisStorage::open("redis://127.0.0.1:6379", Bincode).await.unwrap().erase()
|
RedisStorage::open("redis://127.0.0.1:6379", Bincode).await.unwrap().erase()
|
||||||
|
@ -60,7 +60,7 @@ async fn main() {
|
||||||
.await;
|
.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>()) {
|
match msg.text().map(|text| text.parse::<i32>()) {
|
||||||
Some(Ok(n)) => {
|
Some(Ok(n)) => {
|
||||||
dialogue.update(State::GotNumber(n)).await?;
|
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(
|
async fn got_number(
|
||||||
bot: AutoSend<Bot>,
|
bot: Bot,
|
||||||
msg: Message,
|
msg: Message,
|
||||||
dialogue: MyDialogue,
|
dialogue: MyDialogue,
|
||||||
num: i32,
|
num: i32,
|
||||||
|
@ -97,7 +97,7 @@ async fn got_number(
|
||||||
Ok(())
|
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?;
|
bot.send_message(msg.chat.id, "Please, send /get or /reset.").await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,7 @@ async fn main() {
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
log::info!("Starting dialogue bot...");
|
log::info!("Starting dialogue bot...");
|
||||||
|
|
||||||
let bot = Bot::from_env().auto_send();
|
let bot = Bot::from_env();
|
||||||
|
|
||||||
Dispatcher::builder(
|
Dispatcher::builder(
|
||||||
bot,
|
bot,
|
||||||
|
@ -57,17 +57,13 @@ async fn main() {
|
||||||
.await;
|
.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?;
|
bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
|
||||||
dialogue.update(State::ReceiveFullName).await?;
|
dialogue.update(State::ReceiveFullName).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn receive_full_name(
|
async fn receive_full_name(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||||
bot: AutoSend<Bot>,
|
|
||||||
msg: Message,
|
|
||||||
dialogue: MyDialogue,
|
|
||||||
) -> HandlerResult {
|
|
||||||
match msg.text() {
|
match msg.text() {
|
||||||
Some(text) => {
|
Some(text) => {
|
||||||
bot.send_message(msg.chat.id, "How old are you?").await?;
|
bot.send_message(msg.chat.id, "How old are you?").await?;
|
||||||
|
@ -82,7 +78,7 @@ async fn receive_full_name(
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn receive_age(
|
async fn receive_age(
|
||||||
bot: AutoSend<Bot>,
|
bot: Bot,
|
||||||
msg: Message,
|
msg: Message,
|
||||||
dialogue: MyDialogue,
|
dialogue: MyDialogue,
|
||||||
full_name: String, // Available from `State::ReceiveAge`.
|
full_name: String, // Available from `State::ReceiveAge`.
|
||||||
|
@ -101,7 +97,7 @@ async fn receive_age(
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn receive_location(
|
async fn receive_location(
|
||||||
bot: AutoSend<Bot>,
|
bot: Bot,
|
||||||
msg: Message,
|
msg: Message,
|
||||||
dialogue: MyDialogue,
|
dialogue: MyDialogue,
|
||||||
(full_name, age): (String, u8), // Available from `State::ReceiveLocation`.
|
(full_name, age): (String, u8), // Available from `State::ReceiveLocation`.
|
||||||
|
|
|
@ -14,7 +14,7 @@ async fn main() {
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
log::info!("Starting dispatching features bot...");
|
log::info!("Starting dispatching features bot...");
|
||||||
|
|
||||||
let bot = Bot::from_env().auto_send();
|
let bot = Bot::from_env();
|
||||||
|
|
||||||
let parameters = ConfigParameters {
|
let parameters = ConfigParameters {
|
||||||
bot_maintainer: UserId(0), // Paste your ID to run this bot.
|
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()
|
msg.from().map(|user| user.id == cfg.bot_maintainer).unwrap_or_default()
|
||||||
})
|
})
|
||||||
.filter_command::<MaintainerCommands>()
|
.filter_command::<MaintainerCommands>()
|
||||||
.endpoint(
|
.endpoint(|msg: Message, bot: Bot, cmd: MaintainerCommands| async move {
|
||||||
|msg: Message, bot: AutoSend<Bot>, cmd: MaintainerCommands| async move {
|
|
||||||
match cmd {
|
match cmd {
|
||||||
MaintainerCommands::Rand { from, to } => {
|
MaintainerCommands::Rand { from, to } => {
|
||||||
let mut rng = rand::rngs::OsRng::default();
|
let mut rng = rand::rngs::OsRng::default();
|
||||||
|
@ -48,14 +47,13 @@ async fn main() {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}),
|
||||||
),
|
|
||||||
)
|
)
|
||||||
.branch(
|
.branch(
|
||||||
// Filtering allow you to filter updates by some condition.
|
// Filtering allow you to filter updates by some condition.
|
||||||
dptree::filter(|msg: Message| msg.chat.is_group() || msg.chat.is_supergroup())
|
dptree::filter(|msg: Message| msg.chat.is_group() || msg.chat.is_supergroup())
|
||||||
// An endpoint is the last update handler.
|
// 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.");
|
log::info!("Received a message from a group chat.");
|
||||||
bot.send_message(msg.chat.id, "This is a group chat.").await?;
|
bot.send_message(msg.chat.id, "This is a group chat.").await?;
|
||||||
respond(())
|
respond(())
|
||||||
|
@ -64,14 +62,12 @@ async fn main() {
|
||||||
.branch(
|
.branch(
|
||||||
// There are some extension filtering functions on `Message`. The following filter will
|
// There are some extension filtering functions on `Message`. The following filter will
|
||||||
// filter only messages with dices.
|
// filter only messages with dices.
|
||||||
Message::filter_dice().endpoint(
|
Message::filter_dice().endpoint(|msg: Message, dice: Dice, bot: Bot| async move {
|
||||||
|msg: Message, dice: Dice, bot: AutoSend<Bot>| async move {
|
|
||||||
bot.send_message(msg.chat.id, format!("Dice value: {}", dice.value))
|
bot.send_message(msg.chat.id, format!("Dice value: {}", dice.value))
|
||||||
.reply_to_message_id(msg.id)
|
.reply_to_message_id(msg.id)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
},
|
}),
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
Dispatcher::builder(bot, handler)
|
Dispatcher::builder(bot, handler)
|
||||||
|
@ -119,7 +115,7 @@ enum MaintainerCommands {
|
||||||
|
|
||||||
async fn simple_commands_handler(
|
async fn simple_commands_handler(
|
||||||
msg: Message,
|
msg: Message,
|
||||||
bot: AutoSend<Bot>,
|
bot: Bot,
|
||||||
cmd: SimpleCommand,
|
cmd: SimpleCommand,
|
||||||
cfg: ConfigParameters,
|
cfg: ConfigParameters,
|
||||||
me: teloxide::types::Me,
|
me: teloxide::types::Me,
|
||||||
|
|
|
@ -27,7 +27,7 @@ async fn main() {
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
log::info!("Starting Heroku ping-pong bot...");
|
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
|
// Heroku auto defines a port value
|
||||||
let port: u16 = env::var("PORT")
|
let port: u16 = env::var("PORT")
|
||||||
|
@ -47,7 +47,7 @@ async fn main() {
|
||||||
|
|
||||||
teloxide::repl_with_listener(
|
teloxide::repl_with_listener(
|
||||||
bot,
|
bot,
|
||||||
|msg: Message, bot: AutoSend<Bot>| async move {
|
|msg: Message, bot: Bot| async move {
|
||||||
bot.send_message(msg.chat.id, "pong").await?;
|
bot.send_message(msg.chat.id, "pong").await?;
|
||||||
respond(())
|
respond(())
|
||||||
},
|
},
|
||||||
|
|
|
@ -11,10 +11,10 @@ async fn main() {
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
log::info!("Starting inline bot...");
|
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(
|
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
|
// First, create your actual response
|
||||||
let google_search = InlineQueryResultArticle::new(
|
let google_search = InlineQueryResultArticle::new(
|
||||||
// Each item needs a unique ID, as well as the response container for the
|
// Each item needs a unique ID, as well as the response container for the
|
||||||
|
|
|
@ -8,7 +8,7 @@ async fn main() {
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
log::info!("Starting ngrok ping-pong bot...");
|
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 addr = ([127, 0, 0, 1], 8443).into();
|
||||||
let url = "Your HTTPS ngrok URL here. Get it by `ngrok http 8443`".parse().unwrap();
|
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(
|
teloxide::repl_with_listener(
|
||||||
bot,
|
bot,
|
||||||
|msg: Message, bot: AutoSend<Bot>| async move {
|
|msg: Message, bot: Bot| async move {
|
||||||
bot.send_message(msg.chat.id, "pong").await?;
|
bot.send_message(msg.chat.id, "pong").await?;
|
||||||
respond(())
|
respond(())
|
||||||
},
|
},
|
||||||
|
|
|
@ -48,7 +48,7 @@ async fn main() {
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
log::info!("Starting purchase bot...");
|
log::info!("Starting purchase bot...");
|
||||||
|
|
||||||
let bot = Bot::from_env().auto_send();
|
let bot = Bot::from_env();
|
||||||
|
|
||||||
Dispatcher::builder(bot, schema())
|
Dispatcher::builder(bot, schema())
|
||||||
.dependencies(dptree::deps![InMemStorage::<State>::new()])
|
.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)
|
.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?;
|
bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
|
||||||
dialogue.update(State::ReceiveFullName).await?;
|
dialogue.update(State::ReceiveFullName).await?;
|
||||||
Ok(())
|
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?;
|
bot.send_message(msg.chat.id, Command::descriptions().to_string()).await?;
|
||||||
Ok(())
|
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?;
|
bot.send_message(msg.chat.id, "Cancelling the dialogue.").await?;
|
||||||
dialogue.exit().await?;
|
dialogue.exit().await?;
|
||||||
Ok(())
|
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.")
|
bot.send_message(msg.chat.id, "Unable to handle the message. Type /help to see the usage.")
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn receive_full_name(
|
async fn receive_full_name(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||||
bot: AutoSend<Bot>,
|
|
||||||
msg: Message,
|
|
||||||
dialogue: MyDialogue,
|
|
||||||
) -> HandlerResult {
|
|
||||||
match msg.text().map(ToOwned::to_owned) {
|
match msg.text().map(ToOwned::to_owned) {
|
||||||
Some(full_name) => {
|
Some(full_name) => {
|
||||||
let products = ["Apple", "Banana", "Orange", "Potato"]
|
let products = ["Apple", "Banana", "Orange", "Potato"]
|
||||||
|
@ -130,7 +126,7 @@ async fn receive_full_name(
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn receive_product_selection(
|
async fn receive_product_selection(
|
||||||
bot: AutoSend<Bot>,
|
bot: Bot,
|
||||||
q: CallbackQuery,
|
q: CallbackQuery,
|
||||||
dialogue: MyDialogue,
|
dialogue: MyDialogue,
|
||||||
full_name: String,
|
full_name: String,
|
||||||
|
|
|
@ -12,11 +12,11 @@ async fn main() {
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
log::info!("Starting shared state bot...");
|
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 messages_total = Arc::new(AtomicU64::new(0));
|
||||||
|
|
||||||
let handler = Update::filter_message().endpoint(
|
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);
|
let previous = messages_total.fetch_add(1, Ordering::Relaxed);
|
||||||
bot.send_message(msg.chat.id, format!("I received {previous} messages in total."))
|
bot.send_message(msg.chat.id, format!("I received {previous} messages in total."))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
|
@ -7,9 +7,9 @@ async fn main() {
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
log::info!("Starting throw dice bot...");
|
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?;
|
bot.send_dice(message.chat.id).await?;
|
||||||
respond(())
|
respond(())
|
||||||
})
|
})
|
||||||
|
|
|
@ -108,7 +108,7 @@
|
||||||
//! <summary>Show the endpoints</summary>
|
//! <summary>Show the endpoints</summary>
|
||||||
//!
|
//!
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
//! # use teloxide::{Bot, adaptors::AutoSend};
|
//! # use teloxide::Bot;
|
||||||
//! # use teloxide::types::{Message, CallbackQuery};
|
//! # use teloxide::types::{Message, CallbackQuery};
|
||||||
//! # use teloxide::dispatching::dialogue::{InMemStorage, Dialogue};
|
//! # use teloxide::dispatching::dialogue::{InMemStorage, Dialogue};
|
||||||
//! # enum State{}
|
//! # enum State{}
|
||||||
|
@ -116,32 +116,28 @@
|
||||||
//! type MyDialogue = Dialogue<State, InMemStorage<State>>;
|
//! type MyDialogue = Dialogue<State, InMemStorage<State>>;
|
||||||
//! type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
|
//! 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!()
|
//! todo!()
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! async fn help(bot: AutoSend<Bot>, msg: Message) -> HandlerResult {
|
//! async fn help(bot: Bot, msg: Message) -> HandlerResult {
|
||||||
//! todo!()
|
//! todo!()
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! async fn cancel(bot: AutoSend<Bot>, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
//! async fn cancel(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||||
//! todo!()
|
//! todo!()
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! async fn invalid_state(bot: AutoSend<Bot>, msg: Message) -> HandlerResult {
|
//! async fn invalid_state(bot: Bot, msg: Message) -> HandlerResult {
|
||||||
//! todo!()
|
//! todo!()
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! async fn receive_full_name(
|
//! async fn receive_full_name(bot: Bot, msg: Message, dialogue: MyDialogue) -> HandlerResult {
|
||||||
//! bot: AutoSend<Bot>,
|
|
||||||
//! msg: Message,
|
|
||||||
//! dialogue: MyDialogue,
|
|
||||||
//! ) -> HandlerResult {
|
|
||||||
//! todo!()
|
//! todo!()
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! async fn receive_product_selection(
|
//! async fn receive_product_selection(
|
||||||
//! bot: AutoSend<Bot>,
|
//! bot: Bot,
|
||||||
//! q: CallbackQuery,
|
//! q: CallbackQuery,
|
||||||
//! dialogue: MyDialogue,
|
//! dialogue: MyDialogue,
|
||||||
//! full_name: String,
|
//! full_name: String,
|
||||||
|
@ -153,7 +149,7 @@
|
||||||
//! </details>
|
//! </details>
|
||||||
//!
|
//!
|
||||||
//! Each parameter is supplied as a dependency by teloxide. In particular:
|
//! 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`]
|
//! - `msg: Message` comes from [`Update::filter_message`]
|
||||||
//! - `q: CallbackQuery` comes from [`Update::filter_callback_query`]
|
//! - `q: CallbackQuery` comes from [`Update::filter_callback_query`]
|
||||||
//! - `dialogue: MyDialogue` comes from [`dialogue::enter`]
|
//! - `dialogue: MyDialogue` comes from [`dialogue::enter`]
|
||||||
|
@ -170,7 +166,7 @@
|
||||||
//! # fn schema() -> teloxide::dispatching::UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>> { teloxide::dptree::entry() }
|
//! # fn schema() -> teloxide::dispatching::UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>> { teloxide::dptree::entry() }
|
||||||
//! #[tokio::main]
|
//! #[tokio::main]
|
||||||
//! async fn main() {
|
//! async fn main() {
|
||||||
//! let bot = Bot::from_env().auto_send();
|
//! let bot = Bot::from_env();
|
||||||
//!
|
//!
|
||||||
//! Dispatcher::builder(bot, schema())
|
//! Dispatcher::builder(bot, schema())
|
||||||
//! .dependencies(dptree::deps![InMemStorage::<State>::new()])
|
//! .dependencies(dptree::deps![InMemStorage::<State>::new()])
|
||||||
|
|
|
@ -38,7 +38,7 @@
|
||||||
//! # type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
|
//! # type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
|
||||||
//! # #[derive(Clone, Debug)] enum State { ReceiveLocation { full_name: String, age: u8 } }
|
//! # #[derive(Clone, Debug)] enum State { ReceiveLocation { full_name: String, age: u8 } }
|
||||||
//! async fn receive_age(
|
//! async fn receive_age(
|
||||||
//! bot: AutoSend<Bot>,
|
//! bot: Bot,
|
||||||
//! msg: Message,
|
//! msg: Message,
|
||||||
//! dialogue: MyDialogue,
|
//! dialogue: MyDialogue,
|
||||||
//! full_name: String, // Available from `State::ReceiveAge`.
|
//! full_name: String, // Available from `State::ReceiveAge`.
|
||||||
|
@ -70,7 +70,7 @@
|
||||||
//! # type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
|
//! # type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
|
||||||
//! # #[derive(Clone, Debug)] enum State {}
|
//! # #[derive(Clone, Debug)] enum State {}
|
||||||
//! async fn receive_location(
|
//! async fn receive_location(
|
||||||
//! bot: AutoSend<Bot>,
|
//! bot: Bot,
|
||||||
//! msg: Message,
|
//! msg: Message,
|
||||||
//! dialogue: MyDialogue,
|
//! dialogue: MyDialogue,
|
||||||
//! (full_name, age): (String, u8), // Available from `State::ReceiveLocation`.
|
//! (full_name, age): (String, u8), // Available from `State::ReceiveLocation`.
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
| `webhooks-axum` | Enables webhook implementation based on axum framework |
|
| `webhooks-axum` | Enables webhook implementation based on axum framework |
|
||||||
| `macros` | Re-exports macros from [`teloxide-macros`]. |
|
| `macros` | Re-exports macros from [`teloxide-macros`]. |
|
||||||
| `ctrlc_handler` | Enables the [`DispatcherBuilder::enable_ctrlc_handler`] function (**enabled by default**). |
|
| `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. |
|
| `throttle` | Enables the [`Throttle`](adaptors::Throttle) bot adaptor. |
|
||||||
| `cache-me` | Enables the [`CacheMe`](adaptors::CacheMe) bot adaptor. |
|
| `cache-me` | Enables the [`CacheMe`](adaptors::CacheMe) bot adaptor. |
|
||||||
| `trace-adaptor` | Enables the [`Trace`](adaptors::Trace) bot adaptor. |
|
| `trace-adaptor` | Enables the [`Trace`](adaptors::Trace) bot adaptor. |
|
||||||
|
|
|
@ -13,9 +13,9 @@
|
||||||
//! pretty_env_logger::init();
|
//! pretty_env_logger::init();
|
||||||
//! log::info!("Starting throw dice bot...");
|
//! 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?;
|
//! bot.send_dice(message.chat.id).await?;
|
||||||
//! respond(())
|
//! respond(())
|
||||||
//! })
|
//! })
|
||||||
|
|
|
@ -15,6 +15,7 @@ pub use teloxide_core::types::{
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(feature = "auto-send")]
|
#[cfg(feature = "auto-send")]
|
||||||
|
#[allow(deprecated)]
|
||||||
pub use crate::adaptors::AutoSend;
|
pub use crate::adaptors::AutoSend;
|
||||||
|
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
|
|
Loading…
Reference in a new issue