I have a fairly serious problem. I lost all the numbers in the phone book, breaking the SIM card, but luckily I saw that most of them are stored on telegram. I tried with telegram desktop to export all contacts and the operation was successful.
Now, how can I put them in the phone book? I hope there is a solution, even starting a new procedure from scratch.

Solution here does not work Sync Telegram Contact with Phone Contact


3 Answers 3


@Memmo's answer is great and put me in the right direction. However, the script does not work anymore, because the indention was changing for Telegram exports. Also I wanted a version that works for all country origins. My output CSV can also be imported into Google Contacts without the necessary step to convert it to the VCard. Here is my modified version:

import re

pathToFile = 'lists/contacts.html'

with open(pathToFile, encoding='utf-8') as f:
    lines = tuple(l for l in f.readlines())
    with open('csvToConvert.csv', "w", encoding="utf-8") as outputFile:
        outputFile.write('Name,Group Membership,Phone 1 - Type,Phone 1 - Value\n')
        for i,l in enumerate(lines):
            if '<div class="name bold">' in l and '<div class="details_entry details">' in lines[i+4]:
                tel = re.sub('^00', '+', lines[i+5].replace(' ','').replace('\n',''))
                outputFile.write(lines[i+1].replace('\n','') + ',* myContacts,,' + tel + '\n')

The data export can be found in Telegram Desktop in Settings > Advanced. The script above needs to be located in the resulting Data Export folder and needs to be run by Python3.

My final step was to merge the contacts using Merge + on my mobile after sync, because the script will make one entry per number, not per contact.


After exporting all the contacts with Export Telegram data, I created a Python script that extracted all the contacts from the generated website.
The script, positioned directly inside the Telegram Desktop folder, is the following:

pathToFile = 'DataExport_08_12_2018 (2)/lists/contacts.html'

with open(pathToFile, encoding='utf-8') as f:
    lines = tuple(l for l in f.readlines())
    with open('csvToConvert.csv', "w", encoding="utf-8") as outputFile:
        outputFile.write('nome, telefono\n')
        for i,l in enumerate(lines):
            if l.startswith('    <div class="name bold">') and lines[i+4].startswith('    <div class="details_entry details">'):
                tel = lines[i+5].replace(' ','')
                if not tel.startswith('+39'):
                    if "+" in tel and tel[1] == '3':
                        outputFile.write(lines[i+1].replace('\n','') + ', ' + tel.replace('+', '+39'))
                        if tel[0] == '0':
                            outputFile.write(lines[i+1].replace('\n','') + ', ' + tel)
                elif tel.startswith('0039'):
                    outputFile.write(lines[i+1].replace('\n','') + ', ' + tel)
                    outputFile.write(lines[i+1].replace('\n','') + ', ' + tel)

Note that this script is aimed to the Italian telephone numbers (note the prefixes that are checked in some if clauses)!
However, once the folder is modified and the script is executed, a file called csvToConvert.csv is generated.
Subsequently, this file must be passed to a program called CVS to VCard, which takes care of converting the csv file into a vcard file.
Then, once the vcard file is generated, just enter it on google contacts (https://contacts.google.com/ > more > import), and you're done! Magically you will find all the contacts in the phone book of your android phone!

I hope I have been helpful.


You can export the Telegram contacts via the desktop app by going to Settings > Advanced > Export Telegram data. Now select only contacts and export as JSON.

Now you can transform this into a Google Calendar format CSV file using jq CLI-tool.

jq '[
  # Rename fields to Google Contacts CSV format and format phone number starting 00 to +
  | .phone_number |= sub("^00"; "+") 
  | .["Given Name"] = .first_name 
  | .["Family Name"] = .last_name 
  | del(.first_name, .last_name) 
  | .["Phone 1 - Type"] = "Mobile" 
  | .["Phone 1 - Value"] = .phone_number
  | del(.phone_number, .date, .date_unixtime)]
  # Convert array of objects to CSV, from https://stackoverflow.com/a/71505734/13994822
  | (map(keys) | add | unique) as $cols
  | map(. as $row | $cols | map($row[.])) as $rows 
  | $cols, $rows[]
  | @csv' \
-r "Downloads/Telegram Desktop/DataExport_[DATE]/result.json" \
| tee contacts.csv

This can then be imported at Google Contacts via the import button Import button symbol. As of 2023-05-15 this is on top-right of the application UI.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .