Merge pull request #346 from teloxide/support-new-update-kind-types

Support new UpdateKind types
This commit is contained in:
Hirrolot 2021-03-18 02:08:53 +06:00 committed by GitHub
commit f2026b238b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 55 additions and 7 deletions

View file

@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Allow arbitrary error types to be returned from (sub)transitions ([issue 242](https://github.com/teloxide/teloxide/issues/242)). - Allow arbitrary error types to be returned from (sub)transitions ([issue 242](https://github.com/teloxide/teloxide/issues/242)).
- The `respond` function, a shortcut for `ResponseResult::Ok(())`. - The `respond` function, a shortcut for `ResponseResult::Ok(())`.
- The `sqlite-storage` feature -- enables SQLite support. - The `sqlite-storage` feature -- enables SQLite support.
- `Dispatcher::{my_chat_members_handler, chat_members_handler}`
[teloxide-core]: https://github.com/teloxide/teloxide-core
### Deprecated ### Deprecated

View file

@ -36,7 +36,7 @@ macros = ["teloxide-macros"]
nightly = [] # currently used for `README.md` tests and building docs for `docsrs` to add `This is supported on feature="..." only.` nightly = [] # currently used for `README.md` tests and building docs for `docsrs` to add `This is supported on feature="..." only.`
[dependencies] [dependencies]
teloxide-core = { git = "https://github.com/teloxide/teloxide-core.git", features = ["full"] } teloxide-core = { version = "0.2", features = ["full"] }
serde_json = "1.0" serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }

View file

@ -46,7 +46,7 @@ async fn handle_message(
cx: UpdateWithCx<AutoSend<Bot>, Message>, cx: UpdateWithCx<AutoSend<Bot>, Message>,
dialogue: Dialogue, dialogue: Dialogue,
) -> TransitionOut<Dialogue> { ) -> TransitionOut<Dialogue> {
match cx.update.text_owned() { match cx.update.text().map(ToOwned::to_owned) {
None => { None => {
cx.answer("Send me a text message.").await?; cx.answer("Send me a text message.").await?;
next(dialogue) next(dialogue)

View file

@ -52,7 +52,7 @@ async fn handle_message(
cx: UpdateWithCx<AutoSend<Bot>, Message>, cx: UpdateWithCx<AutoSend<Bot>, Message>,
dialogue: Dialogue, dialogue: Dialogue,
) -> TransitionOut<Dialogue> { ) -> TransitionOut<Dialogue> {
match cx.update.text_owned() { match cx.update.text().map(ToOwned::to_owned) {
None => { None => {
cx.answer("Send me a text message.").await?; cx.answer("Send me a text message.").await?;
next(dialogue) next(dialogue)

View file

@ -9,8 +9,8 @@ use std::{fmt::Debug, sync::Arc};
use teloxide_core::{ use teloxide_core::{
requests::Requester, requests::Requester,
types::{ types::{
CallbackQuery, ChosenInlineResult, InlineQuery, Message, Poll, PollAnswer, CallbackQuery, ChatMemberUpdated, ChosenInlineResult, InlineQuery, Message, Poll,
PreCheckoutQuery, ShippingQuery, UpdateKind, PollAnswer, PreCheckoutQuery, ShippingQuery, UpdateKind,
}, },
}; };
use tokio::sync::mpsc; use tokio::sync::mpsc;
@ -61,6 +61,8 @@ pub struct Dispatcher<R> {
pre_checkout_queries_queue: Tx<R, PreCheckoutQuery>, pre_checkout_queries_queue: Tx<R, PreCheckoutQuery>,
polls_queue: Tx<R, Poll>, polls_queue: Tx<R, Poll>,
poll_answers_queue: Tx<R, PollAnswer>, poll_answers_queue: Tx<R, PollAnswer>,
my_chat_members_queue: Tx<R, ChatMemberUpdated>,
chat_members_queue: Tx<R, ChatMemberUpdated>,
} }
impl<R> Dispatcher<R> impl<R> Dispatcher<R>
@ -83,6 +85,8 @@ where
pre_checkout_queries_queue: None, pre_checkout_queries_queue: None,
polls_queue: None, polls_queue: None,
poll_answers_queue: None, poll_answers_queue: None,
my_chat_members_queue: None,
chat_members_queue: None,
} }
} }
@ -201,6 +205,24 @@ where
self self
} }
#[must_use]
pub fn my_chat_members_handler<H>(mut self, h: H) -> Self
where
H: DispatcherHandler<R, ChatMemberUpdated> + 'static + Send,
{
self.my_chat_members_queue = self.new_tx(h);
self
}
#[must_use]
pub fn chat_members_handler<H>(mut self, h: H) -> Self
where
H: DispatcherHandler<R, ChatMemberUpdated> + 'static + Send,
{
self.chat_members_queue = self.new_tx(h);
self
}
/// Starts your bot with the default parameters. /// Starts your bot with the default parameters.
/// ///
/// The default parameters are a long polling update listener and log all /// The default parameters are a long polling update listener and log all
@ -330,6 +352,22 @@ where
UpdateKind::PollAnswer UpdateKind::PollAnswer
); );
} }
UpdateKind::MyChatMember(chat_member_updated) => {
send!(
&self.requester,
&self.my_chat_members_queue,
chat_member_updated,
UpdateKind::MyChatMember
);
}
UpdateKind::ChatMember(chat_member_updated) => {
send!(
&self.requester,
&self.chat_members_queue,
chat_member_updated,
UpdateKind::MyChatMember
);
}
} }
} }
}) })

View file

@ -34,7 +34,11 @@ where
Self: Stream<Item = UpdateWithCx<R, Message>>, Self: Stream<Item = UpdateWithCx<R, Message>>,
R: Send + 'static, R: Send + 'static,
{ {
self.filter_map(|cx| async move { cx.update.text_owned().map(|text| (cx, text)) }).boxed() self.filter_map(|cx| async move {
let text = cx.update.text().map(ToOwned::to_owned);
text.map(move |text| (cx, text))
})
.boxed()
} }
fn commands<C, N>(self, bot_name: N) -> BoxStream<'static, (UpdateWithCx<R, Message>, C)> fn commands<C, N>(self, bot_name: N) -> BoxStream<'static, (UpdateWithCx<R, Message>, C)>

View file

@ -15,7 +15,10 @@ pub use crate::{
pub use teloxide_core::{ pub use teloxide_core::{
adaptors::AutoSend, adaptors::AutoSend,
requests::{Request, ResponseResult}, requests::{Request, ResponseResult},
types::Message, types::{
CallbackQuery, ChatMemberUpdated, ChosenInlineResult, InlineQuery, Message, Poll,
PollAnswer, PreCheckoutQuery, ShippingQuery,
},
}; };
#[doc(inline)] #[doc(inline)]