getEntityText method

String? getEntityText(
  1. MessageEntityType type
)

Extracts text for entities of the specified type from the current message.

This method looks for entities in message text, caption, or channel post and returns the text content for entities matching the specified type.

For certain entity types, the text is processed:

  • Mentions, hashtags, cashtags: Leading symbol (@, #, $) is removed
  • Bot commands: Leading '/' and bot username (if present) are removed
  • Custom emojis: Returns the custom emoji ID instead of text

Returns null if:

  • No message/text is available
  • No entities are found
  • No entities of the specified type exist

Example:

// For message: "Hello @username, check #telegram"
final mention = ctx.getEntityText(MessageEntityType.mention); // "username"
final hashtag = ctx.getEntityText(MessageEntityType.hashtag); // "telegram"

Implementation

String? getEntityText(MessageEntityType type) {
  final entityData = _getEntityData();
  if (entityData == null) return null;

  final entity = entityData.entities.where((e) => e.type == type).firstOrNull;

  if (entity == null) return null;

  return _extractEntityText(entity, entityData.text, type);
}