Skip to content

Instantly share code, notes, and snippets.

@Amir-P
Last active October 2, 2023 09:41
Show Gist options
  • Save Amir-P/d7017ce65da1bd40f4372dfbd2b68ec2 to your computer and use it in GitHub Desktop.
Save Amir-P/d7017ce65da1bd40f4372dfbd2b68ec2 to your computer and use it in GitHub Desktop.
Convert JSON exported contacts from Telegram to vCard (.vcf)
import sys
import json
input_file = sys.argv[1]
output_file = sys.argv[2]
output_file = open(output_file, 'w')
def to_vcf_string(first_name, last_name, phone):
vcf_lines = []
vcf_lines.append('BEGIN:VCARD')
vcf_lines.append('VERSION:4.0')
vcf_lines.append('N:%s' % (last_name + ';' + first_name))
vcf_lines.append('FN:%s' % (first_name + ('' if len(last_name) == 0 else ' ') + last_name))
vcf_lines.append('TEL:%s' % phone)
vcf_lines.append('END:VCARD')
vcf_string = '\n'.join(vcf_lines) + '\n'
return vcf_string
with open(input_file) as json_file:
data = json.load(json_file)
contacts_list = data['contacts']['list']
for contact in contacts_list:
first_name = contact['first_name']
last_name = contact['last_name']
if len(first_name) == 0 and len(last_name) == 0:
continue
phone = contact['phone_number']
output_file.write(to_vcf_string(first_name, last_name, phone).encode('utf-8'))
output_file.close()
@MrWesturn
Copy link

Hello
how give him json file?

@Amir-P
Copy link
Author

Amir-P commented Oct 2, 2023

Hi. Just run the script passing json file path and desired output path as input like this: python converter.py json_file_path output_file_path. @MrWesturn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment