Skip to main content

Dialogue

Struct Dialogue 

Source
pub struct Dialogue<D, S>
where S: ?Sized,
{ /* private fields */ }
Expand description

A handle for controlling dialogue state.

Implementations§

Source§

impl<D, S> Dialogue<D, S>
where D: Send + 'static, S: Storage<D> + ?Sized,

Source

pub fn new(storage: Arc<S>, chat_id: ChatId) -> Self

Constructs a new dialogue with storage (where dialogues are stored) and chat_id of a current dialogue.

Source

pub fn chat_id(&self) -> ChatId

Returns a chat ID associated with this dialogue.

Examples found in repository?
examples/purchase.rs (line 137)
129async fn receive_product_selection(
130    bot: Bot,
131    dialogue: MyDialogue,
132    full_name: String, // Available from `State::ReceiveProductChoice`.
133    q: CallbackQuery,
134) -> HandlerResult {
135    if let Some(product) = &q.data {
136        bot.send_message(
137            dialogue.chat_id(),
138            format!("{full_name}, product '{product}' has been purchased successfully!"),
139        )
140        .await?;
141        dialogue.exit().await?;
142    }
143
144    Ok(())
145}
Source

pub async fn get(&self) -> Result<Option<D>, S::Error>

Retrieves the current state of the dialogue or None if there is no dialogue.

Source

pub async fn get_or_default(&self) -> Result<D, S::Error>
where D: Default,

Like Dialogue::get but returns a default value if there is no dialogue.

Source

pub async fn update<State>(&self, state: State) -> Result<(), S::Error>
where D: From<State>,

Updates the dialogue state.

The dialogue type D must implement From<State> to allow implicit conversion from State to D.

Examples found in repository?
examples/dialogue.rs (line 62)
60async fn start(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
61    bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
62    dialogue.update(State::ReceiveFullName).await?;
63    Ok(())
64}
65
66async fn receive_full_name(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
67    match msg.text() {
68        Some(text) => {
69            bot.send_message(msg.chat.id, "How old are you?").await?;
70            dialogue.update(State::ReceiveAge { full_name: text.into() }).await?;
71        }
72        None => {
73            bot.send_message(msg.chat.id, "Send me plain text.").await?;
74        }
75    }
76
77    Ok(())
78}
79
80async fn receive_age(
81    bot: Bot,
82    dialogue: MyDialogue,
83    full_name: String, // Available from `State::ReceiveAge`.
84    msg: Message,
85) -> HandlerResult {
86    match msg.text().map(|text| text.parse::<u8>()) {
87        Some(Ok(age)) => {
88            bot.send_message(msg.chat.id, "What's your location?").await?;
89            dialogue.update(State::ReceiveLocation { full_name, age }).await?;
90        }
91        _ => {
92            bot.send_message(msg.chat.id, "Send me a number.").await?;
93        }
94    }
95
96    Ok(())
97}
More examples
Hide additional examples
examples/purchase.rs (line 89)
87async fn start(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
88    bot.send_message(msg.chat.id, "Let's start! What's your full name?").await?;
89    dialogue.update(State::ReceiveFullName).await?;
90    Ok(())
91}
92
93async fn help(bot: Bot, msg: Message) -> HandlerResult {
94    bot.send_message(msg.chat.id, Command::descriptions().to_string()).await?;
95    Ok(())
96}
97
98async fn cancel(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
99    bot.send_message(msg.chat.id, "Cancelling the dialogue.").await?;
100    dialogue.exit().await?;
101    Ok(())
102}
103
104async fn invalid_state(bot: Bot, msg: Message) -> HandlerResult {
105    bot.send_message(msg.chat.id, "Unable to handle the message. Type /help to see the usage.")
106        .await?;
107    Ok(())
108}
109
110async fn receive_full_name(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
111    match msg.text().map(ToOwned::to_owned) {
112        Some(full_name) => {
113            let products = ["Apple", "Banana", "Orange", "Potato"]
114                .map(|product| InlineKeyboardButton::callback(product, product));
115
116            bot.send_message(msg.chat.id, "Select a product:")
117                .reply_markup(InlineKeyboardMarkup::new([products]))
118                .await?;
119            dialogue.update(State::ReceiveProductChoice { full_name }).await?;
120        }
121        None => {
122            bot.send_message(msg.chat.id, "Please, send me your full name.").await?;
123        }
124    }
125
126    Ok(())
127}
examples/db_remember.rs (line 67)
64async fn start(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
65    match msg.text().map(|text| text.parse::<i32>()) {
66        Some(Ok(n)) => {
67            dialogue.update(State::GotNumber(n)).await?;
68            bot.send_message(
69                msg.chat.id,
70                format!("Remembered number {n}. Now use /get or /reset."),
71            )
72            .await?;
73        }
74        _ => {
75            bot.send_message(msg.chat.id, "Please, send me a number.").await?;
76        }
77    }
78
79    Ok(())
80}
examples/deep_linking.rs (line 90)
65pub async fn start(
66    bot: Bot,
67    dialogue: MyDialogue,
68    msg: Message,
69    start: String, // Available from `case![StartCommand::Start(start)]`
70    me: Me,
71) -> HandlerResult {
72    if start.is_empty() {
73        // This means that it is just a regular link like https://t.me/some_bot, or a /start command
74        bot.send_message(
75            msg.chat.id,
76            format!(
77                "Hello!\n\nThis link allows anyone to message you secretly: {}?start={}",
78                me.tme_url(),
79                msg.chat.id
80            ),
81        )
82        .await?;
83        dialogue.exit().await?;
84    } else {
85        // And this means that the link is like this: https://t.me/some_bot?start=123456789,
86        // or a /start 123456789 command
87        match start.parse::<i64>() {
88            Ok(id) => {
89                bot.send_message(msg.chat.id, "Send your message:").await?;
90                dialogue.update(State::WriteToSomeone { id: ChatId(id) }).await?;
91            }
92            Err(_) => {
93                bot.send_message(msg.chat.id, "Bad link!").await?;
94                dialogue.exit().await?;
95            }
96        }
97    }
98    Ok(())
99}
Source

