Skip to content

使用自定义 StoreLayout ​

本指南详细介绍了如何通过使用自定义 StoreLayout 将 Telegram 频道的基本信息和详细信息存储到数据库中。通过此方法,可以减少对 Telegram 服务端的频繁调用,提高数据访问的效率和响应速度。

ChannelInfo 数据 ​

ChannelInfo 主要包含两个核心属性:

  • min:类型为 ImmutableChannel,包含频道的基本属性。
  • full:类型为 telegram4j.tl.ImmutableChannelFull,包含频道的详细信息。

min 字段解释 ​

json
{
  "flags": 24672,
  "flags2": 0,
  "id": 2226225384,
  "title": "长春",
  "username": "woyao51222",
  "photo": {
    "flags": 0,
    "photo_id": 6159076475509258768,
    "stripped_thumb": null,
    "dc_id": 5
  },
  "date": 1727367550,
  "usernames": null,
  "access_hash": -362916920667256444,
  "restriction_reason": null,
  "admin_rights": {
    "flags": 4671
  },
  "banned_rights": null,
  "default_banned_rights": null,
  "participants_count": null
}

min 字段详细解释 ​

  • min: ImmutableChannel 的实例,表示频道的基本属性。
    • accessHash: 唯一标识符,用于访问该频道。
    • adminRights: ImmutableChatAdminRights 对象,表示管理员的权限信息。
      • flags: 权限标识,二进制值表示具体权限(例如,1100000111111 表示特定权限启用)。
    • bannedRights: 被禁言的权限信息(当前为 null)。
    • date: 频道创建或最后修改的 Unix 时间戳。
    • flags: 表示频道的一些状态标识。
    • flags2: 另一个标识字段。
    • id: 频道的唯一标识符。
    • participantsCount: 参与人数(当前为 0)。
    • photo: ImmutableBaseChatPhoto 对象,包含频道的头像信息。
      • dcId: 数据中心 ID,存储该头像的服务器位置。
      • photoId: 头像的唯一标识符。
      • strippedThumb: 缩略图信息(当前为 null)。
    • restrictionReason: 限制原因(当前为 null)。
    • title: 频道名称。
    • username: 频道的用户名(如果有)。
    • defaultBannedRights: 默认禁言权限(当前为 null)。

full 字段解释 ​

json
{
  "flags": 1082130479,
  "flags2": 0,
  "id": 2226225384,
  "about": "",
  "stickerset": null,
  "location": null,
  "pts": 38131,
  "call": null,
  "participants_count": 565,
  "admins_count": 7,
  "kicked_count": 0,
  "banned_count": 0,
  "online_count": null,
  "read_inbox_max_id": 4067,
  "read_outbox_max_id": 0,
  "unread_count": 174,
  "chat_photo": {
    "flags": 0,
    "id": 6159076475509258768,
    "date": 1722745227,
    "sizes": [
      {
        "type": "a",
        "w": 160,
        "h": 160,
        "size": 10118
      },
      {
        "type": "b",
        "w": 320,
        "h": 320,
        "size": 27698
      },
      {
        "type": "c",
        "w": 640,
        "h": 640,
        "size": 80035
      }
    ],
    "access_hash": 2501199300329655937,
    "file_reference": {
      "direct": false,
      "readOnly": true,
      "readable": true,
      "writable": false,
      "contiguous": true
    },
    "video_sizes": null,
    "dc_id": 5
  },
  "notify_settings": {
    "flags": 63,
    "silent": false,
    "show_previews": true,
    "mute_until": 0,
    "ios_sound": {},
    "android_sound": {
      "title": "default",
      "data": "default"
    },
    "other_sound": {
      "title": "default",
      "data": "default"
    },
    "stories_muted": null,
    "stories_hide_sender": null,
    "stories_ios_sound": null,
    "stories_android_sound": null,
    "stories_other_sound": null
  },
  "exported_invite": {
    "flags": 32,
    "link": "https://t.me/+9r8hFunwEo0zOWI9",
    "date": 1727367551,
    "usage": null,
    "requested": null,
    "title": null,
    "admin_id": 6523165888,
    "start_date": null,
    "expire_date": null,
    "usage_limit": null
  },
  "bot_info": [
    {
      "flags": 3,
      "description": "hit",
      "commands": null,
      "user_id": 6523165888,
      "description_photo": null,
      "description_document": null,
      "menu_button": null
    },
    {
      "flags": 3,
      "description": "hi",
      "commands": null,
      "user_id": 6847577069,
      "description_photo": null,
      "description_document": null,
      "menu_button": null
    },
    {
      "flags": 3,
      "description": "hide",
      "commands": null,
      "user_id": 6108202915,
      "description_photo": null,
      "description_document": null,
      "menu_button": null
    },
    {
      "flags": 1,
      "description": null,
      "commands": null,
      "user_id": 7072019031,
      "description_photo": null,
      "description_document": null,
      "menu_button": null
    }
  ],
  "migrated_from_chat_id": null,
  "migrated_from_max_id": null,
  "pinned_msg_id": 5511,
  "available_min_id": null,
  "folder_id": null,
  "linked_chat_id": null,
  "slowmode_seconds": null,
  "slowmode_next_send_date": null,
  "stats_dc": null,
  "ttl_period": null,
  "pending_suggestions": null,
  "groupcall_default_join_as": null,
  "theme_emoticon": null,
  "requests_pending": null,
  "recent_requesters": null,
  "default_send_as": null,
  "available_reactions": {
    "flags": 0
  }
}

