Merge branch 'teloxide:master' into master

This commit is contained in:
puuuuh 2023-02-07 10:02:47 +03:00 committed by GitHub
commit 18a5444f82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 87 additions and 31 deletions

View file

@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## unreleased
### Fixed
- Allow `ChatJoinRequest` updates
### Added
- `Update::filter_chat_join_request`
## 0.12.0 - 2023-01-17
### Changed

View file

@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## unreleased
### Fixed
- `Update::user` now handles channel posts, chat member changes and chat join request updates correctly ([#835][pr835])
[pr835]: https://github.com/teloxide/teloxide/pull/835
## 0.9.0 - 2023-01-17
### Changed

View file

@ -17,6 +17,8 @@ categories = ["api-bindings", "asynchronous"]
[features]
# NB: When adding features here, don't forget to update teloxide's Cargo.toml
default = ["native-tls"]
rustls = ["reqwest/rustls-tls"]

View file

@ -100,8 +100,9 @@ pub enum ApiError {
BotBlocked,
/// Occurs when the bot token is incorrect.
#[serde(rename = "Not Found")]
#[error("Not Found")]
// FIXME: rename this to something akin "InvalidToken"
#[serde(rename = "Unauthorized")]
#[error("Unauthorized")]
NotFound,
/// Occurs when bot tries to modify a message without modification content.

View file

@ -29,39 +29,69 @@ pub struct Update {
}
impl Update {
// FIXME: rename user => from, add mentioned_users -> impl Iterator<&User>
/// Returns the user that performed the action that caused this update, if
/// known.
///
/// This is generally the `from` field (except for `PollAnswer` where it's
/// `user` and `Poll` with `Error` which don't have such field at all).
#[must_use]
pub fn user(&self) -> Option<&User> {
match &self.kind {
UpdateKind::Message(m) => m.from(),
UpdateKind::EditedMessage(m) => m.from(),
UpdateKind::CallbackQuery(query) => Some(&query.from),
UpdateKind::ChosenInlineResult(chosen) => Some(&chosen.from),
UpdateKind::InlineQuery(query) => Some(&query.from),
UpdateKind::ShippingQuery(query) => Some(&query.from),
UpdateKind::PreCheckoutQuery(query) => Some(&query.from),
UpdateKind::PollAnswer(answer) => Some(&answer.user),
_ => None,
}
use UpdateKind::*;
let from = match &self.kind {
Message(m) | EditedMessage(m) | ChannelPost(m) | EditedChannelPost(m) => m.from()?,
CallbackQuery(query) => &query.from,
ChosenInlineResult(chosen) => &chosen.from,
InlineQuery(query) => &query.from,
ShippingQuery(query) => &query.from,
PreCheckoutQuery(query) => &query.from,
PollAnswer(answer) => &answer.user,
MyChatMember(m) | ChatMember(m) => &m.from,
ChatJoinRequest(r) => &r.from,
Poll(_) | Error(_) => return None,
};
Some(from)
}
/// Returns the chat in which is update has happened, if any.
#[must_use]
pub fn chat(&self) -> Option<&Chat> {
match &self.kind {
UpdateKind::Message(m) => Some(&m.chat),
UpdateKind::EditedMessage(m) => Some(&m.chat),
UpdateKind::ChannelPost(p) => Some(&p.chat),
UpdateKind::EditedChannelPost(p) => Some(&p.chat),
UpdateKind::CallbackQuery(q) => Some(&q.message.as_ref()?.chat),
UpdateKind::ChatMember(m) => Some(&m.chat),
UpdateKind::MyChatMember(m) => Some(&m.chat),
UpdateKind::ChatJoinRequest(c) => Some(&c.chat),
_ => None,
}
use UpdateKind::*;
let chat = match &self.kind {
Message(m) | EditedMessage(m) | ChannelPost(m) | EditedChannelPost(m) => &m.chat,
CallbackQuery(q) => &q.message.as_ref()?.chat,
ChatMember(m) => &m.chat,
MyChatMember(m) => &m.chat,
ChatJoinRequest(c) => &c.chat,
InlineQuery(_)
| ChosenInlineResult(_)
| ShippingQuery(_)
| PreCheckoutQuery(_)
| Poll(_)
| PollAnswer(_)
| Error(_) => return None,
};
Some(chat)
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum UpdateKind {
// NB: When adding new variants, don't forget to update
// - `AllowedUpdate`
// - `Update::user`
// - `Update::chat`
// - `DpHandlerDescription::full_set`
// - `dispatching/filter_ext.rs`
/// New incoming message of any kind — text, photo, sticker, etc.
Message(Message),

View file

@ -63,17 +63,15 @@ full = [
[dependencies]
teloxide-core = { version = "0.9.0", default-features = false }
teloxide-macros = { version = "0.7.1", optional = true }
teloxide-core = { version = "0.9.0", path = "../teloxide-core", default-features = false }
teloxide-macros = { version = "0.7.1", path = "../teloxide-macros", optional = true }
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
dptree = "0.3.0"
# These lines are used only for development.
# teloxide-core = { path = "../teloxide-core", default-features = false }
# teloxide-macros = { path = "../teloxide-macros", optional = true }
# Uncomment this if you want to test teloxide with a specific dptree commit
# dptree = { git = "https://github.com/teloxide/dptree", rev = "df578e4" }
tokio = { version = "1.8", features = ["fs"] }

View file

@ -103,7 +103,6 @@ macro_rules! define_update_ext {
}
}
// May be expanded in the future.
define_update_ext! {
(filter_message, UpdateKind::Message, Message),
(filter_edited_message, UpdateKind::EditedMessage, EditedMessage),
@ -118,4 +117,5 @@ define_update_ext! {
(filter_poll_answer, UpdateKind::PollAnswer, PollAnswer),
(filter_my_chat_member, UpdateKind::MyChatMember, MyChatMember),
(filter_chat_member, UpdateKind::ChatMember, ChatMember),
(filter_chat_join_request, UpdateKind::ChatJoinRequest, ChatJoinRequest),
}

View file

@ -21,7 +21,7 @@ impl DpHandlerDescription {
}
pub(crate) fn allowed_updates(&self) -> Vec<AllowedUpdate> {
self.allowed.observed.iter().map(|Kind(x)| x).copied().collect()
self.allowed.observed.iter().map(|&Kind(x)| x).collect()
}
}
@ -50,6 +50,16 @@ impl EventKind for Kind {
fn full_set() -> HashSet<Self> {
use AllowedUpdate::*;
// NB: We need to specify all update kinds by hand, because telegram doesn't
// enable `ChatMember` by default:
//
// > A JSON-serialized list of the update types you want your bot to
// > receive. For example, specify [“message”, “edited_channel_post”,
// > “callback_query”] to only receive updates of these types. See Update
// > for a complete list of available update types. Specify an empty list
// > to receive all update types except chat_member (default). If not
// > specified, the previous setting will be used.
[
Message,
EditedMessage,
@ -64,6 +74,7 @@ impl EventKind for Kind {
PollAnswer,
MyChatMember,
ChatMember,
ChatJoinRequest,
]
.into_iter()
.map(Kind)