Twilio call to your phone example

Dependencies:   EthernetInterface HTTPClient mbed-rtos mbed

twilio.cpp

Committer:
wolfSSL
Date:
2014-12-08
Revision:
0:215c9ef8ad80

File content as of revision 0:215c9ef8ad80:

/* twilio.c
 *
 * Copyright (C) 2006-2014 wolfSSL Inc.
 *
 * This file is part of CyaSSL.
 *
 * CyaSSL is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * CyaSSL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */


#include "mbed.h"
#include "EthernetInterface.h"
#include "HTTPClient.h"

HTTPClient http;

static char str[1500] ;

void twilio_call(char *twilio_AccountSid,
                 char *twilio_AuthToken,
                 char *twilio_From,
                 char *twilio_To,
                 char *twilio_Url)
{
    int ret ;

#define ALLOWANCE 100
#define SAFIX     10

    static const char twilio_api[] = "https://api.twilio.com/2010-04-01/Accounts/%s/%s.%s" ;
    static const char twilio_Method[]   = "GET" ;
    static const char twilio_FBMethod[] = "GET" ;
    static const char twilio_StatusCBM[]= "GET" ;
    static const char twilio_Record[]   = "false" ;    

    static char twilio_request[] = "Calls" ;
    static char twilio_twiML[] = "xml" ;

    static char uri[ 
        sizeof(twilio_AccountSid)
        +sizeof(twilio_AuthToken)
        +sizeof(twilio_api)
        +SAFIX+ALLOWANCE] ;
    static char HeaderLines[] =
        "User-Agent: CyaSSL-Twilio,1.0\n"
        "Host: api.twilio.com\n"
        "Accept: */*\n" ;

    //POST data
    HTTPMap params;
    HTTPText inText(str, 1500);

    params.put("From", twilio_From);
    params.put("To", twilio_To);
    params.put("Url", twilio_Url);
    params.put("Method", twilio_Method);    
    params.put("FaulbackMethod", twilio_FBMethod);
    params.put("StatusCallbackMethod", twilio_StatusCBM);
    params.put("Record", twilio_Record);
    
    printf("\nTrying to call %s\n", twilio_To);
    sprintf(uri, twilio_api, twilio_AccountSid, twilio_request, twilio_twiML) ;

    http.basicAuth(twilio_AccountSid, twilio_AuthToken);
    http.setHeader(HeaderLines) ;
    http.setSSLversion(1) ; /* TLSv1.0 */
    
    ret = http.post(uri, params, &inText);
    if (!ret) {
        printf("Executed POST successfully - read %d characters\n", strlen(str));
        printf("Result: %s\n", str);
    } else {
        printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
    }
}