amazon-web-servicesamazon-s3twiliommstwilio-twiml

Error in sending VCard from S3 using Twilio


I am facing the same issue. I hope you can guide me a bit.

Generating VCard function:

import object
import base64

def b64_image(filename):
    with open(filename, 'rb') as f:
        b64 = base64.b64encode(f.read())
        return b64.decode('utf-8')

def make_vcard(fname,lname,email,phonenumber,CompanyName,website,photo):
    file_name='{}.vcf'.format(fname)
    with open(file_name , 'w') as file_vcard:
    vcard = vobject.vCard()
    o = vcard.add('fn')
    o.value = fname
    
    o = vcard.add('N')
    o.value = vobject.vcard.Name(family=lname, given=fname)

    o = vcard.add('email')
    o.type_param = 'INTERNET'
    o.value = email

    o = vcard.add('org')
    o.value = [CompanyName]

    o = vcard.add('TEL')
    o.type_param = 'HOME'
    o.value = phonenumber

    o = vcard.add('url')
    o.type_param = "Website"
    o.value = website

    o = vcard.add('PHOTO;ENCODING=b;TYPE=image/jpeg')
    o.value = b64_image(photo)

    o = vcard.add('adr')
    o.value = vobject.vcard.Address('900 riverside','gilroy', 'ca', '95000', 'USA')

    file_vcard.write(vcard.serialize())

fname='Test'
lname='Test'
email='Test@gmail.com'
phonenumber='+16500000000'
CompanyName = 'Test'
website='http://google.com'
photo='default.jpg'
make_vcard(fname,lname,email,phonenumber,CompanyName,website,photo)

For uploading it on S3 i am using following code:

import boto3

def upload_vcard_on_bucket(file_path,file_name):
    session = boto3.session.Session(
    aws_access_key_id='xxxxxxx',
    aws_secret_access_key='xxxxxxx', 
    region_name='xxxxxxx',
    )

    # # Define client and resource for s3
    s3_client = session.client('s3')
    s3 = session.resource('s3')
    bucket = "xxxxxxx"
    upload_vcard_path = 'media/vcard/'
    base_url = "https://stg-xxxxxxx.s3.xxxxxxx.amazonaws.com/"

    upload_original_file = (upload_vcard_path + file_name)
    s3.Bucket(bucket).upload_file(file_path, upload_original_file)

    return upload_original_file


a = upload_vcard_on_bucket('Try.vcf','Try.vcf')

Sending MMS Using Twilio.

from twilio.rest import Client

account_sid = 'XXXXXXX' 
auth_token = 'XXXXXXX' 
from_number = '+1XXXXXXXXXX' 
message = 'hi' 
vcard_url = 'https://stg-XXXXX.s3.XXXXX.amazonaws.com/media/vcard/959d-2310efc9a14e.vcf'
company_personal_number = '+91XXXXXXXXXX'
client = Client(account_sid, auth_token)

message = client.messages.create(
                            from_=from_number,
                            body=message,
                            media_url=[vcard_url],
                            to=company_personal_number
                          )

Twillio Error:

Discription. Twilio is unable to process the Content-Type of the provided URL. Please see Twilio's documentation on accepted content types for more information on valid Content-Types.

Images: enter image description here enter image description here

Testing VCard link: https://chatbot-nlp.s3.ap-south-1.amazonaws.com/media/vcard/bf2d5936-70f5-4df2-9f79-7e558d6025a3.vcf


Solution

  • Twilio developer evangelist here.

    Currently your VCard has a Content-Type of binary/octet-stream which Twilio does not recognise.

    Instead, when you upload the file to S3 you should set the content type to text/vcard.

    With boto3 you should be able to do this by including further arguments to the upload_file method, like so:

    s3.Bucket(bucket).upload_file(
      file_path,
      upload_original_file,
      file_name,
      ExtraArgs={'ContentType': 'text/vcard'}
    )
    

    See the answers to this question for other ways to set the Content-Type.