Skip to content

Instantly share code, notes, and snippets.

@ilyaigpetrov
Last active November 13, 2025 23:30
Show Gist options
  • Select an option

  • Save ilyaigpetrov/7b83fcdbc39ef1edcc5269a3d740df0b to your computer and use it in GitHub Desktop.

Select an option

Save ilyaigpetrov/7b83fcdbc39ef1edcc5269a3d740df0b to your computer and use it in GitHub Desktop.
My Telegram Scripts

My Scripts for Telegram

Split Telegram's Exported rusult.json to Posts in JSON

#/bin/sh -xe
cat decrubru_channel-2025-11-04/result.json | jq -c '.messages[] | select(.type=="message") | [.id, (.text | if type == "array" then . else [.] end | tojson)] | @sh' --raw-output0 | xargs -0r -I {} sh -c 'set -- {}; filename=post-id_$(printf %04d $1).json; shift; printf %s "$@" > $filename';

Convert Formatting from JSON to Kind of Markdown

# Specification: https://github.com/telegramdesktop/tdesktop/blob/6fea6393c62106dc60c589304e42fb39b59f03a0/Telegram/SourceFiles/export/output/export_output_json.cpp

def trim: gsub("(^\\s*|\\s*$)"; "");
def getText: .text | trim;
def wrap(str): "\(str)\(getText)\(str)";
def wrapInTag(n): "<\(n)>\(getText)</\(n)>";
def is(t): .type == t;

map(
  if type == "string" then .
  elif is("bold") then wrap("__")
  elif is("italic") then wrap("*")
  elif is("underline") then wrapInTag("u")
  elif is("strikethrough") then wrap("~~")
  elif is("code") then wrap("```")
  elif is("pre") then wrap("`")
  elif is("blockquote") then "> \(getText)"
  elif is("spoiler") then wrapInTag("details")
  elif is("mention") then getText
  elif is("link") then getText
  elif is("text_link") then getText
  elif is("emoji") then getText
  elif is("hashtag") then getText
  else
    "UNKNOWN TYPE"
  end | tostring
) | join("")
cat SOME_POST.json | jq -f FILE_WITH_THE_SCRIPT_ABOVE.jq

Split tegracli's .jsonl Dump to Text Posts

I've dumped my telegram channel to jsonl with tegracli python package and converted all posts to markdown with the script below. Unfortunately tegracli doesn't preserve formatted text (quotes, code, spoilers, etc.) so I just resorted to other tools. Still share the script though.

https://pypi.org/project/tegracli/

# Dump jsonl from tegracli to `dump.jsonl`.
# Then convert it to markdown:
cat ../dump.jsonl | jq -c 'select(.["_"]=="Message" and has("message")) | [.id, .date, .message] | @sh' --raw-output0 | sort -zu | xargs -0r -I {} sh -c 'set -- {}; filename=post_$1.txt; shift 2; echo "$@" > $filename;'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment