filter method

Future<CTX> filter(
  1. Filter<CTX> filter, {
  2. Duration? timeout,
})

Waits for an update that matches a specific filter with timeout.

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

Parameters:

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

Example:

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

Implementation

Future<CTX> filter(
  Filter<CTX> filter, {
  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 (!_matchesFilter(ctx, filter));

  return ctx;
}