Skip to content

ruinmi/telegram-bot

Repository files navigation

Telegram Chat Archiver and Web Viewer

Overview

This project exports Telegram chats using the tdl command line tool and presents them through a small Flask web interface. Messages are saved in SQLite databases, attachments can be downloaded automatically and the viewer fetches Open Graph data for link previews.

Features

  • Export history via update_messages.py and tdl chat export.
  • Optional attachment download with tdl dl.
  • Parse and store messages in per chat SQLite databases.
  • Basic authenticated web server to manage chats and browse them.
  • Background workers periodically update chats based on saved settings.
  • Searchable HTML viewer with inline media display and link previews.

Important Files

  • update_messages.py – run tdl to export messages and files.
  • main.py – coordinates export, parses JSON and saves messages to the database.
  • server.py – Flask server providing the management interface and chat pages.
  • db_utils.py – SQLite helper utilities.
  • message_utils.py – message parsing helpers.
  • og_utils.py – fetch Open Graph metadata for links.
  • Frontend assets: chat.js, chat.css, index.html, template.html.
  • Example startup scripts: run.sh / run.bat and telegrambot.service.

Quick Start

  1. Install Python 3 and the tdl tool.
  2. Install dependencies:
    pip install -r requirements.txt
  3. Log in with tdl:
    tdl login -T qr
  4. Start the server:
    BOT_PASSWORD=yourpassword python server.py
    The server listens on 127.0.0.1:8000 and uses basic authentication. Visit http://localhost:8000/ to add a chat and start its worker. Open /chat/<chat_id> to browse messages.

Notes

  • Ensure you have permission to archive the desired Telegram chats.

Telegram 聊天记录归档与网页查看器

简介

本项目借助 tdl 工具导出 Telegram 聊天记录,将消息保存到 SQLite 数据库,并提供可搜索的网页界面。可以自动下载附件并抓取链接的 Open Graph 预览信息。

功能

  • 通过 update_messages.py 调用 tdl chat export 导出聊天记录。
  • 可选地使用 tdl dl 下载聊天中的文件。
  • 解析 JSON 数据并写入各聊天对应的 SQLite 数据库。
  • Flask 服务器提供基本认证和管理界面,可启动后台线程定期更新聊天。
  • 浏览聊天时支持搜索、无限滚动、媒体展示以及链接预览。
  • 所有数据存放在 data/downloads/ 目录下。

重要文件

  • update_messages.py:封装 tdl 导出聊天和附件。
  • main.py:协调导出、解析 JSON 并写入数据库。
  • server.py:Flask 服务器,提供管理界面及聊天页面。
  • db_utils.py:SQLite 操作辅助函数。
  • message_utils.py:消息解析工具。
  • og_utils.py:抓取链接的 Open Graph 数据。
  • 前端资源:chat.jschat.cssindex.htmltemplate.html
  • 启动脚本示例:run.shrun.battelegrambot.service

快速开始

  1. 安装 Python 3 与 tdl 工具。
  2. 安装依赖:
    pip install -r requirements.txt
  3. 使用 tdl 登录:
    tdl login -T qr
  4. 启动服务器:
    BOT_PASSWORD=你的密码 python server.py
    服务器默认在 127.0.0.1:8000 监听,并启用基本认证。访问 http://localhost:8000/ 添加聊天后启动后台线程,再在 /chat/<chat_id> 查看聊天记录。

注意

  • 请确保有权限访问并归档相应的 Telegram 聊天。

About

Same UI as Telegram, but improved chat search.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published