使用 Twilio 从 S3 发送 VCard 时出错

问题描述

我面临同样的问题。我希望你能指导我一点。

生成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,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 的 Content-Type。有关有效内容类型的详细信息,请参阅 Twilio 的 documentation on accepted content types

图片: enter image description here enter image description here

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

解决方法

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

文字:文字/电子名片

Accepted Content Types for Media

,

这里是 Twilio 开发者布道者。

目前,您的 VCard 具有 Twilio 无法识别的 binary/octet-stream 内容类型。

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

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

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

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...