info.full 字段详细解释 ​

info.full 是 ImmutableChannelFull 类型的实例,包含频道的详细信息。以下是各个字段的详细说明:

  • flags: 标志字段,使用二进制值表示频道的各种状态和属性。具体含义需参考 Telegram 的 API 文档或库的实现。
  • flags2: 另一个标志字段,类似于 flags,用于表示额外的状态或属性。
  • id: 频道的唯一标识符。
  • about: 频道的描述信息,介绍频道的内容或目的。
  • participantsCount: 频道的参与者数量,即成员数量。
  • adminsCount: 管理员的数量,表示负责管理频道的管理员数量。
  • kickedCount: 被踢出的成员数量。
  • bannedCount: 被禁言的成员数量。
  • onlineCount: 当前在线的成员数量(如果可用)。
  • readInboxMaxId: 收件箱中已读消息的最大 ID。
  • readOutboxMaxId: 发件箱中已读消息的最大 ID。
  • unreadCount: 未读消息的数量。
  • chatPhoto: 频道的头像信息,类型为 BasePhoto。
    • flags: 标志字段,表示头像的状态或属性。
    • id: 头像的唯一标识符。
    • accessHash: 访问头像的哈希值,用于安全验证。
    • fileReference: 文件引用,用于访问存储在 Telegram 服务器上的头像文件。
    • date: 头像上传或最后修改的 Unix 时间戳。
    • sizes: 头像的不同尺寸信息,数组类型,每个元素为 BasePhotoSize。
      • type: 尺寸类型标识(例如 'a', 'b', 'c')。
      • w: 宽度(像素)。
      • h: 高度(像素)。
      • size: 文件大小(字节)。
    • videoSizes: 视频尺寸信息(如果有)。
    • dcId: 数据中心 ID,表示头像存储的服务器位置。
  • notifySettings: 通知设置,类型为 PeerNotifySettings。
    • flags: 标志字段,表示通知设置的状态。
    • showPreviews: 是否显示消息预览。
    • silent: 是否静音通知。
    • muteUntil: 静音通知的截止时间(Unix 时间戳)。
    • iosSound: iOS 设备的通知声音设置,类型为 NotificationSoundDefault 或 NotificationSoundLocal。
      • title: 声音标题(如果为 NotificationSoundLocal)。
      • data: 声音数据(如果为 NotificationSoundLocal)。
    • androidSound: Android 设备的通知声音设置,类似于 iosSound。
    • otherSound: 其他设备的通知声音设置,类似于 iosSound。
    • storiesMuted: 是否静音故事通知(如果有)。
    • storiesHideSender: 是否隐藏故事发送者信息(如果有)。
    • storiesIosSound: 故事的 iOS 通知声音设置(如果有)。
    • storiesAndroidSound: 故事的 Android 通知声音设置(如果有)。
    • storiesOtherSound: 故事的其他设备通知声音设置(如果有)。
  • exportedInvite: 导出的邀请链接,类型为 ChatInviteExported。
    • flags: 标志字段,表示邀请链接的状态。
    • link: 邀请链接的 URL。
    • adminId: 创建邀请链接的管理员的用户 ID。
    • date: 邀请链接创建的 Unix 时间戳。
    • startDate: 邀请链接的开始日期(如果有)。
    • expireDate: 邀请链接的过期日期(如果有)。
    • usageLimit: 邀请链接的使用限制(如果有)。
    • usage: 当前邀请链接的使用次数(如果有)。
    • requested: 是否需要请求才能加入(如果有)。
    • title: 邀请链接的标题(如果有)。
  • botInfo: 频道中机器人的信息,数组类型,每个元素为 BotInfo。
    • flags: 标志字段,表示机器人的状态或属性。
    • userId: 机器人的用户 ID。
    • description: 机器人的描述信息。
    • descriptionPhoto: 机器人的描述照片(如果有)。
    • descriptionDocument: 机器人的描述文档(如果有)。
    • commands: 机器人的命令列表(如果有)。
    • menuButton: 机器人的菜单按钮设置(如果有)。
  • migratedFromChatId: 如果频道从其他聊天迁移而来,则为原聊天的 ID(当前为 null)。
  • migratedFromMaxId: 如果频道从其他聊天迁移而来,则为原聊天的最大消息 ID(当前为 null)。
  • pinnedMsgId: 频道中被置顶的消息的 ID(如果有)。
  • stickerset: 频道使用的贴纸包信息(如果有)。
  • availableMinId: 可用的最小消息 ID(如果有)。
  • folderId: 频道所属文件夹的 ID(如果有)。
  • linkedChatId: 关联的聊天 ID(如果有)。
  • location: 频道的位置或地理信息(如果有)。
  • slowmodeSeconds: 频道的慢速模式设置,以秒为单位(如果启用)。
  • slowmodeNextSendDate: 慢速模式下,下一次可以发送消息的时间(Unix 时间戳,当前为 null)。
  • statsDc: 统计数据的数据中心 ID(如果有)。
  • pts: 频道的状态更新计数器,用于同步。
  • call: 频道的通话信息(如果有)。
  • ttlPeriod: 频道的消息生存时间设置(如果有)。
  • pendingSuggestions: 待处理的建议列表(如果有)。
  • groupcallDefaultJoinAs: 默认的群组通话加入方式(如果有)。
  • themeEmoticon: 频道的主题表情符号(当前为 null)。
  • requestsPending: 待处理的请求列表(如果有)。
  • recentRequesters: 最近的请求者列表(如果有)。
  • defaultSendAs: 默认的发送身份设置(如果有)。
  • availableReactions: 频道可用的反应表情,类型为 ChatReactionsAll。
    • flags: 标志字段,表示反应表情的状态。

