JQ script to import Links from "Saved Messages" to Obsidian

Since I used the “Saved Messages” feature from telegram as a (mostly) write-only “Stuff I should look at” inbox…and I wanted to make it more probable that I actually do that, I decided to create a task list in obsidian with all links. To do that I used the Export Telegram Data feature and a little jq script.

def namedArg(p): $ARGS.named | if  has(p) then $ARGS.named[p] else null end;
def hasNamedArg(p): $ARGS.named | has(p);
def _filterStartDate: if hasNamedArg("startdate") then (namedArg("startdate") | tostring | strptime("%Y-%m-%dT%H:%M:%SZ") | mktime) else (now - (7*24*60*60)) end;
def defaultLinkText(p): if ( (p | length) > 0) then p else "Link" end;

 . |
 
#Define some Variables
(null | _filterStartDate) as $start_date |
([10]|implode) as $newline |

#Start of Main Filter Chain
.chats.list |
.[] |
select(.type == "saved_messages") |
.messages? |
# Start transformed Messages
[

  .[] | 
  select((.date_unixtime | tonumber) > $start_date)  |
  select(.text_entities | .[] | (.type == "link")) |
  
# Build the Temporary JSON that contains the relevant info
  {
#   Date of entry, as original
    date: .date,
#   Date value to perform grouping and filtering
    date_iso_date: .date_unixtime | tonumber | strftime("%Y-%m-%d"),
    date_unixtime: .date_unixtime | tonumber,
#   Retrieve the first "non shortened" link
    link:  [(.text_entities | .[] | select(.type == "link") | select(.text | test("https?://t.co/.*") | not) | .text )] | first,
#   Retrieve the plain text values, make them fit into one line and fix up some stuff
    plain_text: [ .text_entities | .[] | select(.type == "plain") | .text ] | map(select(length > 0)) | join(" ") | split("\n") | map(select(length > 0)) | join(" ")
  } |

# Add a url_host element to each json
  .url_host = (.link | match("https?://([^:/]+)/.*") | .captures | first | .string | ascii_downcase) |

# Remove all twitter links
  select(.url_host == "twitter.com" | not)

# End transformed Messages
] |
    
# Sort by time
sort_by(.date_unixtime) |
  
# group by date_iso_date, and build nice object
group_by(.date_iso_date) | reverse | 
[
 .[] |
  { date_iso_date: [.[] | .date_iso_date] | first,
    links: .
  }
] |

[
  .[] |
      [
        ("### \(.date_iso_date)"),
        "",
        (.links | .[] | "- [ ] (URL::[\(defaultLinkText(.plain_text))](\(.link))) [created::\(.date_iso_date)] #telegram"),
        ""
    ]
] |

flatten(1) |

join($newline) |

@text

Assuming this script is saved as transform.jq and the telegram export is result.json, the following command will print out a markdown formated list of links to be included in whichever note you like.

jq -r -M  --arg startdate '2000-01-01T10:12:13Z' -f transform.jq result.json

The -r -M options make sure jq doesn’t mess with the output since it is not json, and the startdate allows to restrict the export to any links newer than this date. (If you leave it out, it will default to anything in the last week)

I hope someone else finds this usefull :slight_smile:

Have a nice day

  • Jan
1 Like