【问题标题】:Error in sending VCard from S3 using Twilio使用 Twilio 从 S3 发送 VCard 时出错
【发布时间】:2021-07-15 06:42:53
【问题描述】:

我也面临同样的问题。希望大家多多指教。

生成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 的 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

【问题讨论】:

    标签: amazon-web-services amazon-s3 twilio mms twilio-twiml


    【解决方案1】:

    这里是 Twilio 开发者宣传员。

    当前,您的 VCard 的 Content-Type 为 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'}
    )
    

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

    【讨论】:

      【解决方案2】:

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

      文字:文字/电子名片

      Accepted Content Types for Media

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多