pub async fn reset(&self) -> Result<(), S::Error>
where D: Default,

Updates the dialogue with a default value.

Examples found in repository?
examples/db_remember.rs (line 94)
82async fn got_number(
83    bot: Bot,
84    dialogue: MyDialogue,
85    num: i32, // Available from `State::GotNumber`.
86    msg: Message,
87    cmd: Command,
88) -> HandlerResult {
89    match cmd {
90        Command::Get => {
91            bot.send_message(msg.chat.id, format!("Here is your number: {num}.")).await?;
92        }
93        Command::Reset => {
94            dialogue.reset().await?;
95            bot.send_message(msg.chat.id, "Number reset.").await?;
96        }
97    }
98    Ok(())
99}
Source

pub async fn exit(&self) -> Result<(), S::Error>

Removes the dialogue from the storage provided to Dialogue::new.

Examples found in repository?
examples/purchase.rs (line 100)
98async fn cancel(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
99    bot.send_message(msg.chat.id, "Cancelling the dialogue.").await?;
100    dialogue.exit().await?;
101    Ok(())
102}
103
104async fn invalid_state(bot: Bot, msg: Message) -> HandlerResult {
105    bot.send_message(msg.chat.id, "Unable to handle the message. Type /help to see the usage.")
106        .await?;
107    Ok(())
108}
109
110async fn receive_full_name(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
111    match msg.text().map(ToOwned::to_owned) {
112        Some(full_name) => {
113            let products = ["Apple", "Banana", "Orange", "Potato"]
114                .map(|product| InlineKeyboardButton::callback(product, product));
115
116            bot.send_message(msg.chat.id, "Select a product:")
117                .reply_markup(InlineKeyboardMarkup::new([products]))
118                .await?;
119            dialogue.update(State::ReceiveProductChoice { full_name }).await?;
120        }
121        None => {
122            bot.send_message(msg.chat.id, "Please, send me your full name.").await?;
123        }
124    }
125
126    Ok(())
127}
128
129async fn receive_product_selection(
130    bot: Bot,
131    dialogue: MyDialogue,
132    full_name: String, // Available from `State::ReceiveProductChoice`.
133    q: CallbackQuery,
134) -> HandlerResult {
135    if let Some(product) = &q.data {
136        bot.send_message(
137            dialogue.chat_id(),
138            format!("{full_name}, product '{product}' has been purchased successfully!"),
139        )
140        .await?;
141        dialogue.exit().await?;
142    }
143
144    Ok(())
145}
More examples
Hide additional examples
examples/dialogue.rs (line 109)
99async fn receive_location(
100    bot: Bot,
101    dialogue: MyDialogue,
102    (full_name, age): (String, u8), // Available from `State::ReceiveLocation`.
103    msg: Message,
104) -> HandlerResult {
105    match msg.text() {
106        Some(location) => {
107            let report = format!("Full name: {full_name}\nAge: {age}\nLocation: {location}");
108            bot.send_message(msg.chat.id, report).await?;
109            dialogue.exit().await?;
110        }
111        None => {
112            bot.send_message(msg.chat.id, "Send me plain text.").await?;
113        }
114    }
115
116    Ok(())
117}
examples/deep_linking.rs (line 83)
65pub async fn start(
66    bot: Bot,
67    dialogue: MyDialogue,
68    msg: Message,
69    start: String, // Available from `case![StartCommand::Start(start)]`
70    me: Me,
71) -> HandlerResult {
72    if start.is_empty() {
73        // This means that it is just a regular link like https://t.me/some_bot, or a /start command
74        bot.send_message(
75            msg.chat.id,
76            format!(
77                "Hello!\n\nThis link allows anyone to message you secretly: {}?start={}",
78                me.tme_url(),
79                msg.chat.id
80            ),
81        )
82        .await?;
83        dialogue.exit().await?;
84    } else {
85        // And this means that the link is like this: https://t.me/some_bot?start=123456789,
86        // or a /start 123456789 command
87        match start.parse::<i64>() {
88            Ok(id) => {
89                bot.send_message(msg.chat.id, "Send your message:").await?;
90                dialogue.update(State::WriteToSomeone { id: ChatId(id) }).await?;
91            }
92            Err(_) => {
93                bot.send_message(msg.chat.id, "Bad link!").await?;
94                dialogue.exit().await?;
95            }
96        }
97    }
98    Ok(())
99}
100
101pub async fn send_message(
102    bot: Bot,
103    id: ChatId, // Available from `State::WriteToSomeone`
104    msg: Message,
105    dialogue: MyDialogue,
106    me: Me,
107) -> HandlerResult {
108    match msg.text() {
109        Some(text) => {
110            // Trying to send a message to the user
111            let sent_result = bot
112                .send_message(id, format!("You have a new message!\n\n<i>{text}</i>"))
113                .parse_mode(ParseMode::Html)
114                .await;
115
116            // And if no error is returned, success!
117            if sent_result.is_ok() {
118                bot.send_message(
119                    msg.chat.id,
120                    format!(
121                        "Message sent!\n\nYour link is: {}?start={}",
122                        me.tme_url(),
123                        msg.chat.id
124                    ),
125                )
126                .await?;
127            } else {
128                bot.send_message(msg.chat.id, "Error sending message! Maybe user blocked the bot?")
129                    .await?;
130            }
131            dialogue.exit().await?;
132        }
133        None => {
134            bot.send_message(msg.chat.id, "This bot can send only text.").await?;
135        }
136    };
137    Ok(())
138}

