Skip to main content
Go to Latest
class ConversationHandle
import { ConversationHandle } from "https://deno.land/x/grammy_conversations@v1.0.1/conversation.ts";

Internally used class which acts as a conversation handle.

Constructors

new
ConversationHandle(
ctx: C,
opLog: OpLog,
rsr: Resolver<ResolveOps>,
)

Properties

private
active: boolean
private
replayIndex: ReplayIndex
readonly
_isReplaying

Internal flag, true if the conversation is currently replaying in order to jump back to an old state, and false otherwise. Do not use unless you know exactly what you are doing.

Utilities for building forms. Contains methods that let you wait for messages and automatically perform input validation.

Methods

Internal method, deactivates the conversation handle. Do not use unless you know exactly what you are doing.

_finalize(slot: AsyncOrder)

Internal method, finalizes a previously generated slot. Do not use unless you know exactly what you are doing.

_logApi(method: string): ApiOp

Internal method, logs an API call and returns the assigned slot. Do not use unless you know exactly what you are doing.

_logExt(): ExtOp

Internal method, logs an external operation and returns the assigned slot. Do not use unless you know exactly what you are doing.

_logWait(op: WaitOp)

Internal method, logs a wait call. Do not use unless you know exactly what you are doing.

_replayApi(method: string): Promise<NonNullable<ApiOp["r"]>>

Internal method, replays an API call operation and advances the replay cursor. Do not use unless you know exactly what you are doing.

_replayExt(): Promise<NonNullable<ExtOp["r"]>>

Internal method, replays an external operation and advances the replay cursor. Do not use unless you know exactly what you are doing.

Internal method, replays a wait operation and advances the replay cursor. Do not use unless you know exactly what you are doing.

_resolveAt<T>(index: number, value?: T): Promise<T>

Internal method, creates a promise from a given value that will resolve at the given index in order to accurately restore the order in which different operations complete. Do not use unless you know exactly what you are doing.

Internal method, unlogs the most recent call. Do not use unless you know exactly what you are doing.

external<F extends (...args: any[]) => any, I = any>(op: F | { task: F; args?: Parameters<F>; beforeStore?: (value: ReturnType<F>) => I | Promise<I>; afterLoad?: (value: I) => ReturnType<F> | Promise<ReturnType<F>>; beforeStoreError?: (value: unknown) => unknown | Promise<unknown>; afterLoadError?: (value: unknown) => unknown; }): Promise<Awaited<ReturnType<F>>>

Safely performs an operation with side-effects. You must use this to wrap all communication with external systems that does not go through grammY, such as database communication or calls to external APIs.

This function will then make sure the operation is only performed once, and not every time a message is handled by the conversation.

It will need to be able to store the result value of this operation in the session. Hence, it must store and load the result of the operation according to your storage adapter. It is therefore best to only return primitive values or POJOs. If you need to transform your data before it can be stored, you can specify the beforeStore function. If you need to transform your data after it was loaded, you can specify the afterLoad function.

log(...args: Parameters<console.log>)

Safely perform console.log calls, but only when they should really be logged (so not during replay operations).

Safely generates a random number from Math.random(). You should use this instead of Math.random() in your conversation because non-deterministic behavior is not allowed.

Skips handling the update that was received in the last wait call. Once called, the conversation resets to the last wait call, as if the update had never been received. The control flow is passed on immediately, so that middleware downstream of the conversation can continue handling the update.

Effectively, calling await conversation.skip() behaves as if this conversation had not received the update at all.

While the conversation rewinds its logs internally, it does not unsend messages that you send between the calls to wait and skip.

sleep(milliseconds: number): Promise<void>

Sleep for the specified number of milliseconds. You should use this instead of your own sleeping function so that you don't block the conversation while it is restoring a previous position.

wait(): Promise<C>

Waits for a new update (e.g. a message, callback query, etc) from the user. Once received, this method returns the new context object for the incoming update.

waitFor<Q extends FilterQuery>(query: Q | Q[], otherwise?: (ctx: C) => unknown | Promise<unknown>): Promise<Filter<C, Q>>

Waits for a new update (e.g. a message, callback query, etc) that matches the given filter query. As soon as an update arrives that matches the filter query, the corresponding context object is returned.

waitFrom(user: number | User, otherwise?: (ctx: C) => unknown | Promise<unknown>): Promise<C & { from: User; }>

Waits for a new update (e.g. a message, callback query, etc) from the given user. As soon as an update arrives from this user, the corresponding context object is returned.

waitUnless(predicate: (ctx: C) => boolean | Promise<boolean>, otherwise?: (ctx: C) => unknown | Promise<unknown>): Promise<C>

Waits for a new update (e.g. a message, callback query, etc) that does not fulfil a certain condition. This condition is specified via the given predicate function. As soon as an update arrives for which the predicate function returns false, this method will return it.

waitUntil<D extends C>(predicate: (ctx: C) => ctx is D, otherwise?: (ctx: C) => unknown | Promise<unknown>): Promise<D>

Waits for a new update (e.g. a message, callback query, etc) that fulfils a certain condition. This condition is specified via the given predicate function. As soon as an update arrives for which the predicate function returns true, this method will return it.

waitUntil(predicate: (ctx: C) => boolean | Promise<boolean>, otherwise?: (ctx: C) => unknown | Promise<unknown>): Promise<C>