保存为 json 格式 ​

创建数据库表 ​

为存储频道的基本信息、详细信息和参与者信息,需要创建相应的数据库表。以下是表结构的创建脚本:

telegram_channel_min 表 ​

存储频道的基本信息。

sql
DROP TABLE IF EXISTS telegram_channel_min;
CREATE TABLE telegram_channel_min (
  id BIGINT AUTO_INCREMENT PRIMARY KEY,
  json JSON NOT NULL,
  `creator` VARCHAR(64) DEFAULT '',
  `create_time` DATETIME(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updater` VARCHAR(64) DEFAULT '',
  `update_time` DATETIME(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `deleted` SMALLINT(6) NOT NULL DEFAULT 0,
  `tenant_id` BIGINT(20) NOT NULL DEFAULT 0
);

或者,如果数据库不支持 JSON 类型,可以使用 VARCHAR 类型存储 JSON 字符串:

sql
DROP TABLE IF EXISTS telegram_channel_min;
CREATE TABLE telegram_channel_min (
  id BIGINT AUTO_INCREMENT PRIMARY KEY,
  json VARCHAR(16000) NOT NULL,
  `creator` VARCHAR(64) DEFAULT '',
  `create_time` DATETIME(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updater` VARCHAR(64) DEFAULT '',
  `update_time` DATETIME(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `deleted` SMALLINT(6) NOT NULL DEFAULT 0,
  `tenant_id` BIGINT(20) NOT NULL DEFAULT 0
);

telegram_channel_full 表 ​

存储频道的详细信息。

sql
DROP TABLE IF EXISTS telegram_channel_full;
CREATE TABLE telegram_channel_full (
  id BIGINT AUTO_INCREMENT PRIMARY KEY,
  json JSON NOT NULL,
  `creator` VARCHAR(64) DEFAULT '',
  `create_time` DATETIME(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updater` VARCHAR(64) DEFAULT '',
  `update_time` DATETIME(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `deleted` SMALLINT(6) NOT NULL DEFAULT 0,
  `tenant_id` BIGINT(20) NOT NULL DEFAULT 0
);

或者使用 VARCHAR 类型:

sql
DROP TABLE IF EXISTS telegram_channel_full;
CREATE TABLE telegram_channel_full (
  id BIGINT AUTO_INCREMENT PRIMARY KEY,
  json VARCHAR(16000) NOT NULL,
  `creator` VARCHAR(64) DEFAULT '',
  `create_time` DATETIME(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updater` VARCHAR(64) DEFAULT '',
  `update_time` DATETIME(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `deleted` SMALLINT(6) NOT NULL DEFAULT 0,
  `tenant_id` BIGINT(20) NOT NULL DEFAULT 0
);

telegram_channel_participants 表 ​

存储频道的参与者信息。

sql
DROP TABLE IF EXISTS telegram_channel_participants;
CREATE TABLE telegram_channel_participants (
  id BIGINT AUTO_INCREMENT PRIMARY KEY,
  json VARCHAR(16000) NOT NULL,
  `creator` VARCHAR(64) DEFAULT '',
  `create_time` DATETIME(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updater` VARCHAR(64) DEFAULT '',
  `update_time` DATETIME(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `deleted` SMALLINT(6) NOT NULL DEFAULT 0,
  `tenant_id` BIGINT(20) NOT NULL DEFAULT 0
);

实现自定义 DbChannelStoreServiceImpl ​

以下是自定义 DbChannelStoreServiceImpl 的实现,该类负责将频道的基本信息、详细信息和参与者信息存储到数据库中。

java
package nexus.io.telegram.bots.service;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BiFunction;

import com.fasterxml.jackson.core.type.TypeReference;
import nexus.io.db.activerecord.Db;
import nexus.io.db.activerecord.Row;
import nexus.io.telegram.store.ChannelInfo;
import nexus.io.telegram.store.TlObjectMapperUtils;
import nexus.io.telegram.store.service.DbChannelStoreService;

import lombok.extern.slf4j.Slf4j;
import telegram4j.tl.ChannelParticipant;
import telegram4j.tl.ImmutableChannel;
import telegram4j.tl.ImmutableChannelFull;
import telegram4j.tl.Peer;

@Slf4j
public class DbChannelStoreServiceImpl implements DbChannelStoreService {
  public static final String TELEGRAM_CHANNEL_MIN = "telegram_channel_min";
  public static final String TELEGRAM_CHANNEL_FULL = "telegram_channel_full";
  public static final String TELEGRAM_CHANNEL_PARTICIPANTS = "telegram_channel_participants";

  private TypeReference<ConcurrentMap<Peer, ChannelParticipant>> typeRef = new TypeReference<ConcurrentMap<Peer, ChannelParticipant>>() {
  };

  private final ConcurrentMap<Long, ReentrantLock> lockMap = new ConcurrentHashMap<>();

  /**
   * 从数据库中获取 ChannelInfo
   *
   * @param rawPeerId 频道的原始 Peer ID
   * @return ChannelInfo 对象,如果不存在则返回 null
   */
  @Override
  public ChannelInfo get(Long rawPeerId) {
    ChannelInfo info = null;

    // 获取基本信息
    Row row = Db.findById(TELEGRAM_CHANNEL_MIN, rawPeerId);
    if (row != null) {
      String str = row.getStr("json");
      try {
        ImmutableChannel min = TlObjectMapperUtils.readValue(str, ImmutableChannel.class);
        info = new ChannelInfo(min);
      } catch (Exception e) {
        log.error("Failed to readValue: rawPeerId={}, json={}, error={}", rawPeerId, str, e.getMessage(), e);
        return null;
      }
    } else {
      return null;
    }

    // 获取详细信息
    row = Db.findById(TELEGRAM_CHANNEL_FULL, rawPeerId);
    if (row != null) {
      String str = row.getStr("json");
      try {
        ImmutableChannelFull full = TlObjectMapperUtils.readValue(str, ImmutableChannelFull.class);
        info.setFull(full);
      } catch (Exception e) {
        log.error("Failed to readValue: rawPeerId={}, json={}, error={}", rawPeerId, str, e.getMessage(), e);
        return null;
      }
    } else {
      return null;
    }

    // 获取参与者信息
    row = Db.findById(TELEGRAM_CHANNEL_PARTICIPANTS, rawPeerId);
    if (row != null) {
      String str = row.getStr("json");
      try {
        ConcurrentMap<Peer, ChannelParticipant> participants = TlObjectMapperUtils.readValue(str, typeRef);
        info.setParticipants(participants);
      } catch (Exception e) {
        log.error("Failed to readValue: rawPeerId={}, json={}, error={}", rawPeerId, str, e.getMessage(), e);
      }
    }

    return info;
  }

  /**
   * 如果频道存在,则计算新的 ChannelInfo 并更新数据库
   *
   * @param channelId         频道 ID
   * @param remappingFunction 计算函数
   * @return 更新后的 ChannelInfo
   */
  @Override
  public ChannelInfo computeIfPresent(Long channelId, BiFunction<? super Long, ? super ChannelInfo, ? extends ChannelInfo> remappingFunction) {
    ChannelInfo existing = get(channelId);
    if (existing != null) {
      ChannelInfo updated = remappingFunction.apply(channelId, existing);
      if (updated != null) {
        updateChannelInfo(channelId, updated);
        return updated;
      } else {
        deleteChannelInfo(channelId);
      }
    }
    return null;
  }

  /**
   * 计算并更新或插入 ChannelInfo
   *
   * @param id                 频道 ID
   * @param remappingFunction  计算函数
   */
  @Override
  public void compute(Long id, BiFunction<? super Long, ? super ChannelInfo, ? extends ChannelInfo> remappingFunction) {
    ChannelInfo existing = get(id);
    ChannelInfo updated = remappingFunction.apply(id, existing);

    if (updated != null) {
      if (existing != null) {
        updateChannelInfo(id, updated);
      } else {
        insertChannelInfo(id, updated);
      }
    } else {
      if (existing != null) {
        deleteChannelInfo(id);
      }
    }
  }

  /**
   * 插入或更新 ChannelInfo
   *
   * @param channelId 频道 ID
   * @param info      ChannelInfo 对象
   */
  private void insertChannelInfo(Long channelId, ChannelInfo info) {
    ReentrantLock lock = lockMap.computeIfAbsent(channelId, id -> new ReentrantLock());

    // 处理基本信息
    if (info.getMin() != null) {
      if (info.getMin() instanceof ImmutableChannel immutableChannel) {
        String json = TlObjectMapperUtils.writeValueAsString(immutableChannel);
        lock.lock();
        try {
          Row row = Row.by("id", channelId).set("json", json);
          if (Db.exists(TELEGRAM_CHANNEL_MIN, "id", channelId)) {
            Db.update(TELEGRAM_CHANNEL_MIN, row);
          } else {
            Db.save(TELEGRAM_CHANNEL_MIN, row);
          }
        } finally {
          lock.unlock();
        }
      } else {
        log.error("Unsupported class type for min: {}", info.getMin().getClass());
      }
    }

    // 处理详细信息
    if (info.getFull() != null) {
      String json = TlObjectMapperUtils.writeValueAsString(info.getFull());
      Row fullRow = Row.by("id", channelId).set("json", json);
      lock.lock();
      try {
        if (Db.exists(TELEGRAM_CHANNEL_FULL, "id", channelId)) {
          Db.update(TELEGRAM_CHANNEL_FULL, fullRow);
        } else {
          Db.save(TELEGRAM_CHANNEL_FULL, fullRow);
        }
      } finally {
        lock.unlock();
      }
    }

    // 处理参与者信息
    if (info.getParticipants() != null) {
      String json = TlObjectMapperUtils.writeValueAsString(info.getParticipants());
      Row row = Row.by("id", channelId).set("json", json);
      lock.lock();
      try {
        if (Db.exists(TELEGRAM_CHANNEL_PARTICIPANTS, "id", channelId)) {
          Db.update(TELEGRAM_CHANNEL_PARTICIPANTS, row);
        } else {
          Db.save(TELEGRAM_CHANNEL_PARTICIPANTS, row);
        }
      } finally {
        lock.unlock();
      }
    }
  }

  /**
   * 更新现有的 ChannelInfo
   *
   * @param channelId 频道 ID
   * @param info      更新后的 ChannelInfo 对象
   */
  private void updateChannelInfo(Long channelId, ChannelInfo info) {
    insertChannelInfo(channelId, info);
  }

  /**
   * 删除 ChannelInfo
   *
   * @param channelId 频道 ID
   */
  private void deleteChannelInfo(Long channelId) {
    String deleteMinSql = "DELETE FROM " + TELEGRAM_CHANNEL_MIN + " WHERE id = ?";
    String deleteFullSql = "DELETE FROM " + TELEGRAM_CHANNEL_FULL + " WHERE id = ?";
    String deleteParticipantsSql = "DELETE FROM " + TELEGRAM_CHANNEL_PARTICIPANTS + " WHERE id = ?";
    Db.delete(deleteMinSql, channelId);
    Db.delete(deleteFullSql, channelId);
    Db.delete(deleteParticipantsSql, channelId);
  }
}

关键功能说明 ​

  • 获取频道信息 (get 方法): 从 telegram_channel_min、telegram_channel_full 和 telegram_channel_participants 表中分别获取基本信息、详细信息和参与者信息,并组装成 ChannelInfo 对象。

  • 插入或更新频道信息 (insertChannelInfo 方法): 将 ChannelInfo 对象中的各个部分序列化为 JSON 字符串,并分别存储到对应的数据库表中。如果记录已存在,则更新;否则,插入新记录。

  • 更新频道信息 (updateChannelInfo 方法): 实际上调用了 insertChannelInfo 方法,因为插入和更新操作在逻辑上是相似的。

  • 删除频道信息 (deleteChannelInfo 方法): 从所有相关的数据库表中删除指定频道的记录。

  • 计算频道信息 (compute 和 computeIfPresent 方法): 提供了基于现有频道信息进行计算和更新的接口,确保数据的一致性和完整性。

使用 DbStoreLayoutImpl ​

完成自定义的 DbChannelStoreServiceImpl 实现后,需要将其集成到 StoreLayout 中,以便在应用启动时进行配置。

java
// 初始化 DbChannelStoreServiceImpl
DbChannelStoreServiceImpl dbChannelStoreService = new DbChannelStoreServiceImpl();

// 创建 DbStoreLayoutImpl,并传入必要的函数
DbStoreLayoutImpl storeLayoutImpl = new DbStoreLayoutImpl(Function.identity());

// 设置自定义的 DbChannelStoreService
storeLayoutImpl.setDbChannels(dbChannelStoreService);

// 创建 FileStoreLayout,并指定存储路径(例如:t4j-bot_{botId}.bin)
FileStoreLayout storeLayout = new FileStoreLayout(storeLayoutImpl, Path.of("t4j-bot_" + botId + ".bin"));

// 配置引导程序使用自定义的 StoreLayout
bootstrap.setStoreLayout(storeLayout);

配置说明 ​

  1. 初始化 DbChannelStoreServiceImpl: 创建自定义的数据库存储服务实例。
  2. 创建 DbStoreLayoutImpl: 使用 Function.identity() 作为参数创建 DbStoreLayoutImpl 实例。
  3. 设置数据库频道存储服务: 将自定义的 DbChannelStoreServiceImpl 实例设置到 DbStoreLayoutImpl 中。
  4. 创建 FileStoreLayout: 指定存储路径,用于存储其他相关数据(例如:t4j-bot_{botId}.bin)。
  5. 配置引导程序: 将自定义的 StoreLayout 配置到应用的引导程序中。

配置完成后,频道信息将自动回存到数据库中,显著减少对 Telegram 服务端的调用次数,提高系统性能和稳定性。

将单独的字段保存到数据库 ​

创建数据表 ​

sql
DROP TABLE IF EXISTS telegram_channel_min;
CREATE TABLE telegram_channel_min (
  id BIGINT PRIMARY KEY,
  identifier BIGINT NOT NULL,
  flags INT NOT NULL,
  flags2 INT NOT NULL,
  channel_id BIGINT NOT NULL,
  title VARCHAR(255) NOT NULL,
  username VARCHAR(255) DEFAULT NULL,
  date bigint NOT NULL,
  access_hash BIGINT NOT NULL,
	photo varchar(1600) DEFAULT NULL, -- 存储 photo 对象
  admin_rights varchar(1600) DEFAULT NULL, -- 存储 admin_rights 对象
  creator VARCHAR(64) DEFAULT '',
  create_time DATETIME(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updater VARCHAR(64) DEFAULT '',
  update_time DATETIME(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  deleted SMALLINT(6) NOT NULL DEFAULT 0,
  tenant_id BIGINT(20) NOT NULL DEFAULT 0
);

总结 ​

本文档介绍了如何通过自定义 StoreLayout 将 Telegram 频道的信息存储到数据库中。通过创建相应的数据库表、实现自定义的存储服务类 DbChannelStoreServiceImpl,并将其集成到 StoreLayout 中,可以有效地管理和存储频道的基本信息、详细信息和参与者信息。这种方法不仅提高了数据访问效率,还增强了系统的可扩展性和维护性。

通过以下步骤,您可以将 Telegram 频道信息存入数据库:

  1. 理解频道信息结构: 了解 ChannelInfo 中 min 和 full 字段的具体内容和用途。
  2. 创建数据库表: 根据需求创建存储频道基本信息、详细信息和参与者信息的数据库表。
  3. 实现自定义存储服务: 编写 DbChannelStoreServiceImpl 类,实现从数据库获取、插入、更新和删除频道信息的逻辑。
  4. 配置 StoreLayout: 将自定义的存储服务集成到 StoreLayout 中,确保频道信息能够正确存储和读取。
  5. 测试和验证: 配置完成后,验证频道信息是否能够正确存储到数据库,并确保系统性能的提升。

通过以上步骤,您可以有效地管理 Telegram 频道的信息存储,提升应用的整体性能和用户体验。