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


@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 are welcome to use and contribute to the site :)
    – beeshyams
    Dec 17 '18 at 12:30
  • 1
    I'm very proud of this! Today I will do the same on Stack overflow! I have some questions to which I would like to answer! :)
    – Memmo
    Dec 17 '18 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.