From be7bec6fa90ec82ec7184ce5a449849fce29aeb1 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Wed, 17 Mar 2021 23:36:51 +0600 Subject: [PATCH 1/3] Support UpdateKind::{MyChatMember, ChatMember} --- CHANGELOG.md | 3 ++ Cargo.toml | 2 +- src/dispatching/dispatcher.rs | 42 +++++++++++++++++++- src/dispatching/dispatcher_handler_rx_ext.rs | 6 ++- src/prelude.rs | 5 ++- 5 files changed, 53 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f1a9643..060024a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)). - The `respond` function, a shortcut for `ResponseResult::Ok(())`. - The `sqlite-storage` feature -- enables SQLite support. + - `Dispatcher::{my_chat_members_handler, chat_members_handler}` + +[teloxide-core]: https://github.com/teloxide/teloxide-core ### Deprecated diff --git a/Cargo.toml b/Cargo.toml index 40b0133b..04d36c73 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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.` [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 = { version = "1.0", features = ["derive"] } diff --git a/src/dispatching/dispatcher.rs b/src/dispatching/dispatcher.rs index fa5eb7de..e7635ebb 100644 --- a/src/dispatching/dispatcher.rs +++ b/src/dispatching/dispatcher.rs @@ -9,8 +9,8 @@ use std::{fmt::Debug, sync::Arc}; use teloxide_core::{ requests::Requester, types::{ - CallbackQuery, ChosenInlineResult, InlineQuery, Message, Poll, PollAnswer, - PreCheckoutQuery, ShippingQuery, UpdateKind, + CallbackQuery, ChatMemberUpdated, ChosenInlineResult, InlineQuery, Message, Poll, + PollAnswer, PreCheckoutQuery, ShippingQuery, UpdateKind, }, }; use tokio::sync::mpsc; @@ -61,6 +61,8 @@ pub struct Dispatcher { pre_checkout_queries_queue: Tx, polls_queue: Tx, poll_answers_queue: Tx, + my_chat_members_queue: Tx, + chat_members_queue: Tx, } impl Dispatcher @@ -83,6 +85,8 @@ where pre_checkout_queries_queue: None, polls_queue: None, poll_answers_queue: None, + my_chat_members_queue: None, + chat_members_queue: None, } } @@ -201,6 +205,24 @@ where self } + #[must_use] + pub fn my_chat_members_handler(mut self, h: H) -> Self + where + H: DispatcherHandler + 'static + Send, + { + self.my_chat_members_queue = self.new_tx(h); + self + } + + #[must_use] + pub fn chat_members_handler(mut self, h: H) -> Self + where + H: DispatcherHandler + 'static + Send, + { + self.chat_members_queue = self.new_tx(h); + self + } + /// Starts your bot with the default parameters. /// /// The default parameters are a long polling update listener and log all @@ -330,6 +352,22 @@ where 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 + ); + } } } }) diff --git a/src/dispatching/dispatcher_handler_rx_ext.rs b/src/dispatching/dispatcher_handler_rx_ext.rs index 7a0dddf6..d9616645 100644 --- a/src/dispatching/dispatcher_handler_rx_ext.rs +++ b/src/dispatching/dispatcher_handler_rx_ext.rs @@ -34,7 +34,11 @@ where Self: Stream>, 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(self, bot_name: N) -> BoxStream<'static, (UpdateWithCx, C)> diff --git a/src/prelude.rs b/src/prelude.rs index dac0b14f..d246a85e 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -15,7 +15,10 @@ pub use crate::{ pub use teloxide_core::{ adaptors::AutoSend, requests::{Request, ResponseResult}, - types::Message, + types::{ + CallbackQuery, ChatMemberUpdated, ChosenInlineResult, InlineQuery, Message, Poll, + PollAnswer, PreCheckoutQuery, ShippingQuery, + }, }; #[doc(inline)] From f0d4bc320bc07310ef750908b30d5df8b5e4ece9 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Thu, 18 Mar 2021 01:50:48 +0600 Subject: [PATCH 2/3] Fix examples/redis_remember_bot --- examples/redis_remember_bot/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/redis_remember_bot/src/main.rs b/examples/redis_remember_bot/src/main.rs index 8b8fed65..9fc32f22 100644 --- a/examples/redis_remember_bot/src/main.rs +++ b/examples/redis_remember_bot/src/main.rs @@ -52,7 +52,7 @@ async fn handle_message( cx: UpdateWithCx, Message>, dialogue: Dialogue, ) -> TransitionOut { - match cx.update.text_owned() { + match cx.update.text().map(ToOwned::to_owned) { None => { cx.answer("Send me a text message.").await?; next(dialogue) From 9a901481145660c2013f7b94e496b72bbf0bc9d9 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Thu, 18 Mar 2021 01:56:41 +0600 Subject: [PATCH 3/3] Fix examples/dialogue_bot --- examples/dialogue_bot/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/dialogue_bot/src/main.rs b/examples/dialogue_bot/src/main.rs index 310a69a3..cc2e6e76 100644 --- a/examples/dialogue_bot/src/main.rs +++ b/examples/dialogue_bot/src/main.rs @@ -46,7 +46,7 @@ async fn handle_message( cx: UpdateWithCx, Message>, dialogue: Dialogue, ) -> TransitionOut { - match cx.update.text_owned() { + match cx.update.text().map(ToOwned::to_owned) { None => { cx.answer("Send me a text message.").await?; next(dialogue)