Struct teloxide::dispatching::DispatcherBuilder
source · pub struct DispatcherBuilder<R, Err, Key> { /* private fields */ }
Expand description
The builder for Dispatcher
.
See also: “Dispatching or REPLs?”
Implementations§
source§impl<R, Err, Key> DispatcherBuilder<R, Err, Key>where
R: Clone + Requester + Send + Sync + 'static,
Err: Debug + Send + Sync + 'static,
impl<R, Err, Key> DispatcherBuilder<R, Err, Key>where R: Clone + Requester + Send + Sync + 'static, Err: Debug + Send + Sync + 'static,
sourcepub fn default_handler<H, Fut>(self, handler: H) -> Selfwhere
H: Fn(Arc<Update>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ()> + Send + 'static,
pub fn default_handler<H, Fut>(self, handler: H) -> Selfwhere H: Fn(Arc<Update>) -> Fut + Send + Sync + 'static, Fut: Future<Output = ()> + Send + 'static,
Specifies a handler that will be called for an unhandled update.
By default, it is a mere log::warn
.
Examples found in repository?
examples/dispatching_features.rs (lines 79-81)
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
async fn main() {
pretty_env_logger::init();
log::info!("Starting dispatching features bot...");
let bot = Bot::from_env();
let parameters = ConfigParameters {
bot_maintainer: UserId(0), // Paste your ID to run this bot.
maintainer_username: None,
};
let handler = Update::filter_message()
// You can use branching to define multiple ways in which an update will be handled. If the
// first branch fails, an update will be passed to the second branch, and so on.
.branch(
dptree::entry()
// Filter commands: the next handlers will receive a parsed `SimpleCommand`.
.filter_command::<SimpleCommand>()
// If a command parsing fails, this handler will not be executed.
.endpoint(simple_commands_handler),
)
.branch(
// Filter a maintainer by a user ID.
dptree::filter(|cfg: ConfigParameters, msg: Message| {
msg.from().map(|user| user.id == cfg.bot_maintainer).unwrap_or_default()
})
.filter_command::<MaintainerCommands>()
.endpoint(|msg: Message, bot: 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);
bot.send_message(msg.chat.id, value.to_string()).await?;
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: 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(())
}),
)
.branch(
// There are some extension filtering functions on `Message`. The following filter will
// filter only messages with dices.
Message::filter_dice().endpoint(|bot: Bot, msg: Message, dice: Dice| 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)
// Here you specify initial dependencies that all handlers will receive; they can be
// database connections, configurations, and other auxiliary arguments. It is similar to
// `actix_web::Extensions`.
.dependencies(dptree::deps![parameters])
// If no handler succeeded to handle an update, this closure will be called.
.default_handler(|upd| async move {
log::warn!("Unhandled update: {:?}", upd);
})
// If the dispatcher fails for some reason, execute this handler.
.error_handler(LoggingErrorHandler::with_custom_text(
"An error has occurred in the dispatcher",
))
.enable_ctrlc_handler()
.build()
.dispatch()
.await;
}
sourcepub fn error_handler(
self,
handler: Arc<dyn ErrorHandler<Err> + Send + Sync>
) -> Self
pub fn error_handler( self, handler: Arc<dyn ErrorHandler<Err> + Send + Sync> ) -> Self
Specifies a handler that will be called on a handler error.
By default, it is LoggingErrorHandler
.
Examples found in repository?
examples/dispatching_features.rs (lines 83-85)
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
async fn main() {
pretty_env_logger::init();
log::info!("Starting dispatching features bot...");
let bot = Bot::from_env();
let parameters = ConfigParameters {
bot_maintainer: UserId(0), // Paste your ID to run this bot.
maintainer_username: None,
};
let handler = Update::filter_message()
// You can use branching to define multiple ways in which an update will be handled. If the
// first branch fails, an update will be passed to the second branch, and so on.
.branch(
dptree::entry()
// Filter commands: the next handlers will receive a parsed `SimpleCommand`.
.filter_command::<SimpleCommand>()
// If a command parsing fails, this handler will not be executed.
.endpoint(simple_commands_handler),
)
.branch(
// Filter a maintainer by a user ID.
dptree::filter(|cfg: ConfigParameters, msg: Message| {
msg.from().map(|user| user.id == cfg.bot_maintainer).unwrap_or_default()
})
.filter_command::<MaintainerCommands>()
.endpoint(|msg: Message, bot: 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);
bot.send_message(msg.chat.id, value.to_string()).await?;
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: 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(())
}),
)
.branch(
// There are some extension filtering functions on `Message`. The following filter will
// filter only messages with dices.
Message::filter_dice().endpoint(|bot: Bot, msg: Message, dice: Dice| 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)
// Here you specify initial dependencies that all handlers will receive; they can be
// database connections, configurations, and other auxiliary arguments. It is similar to
// `actix_web::Extensions`.
.dependencies(dptree::deps![parameters])
// If no handler succeeded to handle an update, this closure will be called.
.default_handler(|upd| async move {
log::warn!("Unhandled update: {:?}", upd);
})
// If the dispatcher fails for some reason, execute this handler.
.error_handler(LoggingErrorHandler::with_custom_text(
"An error has occurred in the dispatcher",
))
.enable_ctrlc_handler()
.build()
.dispatch()
.await;
}
sourcepub fn dependencies(self, dependencies: DependencyMap) -> Self
pub fn dependencies(self, dependencies: DependencyMap) -> Self
Specifies dependencies that can be used inside of handlers.
By default, there is no dependencies.
Examples found in repository?
examples/purchase.rs (line 54)
47 48 49 50 51 52 53 54 55 56 57 58 59
async fn main() {
pretty_env_logger::init();
log::info!("Starting purchase bot...");
let bot = Bot::from_env();
Dispatcher::builder(bot, schema())
.dependencies(dptree::deps![InMemStorage::<State>::new()])
.enable_ctrlc_handler()
.build()
.dispatch()
.await;
}
More examples
examples/dialogue.rs (line 53)
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
async fn main() {
pretty_env_logger::init();
log::info!("Starting dialogue bot...");
let bot = Bot::from_env();
Dispatcher::builder(
bot,
Update::filter_message()
.enter_dialogue::<Message, InMemStorage<State>, State>()
.branch(dptree::case![State::Start].endpoint(start))
.branch(dptree::case![State::ReceiveFullName].endpoint(receive_full_name))
.branch(dptree::case![State::ReceiveAge { full_name }].endpoint(receive_age))
.branch(
dptree::case![State::ReceiveLocation { full_name, age }].endpoint(receive_location),
),
)
.dependencies(dptree::deps![InMemStorage::<State>::new()])
.enable_ctrlc_handler()
.build()
.dispatch()
.await;
}
examples/shared_state.rs (line 29)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
async fn main() {
pretty_env_logger::init();
log::info!("Starting shared state bot...");
let bot = Bot::from_env();
let messages_total = Arc::new(AtomicU64::new(0));
let handler = Update::filter_message().endpoint(
|bot: Bot, messages_total: Arc<AtomicU64>, msg: Message| 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?;
respond(())
},
);
Dispatcher::builder(bot, handler)
// Pass the shared state to the handler as a dependency.
.dependencies(dptree::deps![messages_total])
.enable_ctrlc_handler()
.build()
.dispatch()
.await;
}
examples/db_remember.rs (line 56)
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
async fn main() {
pretty_env_logger::init();
log::info!("Starting DB remember bot...");
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()
} else {
SqliteStorage::open("db.sqlite", Json).await.unwrap().erase()
};
let handler = Update::filter_message()
.enter_dialogue::<Message, ErasedStorage<State>, State>()
.branch(dptree::case![State::Start].endpoint(start))
.branch(
dptree::case![State::GotNumber(n)]
.branch(dptree::entry().filter_command::<Command>().endpoint(got_number))
.branch(dptree::endpoint(invalid_command)),
);
Dispatcher::builder(bot, handler)
.dependencies(dptree::deps![storage])
.enable_ctrlc_handler()
.build()
.dispatch()
.await;
}
examples/dispatching_features.rs (line 77)
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
async fn main() {
pretty_env_logger::init();
log::info!("Starting dispatching features bot...");
let bot = Bot::from_env();
let parameters = ConfigParameters {
bot_maintainer: UserId(0), // Paste your ID to run this bot.
maintainer_username: None,
};
let handler = Update::filter_message()
// You can use branching to define multiple ways in which an update will be handled. If the
// first branch fails, an update will be passed to the second branch, and so on.
.branch(
dptree::entry()
// Filter commands: the next handlers will receive a parsed `SimpleCommand`.
.filter_command::<SimpleCommand>()
// If a command parsing fails, this handler will not be executed.
.endpoint(simple_commands_handler),
)
.branch(
// Filter a maintainer by a user ID.
dptree::filter(|cfg: ConfigParameters, msg: Message| {
msg.from().map(|user| user.id == cfg.bot_maintainer).unwrap_or_default()
})
.filter_command::<MaintainerCommands>()
.endpoint(|msg: Message, bot: 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);
bot.send_message(msg.chat.id, value.to_string()).await?;
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: 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(())
}),
)
.branch(
// There are some extension filtering functions on `Message`. The following filter will
// filter only messages with dices.
Message::filter_dice().endpoint(|bot: Bot, msg: Message, dice: Dice| 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)
// Here you specify initial dependencies that all handlers will receive; they can be
// database connections, configurations, and other auxiliary arguments. It is similar to
// `actix_web::Extensions`.
.dependencies(dptree::deps![parameters])
// If no handler succeeded to handle an update, this closure will be called.
.default_handler(|upd| async move {
log::warn!("Unhandled update: {:?}", upd);
})
// If the dispatcher fails for some reason, execute this handler.
.error_handler(LoggingErrorHandler::with_custom_text(
"An error has occurred in the dispatcher",
))
.enable_ctrlc_handler()
.build()
.dispatch()
.await;
}
sourcepub fn enable_ctrlc_handler(self) -> Self
Available on crate feature ctrlc_handler
only.
pub fn enable_ctrlc_handler(self) -> Self
ctrlc_handler
only.Enables the ^C
handler that shutdown
s dispatching.
Examples found in repository?
examples/purchase.rs (line 55)
47 48 49 50 51 52 53 54 55 56 57 58 59
async fn main() {
pretty_env_logger::init();
log::info!("Starting purchase bot...");
let bot = Bot::from_env();
Dispatcher::builder(bot, schema())
.dependencies(dptree::deps![InMemStorage::<State>::new()])
.enable_ctrlc_handler()
.build()
.dispatch()
.await;
}
More examples
examples/buttons.rs (line 33)
22 23 24 25 26 27 28 29 30 31 32 33 34 35
async fn main() -> Result<(), Box<dyn Error>> {
pretty_env_logger::init();
log::info!("Starting buttons bot...");
let bot = Bot::from_env();
let handler = dptree::entry()
.branch(Update::filter_message().endpoint(message_handler))
.branch(Update::filter_callback_query().endpoint(callback_handler))
.branch(Update::filter_inline_query().endpoint(inline_query_handler));
Dispatcher::builder(bot, handler).enable_ctrlc_handler().build().dispatch().await;
Ok(())
}
examples/dialogue.rs (line 54)
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
async fn main() {
pretty_env_logger::init();
log::info!("Starting dialogue bot...");
let bot = Bot::from_env();
Dispatcher::builder(
bot,
Update::filter_message()
.enter_dialogue::<Message, InMemStorage<State>, State>()
.branch(dptree::case![State::Start].endpoint(start))
.branch(dptree::case![State::ReceiveFullName].endpoint(receive_full_name))
.branch(dptree::case![State::ReceiveAge { full_name }].endpoint(receive_age))
.branch(
dptree::case![State::ReceiveLocation { full_name, age }].endpoint(receive_location),
),
)
.dependencies(dptree::deps![InMemStorage::<State>::new()])
.enable_ctrlc_handler()
.build()
.dispatch()
.await;
}
examples/shared_state.rs (line 30)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
async fn main() {
pretty_env_logger::init();
log::info!("Starting shared state bot...");
let bot = Bot::from_env();
let messages_total = Arc::new(AtomicU64::new(0));
let handler = Update::filter_message().endpoint(
|bot: Bot, messages_total: Arc<AtomicU64>, msg: Message| 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?;
respond(())
},
);
Dispatcher::builder(bot, handler)
// Pass the shared state to the handler as a dependency.
.dependencies(dptree::deps![messages_total])
.enable_ctrlc_handler()
.build()
.dispatch()
.await;
}
examples/db_remember.rs (line 57)
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
async fn main() {
pretty_env_logger::init();
log::info!("Starting DB remember bot...");
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()
} else {
SqliteStorage::open("db.sqlite", Json).await.unwrap().erase()
};
let handler = Update::filter_message()
.enter_dialogue::<Message, ErasedStorage<State>, State>()
.branch(dptree::case![State::Start].endpoint(start))
.branch(
dptree::case![State::GotNumber(n)]
.branch(dptree::entry().filter_command::<Command>().endpoint(got_number))
.branch(dptree::endpoint(invalid_command)),
);
Dispatcher::builder(bot, handler)
.dependencies(dptree::deps![storage])
.enable_ctrlc_handler()
.build()
.dispatch()
.await;
}
examples/inline.rs (line 63)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
async fn main() {
pretty_env_logger::init();
log::info!("Starting inline bot...");
let bot = Bot::from_env();
let handler = Update::filter_inline_query().branch(dptree::endpoint(
|bot: Bot, q: InlineQuery| 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
// items. These can be whatever, as long as they don't
// conflict.
"01".to_string(),
// What the user will actually see
"Google Search",
// What message will be sent when clicked/tapped
InputMessageContent::Text(InputMessageContentText::new(format!(
"https://www.google.com/search?q={}",
q.query,
))),
);
// While constructing them from the struct itself is possible, it is preferred
// to use the builder pattern if you wish to add more
// information to your result. Please refer to the documentation
// for more detailed information about each field. https://docs.rs/teloxide/latest/teloxide/types/struct.InlineQueryResultArticle.html
let ddg_search = InlineQueryResultArticle::new(
"02".to_string(),
"DuckDuckGo Search".to_string(),
InputMessageContent::Text(InputMessageContentText::new(format!(
"https://duckduckgo.com/?q={}",
q.query
))),
)
.description("DuckDuckGo Search")
.thumb_url("https://duckduckgo.com/assets/logo_header.v108.png".parse().unwrap())
.url("https://duckduckgo.com/about".parse().unwrap()); // Note: This is the url that will open if they click the thumbnail
let results = vec![
InlineQueryResult::Article(google_search),
InlineQueryResult::Article(ddg_search),
];
// Send it off! One thing to note -- the ID we use here must be of the query
// we're responding to.
let response = bot.answer_inline_query(&q.id, results).send().await;
if let Err(err) = response {
log::error!("Error in handler: {:?}", err);
}
respond(())
},
));
Dispatcher::builder(bot, handler).enable_ctrlc_handler().build().dispatch().await;
}
Additional examples can be found in:
sourcepub fn worker_queue_size(self, size: usize) -> Self
pub fn worker_queue_size(self, size: usize) -> Self
Specifies size of the queue for workers.
By default it’s 64.
sourcepub fn distribution_function<K>(
self,
f: fn(_: &Update) -> Option<K>
) -> DispatcherBuilder<R, Err, K>where
K: Hash + Eq,
pub fn distribution_function<K>( self, f: fn(_: &Update) -> Option<K> ) -> DispatcherBuilder<R, Err, K>where K: Hash + Eq,
Specifies the distribution function that decides how updates are grouped before execution.
sourcepub fn build(self) -> Dispatcher<R, Err, Key>
pub fn build(self) -> Dispatcher<R, Err, Key>
Constructs Dispatcher
.
Examples found in repository?
examples/purchase.rs (line 56)
47 48 49 50 51 52 53 54 55 56 57 58 59
async fn main() {
pretty_env_logger::init();
log::info!("Starting purchase bot...");
let bot = Bot::from_env();
Dispatcher::builder(bot, schema())
.dependencies(dptree::deps![InMemStorage::<State>::new()])
.enable_ctrlc_handler()
.build()
.dispatch()
.await;
}
More examples
examples/buttons.rs (line 33)
22 23 24 25 26 27 28 29 30 31 32 33 34 35
async fn main() -> Result<(), Box<dyn Error>> {
pretty_env_logger::init();
log::info!("Starting buttons bot...");
let bot = Bot::from_env();
let handler = dptree::entry()
.branch(Update::filter_message().endpoint(message_handler))
.branch(Update::filter_callback_query().endpoint(callback_handler))
.branch(Update::filter_inline_query().endpoint(inline_query_handler));
Dispatcher::builder(bot, handler).enable_ctrlc_handler().build().dispatch().await;
Ok(())
}
examples/dialogue.rs (line 55)
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
async fn main() {
pretty_env_logger::init();
log::info!("Starting dialogue bot...");
let bot = Bot::from_env();
Dispatcher::builder(
bot,
Update::filter_message()
.enter_dialogue::<Message, InMemStorage<State>, State>()
.branch(dptree::case![State::Start].endpoint(start))
.branch(dptree::case![State::ReceiveFullName].endpoint(receive_full_name))
.branch(dptree::case![State::ReceiveAge { full_name }].endpoint(receive_age))
.branch(
dptree::case![State::ReceiveLocation { full_name, age }].endpoint(receive_location),
),
)
.dependencies(dptree::deps![InMemStorage::<State>::new()])
.enable_ctrlc_handler()
.build()
.dispatch()
.await;
}
examples/shared_state.rs (line 31)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
async fn main() {
pretty_env_logger::init();
log::info!("Starting shared state bot...");
let bot = Bot::from_env();
let messages_total = Arc::new(AtomicU64::new(0));
let handler = Update::filter_message().endpoint(
|bot: Bot, messages_total: Arc<AtomicU64>, msg: Message| 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?;
respond(())
},
);
Dispatcher::builder(bot, handler)
// Pass the shared state to the handler as a dependency.
.dependencies(dptree::deps![messages_total])
.enable_ctrlc_handler()
.build()
.dispatch()
.await;
}
examples/db_remember.rs (line 58)
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
async fn main() {
pretty_env_logger::init();
log::info!("Starting DB remember bot...");
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()
} else {
SqliteStorage::open("db.sqlite", Json).await.unwrap().erase()
};
let handler = Update::filter_message()
.enter_dialogue::<Message, ErasedStorage<State>, State>()
.branch(dptree::case![State::Start].endpoint(start))
.branch(
dptree::case![State::GotNumber(n)]
.branch(dptree::entry().filter_command::<Command>().endpoint(got_number))
.branch(dptree::endpoint(invalid_command)),
);
Dispatcher::builder(bot, handler)
.dependencies(dptree::deps![storage])
.enable_ctrlc_handler()
.build()
.dispatch()
.await;
}
examples/inline.rs (line 63)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
async fn main() {
pretty_env_logger::init();
log::info!("Starting inline bot...");
let bot = Bot::from_env();
let handler = Update::filter_inline_query().branch(dptree::endpoint(
|bot: Bot, q: InlineQuery| 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
// items. These can be whatever, as long as they don't
// conflict.
"01".to_string(),
// What the user will actually see
"Google Search",
// What message will be sent when clicked/tapped
InputMessageContent::Text(InputMessageContentText::new(format!(
"https://www.google.com/search?q={}",
q.query,
))),
);
// While constructing them from the struct itself is possible, it is preferred
// to use the builder pattern if you wish to add more
// information to your result. Please refer to the documentation
// for more detailed information about each field. https://docs.rs/teloxide/latest/teloxide/types/struct.InlineQueryResultArticle.html
let ddg_search = InlineQueryResultArticle::new(
"02".to_string(),
"DuckDuckGo Search".to_string(),
InputMessageContent::Text(InputMessageContentText::new(format!(
"https://duckduckgo.com/?q={}",
q.query
))),
)
.description("DuckDuckGo Search")
.thumb_url("https://duckduckgo.com/assets/logo_header.v108.png".parse().unwrap())
.url("https://duckduckgo.com/about".parse().unwrap()); // Note: This is the url that will open if they click the thumbnail
let results = vec![
InlineQueryResult::Article(google_search),
InlineQueryResult::Article(ddg_search),
];
// Send it off! One thing to note -- the ID we use here must be of the query
// we're responding to.
let response = bot.answer_inline_query(&q.id, results).send().await;
if let Err(err) = response {
log::error!("Error in handler: {:?}", err);
}
respond(())
},
));
Dispatcher::builder(bot, handler).enable_ctrlc_handler().build().dispatch().await;
}
Additional examples can be found in: