mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-03 09:49:07 +01:00
Use Box<dyn Error + Send + Sync> in the examples
This commit is contained in:
parent
d625b69938
commit
09cca0faff
7 changed files with 25 additions and 13 deletions
|
@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
`#[non_exhaustive]` annotation is removed from the enum, type of `TargetMessage::Inline::inline_message_id` changed
|
||||
`i32` => `String`. `TargetMessage` now implements `From<String>`, `get_game_high_scores` and `set_game_score` use
|
||||
`Into<TargetMessage>` to accept `String`s. ([issue 253], [pr 257])
|
||||
- Remove `ResponseResult` from `prelude`.
|
||||
|
||||
[issue 253]: https://github.com/teloxide/teloxide/issues/253
|
||||
[pr 257]: https://github.com/teloxide/teloxide/pull/257
|
||||
|
|
13
README.md
13
README.md
|
@ -130,7 +130,9 @@ Commands are strongly typed and defined declaratively, similar to how we define
|
|||
|
||||
([Full](./examples/simple_commands_bot/src/main.rs))
|
||||
```rust,no_run
|
||||
use teloxide::{utils::command::BotCommand, prelude::*};
|
||||
use teloxide::{prelude::*, utils::command::BotCommand};
|
||||
|
||||
use std::error::Error;
|
||||
|
||||
#[derive(BotCommand)]
|
||||
#[command(rename = "lowercase", description = "These commands are supported:")]
|
||||
|
@ -143,14 +145,17 @@ enum Command {
|
|||
UsernameAndAge { username: String, age: u8 },
|
||||
}
|
||||
|
||||
async fn answer(cx: UpdateWithCx<AutoSend<Bot>, Message>, command: Command) -> ResponseResult<()> {
|
||||
async fn answer(
|
||||
cx: UpdateWithCx<AutoSend<Bot>, Message>,
|
||||
command: Command,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
match command {
|
||||
Command::Help => cx.answer(Command::descriptions()).send().await?,
|
||||
Command::Username(username) => {
|
||||
cx.answer_str(format!("Your username is @{}.", username)).await?
|
||||
cx.answer(format!("Your username is @{}.", username)).await?
|
||||
}
|
||||
Command::UsernameAndAge { username, age } => {
|
||||
cx.answer_str(format!("Your username is @{} and age is {}.", username, age)).await?
|
||||
cx.answer(format!("Your username is @{} and age is {}.", username, age)).await?
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use std::{convert::TryInto, str::FromStr};
|
||||
use std::{convert::TryInto, error::Error, str::FromStr};
|
||||
|
||||
use teloxide::{prelude::*, utils::command::BotCommand};
|
||||
|
||||
|
@ -65,7 +65,7 @@ fn calc_restrict_time(time: u32, unit: UnitOfTime) -> u32 {
|
|||
type Cx = UpdateWithCx<AutoSend<Bot>, Message>;
|
||||
|
||||
// Mute a user with a replied message.
|
||||
async fn mute_user(cx: &Cx, time: u32) -> ResponseResult<()> {
|
||||
async fn mute_user(cx: &Cx, time: u32) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
match cx.update.reply_to_message() {
|
||||
Some(msg1) => {
|
||||
cx.requester
|
||||
|
@ -85,7 +85,7 @@ async fn mute_user(cx: &Cx, time: u32) -> ResponseResult<()> {
|
|||
}
|
||||
|
||||
// Kick a user with a replied message.
|
||||
async fn kick_user(cx: &Cx) -> ResponseResult<()> {
|
||||
async fn kick_user(cx: &Cx) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
match cx.update.reply_to_message() {
|
||||
Some(mes) => {
|
||||
// bot.unban_chat_member can also kicks a user from a group chat.
|
||||
|
@ -102,7 +102,7 @@ async fn kick_user(cx: &Cx) -> ResponseResult<()> {
|
|||
}
|
||||
|
||||
// Ban a user with replied message.
|
||||
async fn ban_user(cx: &Cx, time: u32) -> ResponseResult<()> {
|
||||
async fn ban_user(cx: &Cx, time: u32) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
match cx.update.reply_to_message() {
|
||||
Some(message) => {
|
||||
cx.requester
|
||||
|
@ -120,7 +120,7 @@ async fn ban_user(cx: &Cx, time: u32) -> ResponseResult<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn action(cx: Cx, command: Command) -> ResponseResult<()> {
|
||||
async fn action(cx: Cx, command: Command) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
match command {
|
||||
Command::Help => cx.answer(Command::descriptions()).send().await.map(|_| ())?,
|
||||
Command::Kick => kick_user(&cx).await?,
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
use teloxide::{prelude::*, utils::command::BotCommand};
|
||||
|
||||
use std::error::Error;
|
||||
|
||||
#[derive(BotCommand)]
|
||||
#[command(rename = "lowercase", description = "These commands are supported:")]
|
||||
enum Command {
|
||||
|
@ -11,7 +13,10 @@ enum Command {
|
|||
UsernameAndAge { username: String, age: u8 },
|
||||
}
|
||||
|
||||
async fn answer(cx: UpdateWithCx<AutoSend<Bot>, Message>, command: Command) -> ResponseResult<()> {
|
||||
async fn answer(
|
||||
cx: UpdateWithCx<AutoSend<Bot>, Message>,
|
||||
command: Command,
|
||||
) -> Result<(), Box<dyn Error + Send + Sync>> {
|
||||
match command {
|
||||
Command::Help => cx.answer(Command::descriptions()).send().await?,
|
||||
Command::Username(username) => {
|
||||
|
|
|
@ -29,7 +29,7 @@ async fn handle_message(
|
|||
cx: UpdateWithCx<AutoSend<Bot>, Message>,
|
||||
dialogue: Dialogue,
|
||||
) -> TransitionOut<Dialogue> {
|
||||
match cx.update.text_owned() {
|
||||
match cx.update.text().map(ToOwned::to_owned) {
|
||||
None => {
|
||||
cx.answer("Send me a text message.").await?;
|
||||
next(dialogue)
|
||||
|
|
|
@ -66,7 +66,6 @@ pub mod utils;
|
|||
#[doc(inline)]
|
||||
pub use teloxide_core::*;
|
||||
|
||||
use teloxide_core::requests::ResponseResult;
|
||||
#[cfg(feature = "macros")]
|
||||
// FIXME(waffle): use `docsrs` here when issue with combine is resolved <https://github.com/teloxide/teloxide/pull/305#issuecomment-716172103>
|
||||
#[cfg_attr(all(teloxide_docsrs, feature = "nightly"), doc(cfg(feature = "macros")))]
|
||||
|
@ -76,6 +75,8 @@ pub use teloxide_macros::teloxide;
|
|||
#[doc(include = "../README.md")]
|
||||
enum ReadmeDocTests {}
|
||||
|
||||
use teloxide_core::requests::ResponseResult;
|
||||
|
||||
/// A shortcut for `ResponseResult::Ok(val)`.
|
||||
pub fn respond<T>(val: T) -> ResponseResult<T> {
|
||||
ResponseResult::Ok(val)
|
||||
|
|
|
@ -14,7 +14,7 @@ pub use crate::{
|
|||
|
||||
pub use teloxide_core::{
|
||||
adaptors::AutoSend,
|
||||
requests::{Request, ResponseResult},
|
||||
requests::Request,
|
||||
types::{
|
||||
CallbackQuery, ChatMemberUpdated, ChosenInlineResult, InlineQuery, Message, Poll,
|
||||
PollAnswer, PreCheckoutQuery, ShippingQuery,
|
||||
|
|
Loading…
Reference in a new issue