mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 22:46:39 +01:00
Merge branch 'teloxide:master' into master
This commit is contained in:
commit
18a5444f82
8 changed files with 87 additions and 31 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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"]
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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),
|
||||
|
||||
|
|
|
@ -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"] }
|
||||
|
|
|
@ -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),
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue