getEntityText method

String? getEntityText(
  1. MessageEntityType type
)

Returns the text where the given MessageEntityType is found

Implementation

String? getEntityText(MessageEntityType type) {
  final ents = entities ?? captionEntities;
  final msgTxt = text ?? caption;

  // If entities property is null or is empty just return null
  if (ents == null || ents.isEmpty) return null;
  if (msgTxt == null) return null;

  if (ents.any((e) => e.type == type) != true) return null;

  final entity = ents.firstWhere(
    (element) => element.type == type,
  );

  String entxt = msgTxt.substring(
    entity.offset,
    entity.offset + entity.length,
  );

  switch (type) {
    case MessageEntityType.mention:
    case MessageEntityType.hashtag:
    case MessageEntityType.cashtag:
      entxt = entxt.substring(1);
      break;
    case MessageEntityType.botCommand:
      if (entxt.contains('@')) {
        entxt = entxt.substring(0, entxt.indexOf('@'));
      } else {
        entxt = entxt.substring(1);
      }
      break;
    case MessageEntityType.textMention:
    case MessageEntityType.url:
    case MessageEntityType.email:
    case MessageEntityType.phoneNumber:
    case MessageEntityType.bold:
    case MessageEntityType.italic:
    case MessageEntityType.underline:
    case MessageEntityType.strikethrough:
    case MessageEntityType.spoiler:
    case MessageEntityType.code:
    case MessageEntityType.pre:
    case MessageEntityType.textLink:
    case MessageEntityType.blockquote:
    case MessageEntityType.expandableBlockquote:
      break;
    case MessageEntityType.customEmoji:
      entxt = entity.customEmojiId!;
      break;
  }
  return entxt;
}