使用 Twilio 从 S3 发送 VCard 时出错

Error in sending VCard from S3 using Twilio

我遇到了同样的问题。希望大家多多指导。

正在生成 VCard 函数:

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)

为了将其上传到 S3,我使用以下代码:

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')

使用 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 错误:

说明。 Twilio 无法处理所提供 URL 的内容类型。请参阅 Twilio 的 documentation on accepted content types 以获取有关有效内容类型的更多信息。

图片: enter image description here enter image description here

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

确保看到托管在 S3 上的文件的正确 MIME 类型?

正文:text/vcard

Accepted Content Types for Media

这里是 Twilio 开发人员布道者。

当前您的 VCard 的内容类型 binary/octet-stream Twilio 无法识别。

相反,当您将文件上传到 S3 时,您应该将内容类型设置为 text/vcard

使用 boto3,您应该能够通过向 upload_file 方法添加更多参数来做到这一点,如下所示:

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

参见