Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate VCard and Send Via Twilio

I am writing a Twilio + Parse app which lets users share their contact info via SMS. I have a sample Vcard as a string in javascript:

  message = '';
  message += 'BEGIN:VCARD';
  message += 'BDAY;VALUE=DATE:1963-09-21';
  message += 'VERSION:3.0';
  message += 'N:Stenerson;Derik';
  message += 'FN:Derik Stenerson';
  message += 'ORG:Microsoft Corporation';
  message += 'ADR;TYPE=WORK,POSTAL,PARCEL:;;One Microsoft Way;Redmond;WA;98052-6399;USA';
  message += 'TEL;TYPE=WORK,MSG:+1-425-936-5522';
  message += 'TEL;TYPE=WORK,FAX:+1-425-936-7329';
  message += 'EMAIL;TYPE=INTERNET:[email protected]';
  message += 'END:VCARD';
  message += 'BEGIN:VCARD';
  message += 'VERSION:3.0';
  message += 'N:Ganguly;Anik';
  message += 'FN:Anik Ganguly';
  message += 'ORG: Open Text Inc.';
  message += 'ADR;TYPE=WORK,POSTAL,PARCEL:;Suite 101;38777 West Six Mile Road;Livonia;MI;48152;USA';
  message += 'TEL;TYPE=WORK,MSG:+1-734-542-5955';
  message += 'EMAIL;TYPE=INTERNET:[email protected]';
  message += 'END:VCARD';
  message += 'BEGIN:VCARD';
  message += 'VERSION:3.0';
  message += 'N:Moskowitz;Robert';
  message += 'FN:Robert Moskowitz';
  message += 'EMAIL;TYPE=INTERNET:[email protected]';
  message += 'END:VCARD';

And I am trying to figure out how to send this to a phone number as a vcard so the phone detects it instead of the text.

I have a method:

function respondWithMessage(message, response) {
  response.set('Content-Type', 'text/xml');
  var xmlVersion = '<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n';
  message = '<Message>\n' + message + '\n</Message>';
  message = '<Response>\n' + message + '\n</Response>';
  message = xmlVersion + message;
  response.send(message);
}

With which I am able to send text messages. In their documentation, Twilio states that text/vcard messages are supported here: https://www.twilio.com/docs/api/rest/accepted-mime-types But I have been unable to get it to work. Could you provide an example of how to send this VCard via SMS with Twilio? Thanks!

like image 621
AttilaTheFun Avatar asked Sep 20 '15 02:09

AttilaTheFun


People also ask

What is a vCard SMS?

vCards, also known as Virtual Contact Files, are a standardized (RFC 6350) file format for sending business/contact information which can be easily imported into address/contact books. This post will quickly walk you through creating a vCard using Python and sending it as an MMS using Twilio Programmable SMS.

How does twilio send SMS?

You simply specify the recipient phone number you want to send to, the Twilio resource you're sending from (phone number, short code, Messaging service, etc. ), and the content of the message (text, pictures, links, etc). This can be in response to a previous inbound message, or initiated independently.


2 Answers

How to send

To send a contact card in Twilio is pretty simple, use this curl command and it should work:

curl -X POST https://api.twilio.com/2010-04-01/Accounts/<account sid>/Messages.json \
--data-urlencode "Body=Your Heart Health Coach contact card" \
--data-urlencode "MediaUrl=https://mighty-health-assets.s3.amazonaws.com/vcf/James%20Li.vcf" \
--data-urlencode "From=<from>" \
--data-urlencode "To=<to>" \
-u <account sid>:<auth token>

You can actually use that vCard linked in the request for testing purposes.

What can go wrong

If your header are not properly configured, the contact card will not render properly on your iOS/Android phone, even though it seems to be a correct contact card.

Headers

The contact card on iOS can only display the same text as of your file, so name your file with the same filename header property on Content-Type and it should work accordingly

Content-Type: text/x-vcard
Content-Disposition: inline; filename="<You file name>.vcf"
Cache-Control: no-cache

Cached file

  1. When Twilio receives a file for the first time, it caches
  2. If you adjust the headers accordingly it won't impact the delivery of the VCF file, because it will use the cached version
  3. There is a way to overcome that

Make sure you submit it correctly from the first time, or you'll have to the the changes I suggest below

Twilio Caching

To remove the caching on your files or to set the appropriate cache policy, read: https://support.twilio.com/hc/en-us/articles/360024716314-How-Can-I-Change-the-Cache-Behavior-or-Message-Media-Files-

Troubleshooting I figured out that Twilio don't change cache based only on filename for VCF files, but by the file content itself, so you have to modify your file in order for it to clear the cache, not just the name.

like image 170
felipeclopes Avatar answered Oct 05 '22 23:10

felipeclopes


Twilio developer evangelist here. If you're trying to start a new message, then you don't need to use TwiML, but can simply use the rest API to send the information from your server.

With Parse.com it should be something like the following:

// Require and initialize the Twilio module with your credentials
var client = require('twilio')('ACCOUNT_SID', 'AUTH_TOKEN');

// Send an SMS message
client.sendSms({
    to:'to', 
    from: 'from', 
    body: 'Hello world!',
    mediaUrl: your-vcard-url
  }, function(err, responseData) { 
    if (err) {
      console.log(err);
    } else { 
      console.log(responseData.from); 
      console.log(responseData.body);
    }
  }
);

So all you need to do now, is make sure your-vcard-url is accessible by Twilio, and that its mime type is set to be text/vcard.

In other words, you can't just send the contents of the vcard via text message, but need to tell Twilio where the vcard actually is, so it includes it in the message.

An example of a valid URL for a vcard would be this one. Hope this helps you

like image 43
Marcos Placona Avatar answered Oct 05 '22 23:10

Marcos Placona