Trait Implementations§

Source§

impl<D, S> Clone for Dialogue<D, S>
where S: ?Sized,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<D: Debug, S> Debug for Dialogue<D, S>
where S: ?Sized + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<D, S> Freeze for Dialogue<D, S>
where S: ?Sized,

§

impl<D, S> RefUnwindSafe for Dialogue<D, S>

§

impl<D, S> Send for Dialogue<D, S>
where S: Sync + Send + ?Sized, D: Send,

§

impl<D, S> Sync for Dialogue<D, S>
where S: Sync + Send + ?Sized, D: Sync,

§

impl<D, S> Unpin for Dialogue<D, S>
where D: Unpin, S: ?Sized,

§

impl<D, S> UnwindSafe for Dialogue<D, S>
where S: RefUnwindSafe + ?Sized, D: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Erasable for T

Source§

const ACK_1_1_0: bool = true

Available on non-enforce_1_1_0_semantics only.
Whether this implementor has acknowledged the 1.1.0 update to unerase’s documented implementation requirements. Read more
Source§

unsafe fn unerase(this: NonNull<Erased>) -> NonNull<T>

Unerase this erased pointer. Read more
Source§

fn erase(this: NonNull<Self>) -> NonNull<Erased>

Turn this erasable pointer into an erased pointer. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more