waitFor method

Future<CTX> waitFor(
  1. MiddlewarePredicate<CTX> predicate, {
  2. Duration? timeout,
})

Waits for an update that matches a custom predicate with timeout.

This method keeps waiting for updates until one matches the filter or the timeout is exceeded.

Parameters:

  • predicate: The predicate that the update must match.
  • timeout: Custom timeout for this wait operation.

Example:

await ctx.reply('Send me a photo:');
final photoCtx = await conversation.waitFor(
  PhotoFilter(),
  Duration(minutes: 5),
);
await photoCtx.reply('Nice photo!');

Implementation

Future<CTX> waitFor(
  MiddlewarePredicate<CTX> predicate, {
  Duration? timeout,
}) async {
  final deadline = timeout != null ? DateTime.now().add(timeout) : null;

  CTX ctx;
  do {
    final remainingTime = deadline?.difference(DateTime.now());

    if (remainingTime != null && remainingTime.isNegative) {
      throw ConversationTimeoutException(_conversationName);
    }

    ctx = await wait(remainingTime);
  } while (!predicate(ctx));

  return ctx;
}