Twilio phone calls by Dropbox phone book

Dependencies:   EthernetInterface HTTPClient mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers twilio.cpp Source File

twilio.cpp

00001 /* twilio.cpp */
00002 /* Copyright (C) 2014 wolfSSL, MIT License
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00006  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in all copies or
00011  * substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00014  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018  */
00019 #include "mbed.h"
00020 #include "HTTPClient.h"
00021 
00022 extern HTTPClient http;
00023 
00024 static char str[1500] ;
00025 
00026 void twilio_main(const char *phone)
00027 {
00028     int ret ;
00029 
00030 #define ALLOWANCE 100
00031 #define SAFIX     10
00032 
00033     static char twilio_AccountSid[] = "TWILIO_ACCOUNT_SID" ;
00034     static char twilio_AuthToken[]  = "AUTH_TOKEN" ;
00035     static const char twilio_From[] = "PHONE_NUMBER_FROM" ;
00036     static const char twilio_Url[]  = "http://twimlbin.com/TWIML_PATH" ;
00037     
00038     static const char *twilio_To   = phone ;
00039     static const char twilio_api[] = "https://api.twilio.com/2010-04-01/Accounts/%s/%s.%s" ;
00040     static const char twilio_Method[]   = "GET" ;
00041     static const char twilio_FBMethod[] = "GET" ;
00042     static const char twilio_StatusCBM[]= "GET" ;
00043     static const char twilio_Record[]   = "false" ;    
00044 
00045     static char twilio_request[] = "Calls" ;
00046     static char twilio_twiML[] = "xml" ;
00047 
00048     static char uri[ 
00049         sizeof(twilio_AccountSid)
00050         +sizeof(twilio_AuthToken)
00051         +sizeof(twilio_api)
00052         +SAFIX+ALLOWANCE] ;
00053     static char HeaderLines[] =
00054         "User-Agent:  CyaSSL-Twilio,1.0\n"
00055         "Host: api.twilio.com\n"
00056         "Accept: */*\n" ;
00057 
00058     //POST data
00059     HTTPMap params;
00060     HTTPText inText(str, 1500);
00061 
00062     params.put("From", twilio_From);
00063     params.put("To", twilio_To);
00064     params.put("Url", twilio_Url);
00065     params.put("Method", twilio_Method);    
00066     params.put("FaulbackMethod", twilio_FBMethod);
00067     params.put("StatusCallbackMethod", twilio_StatusCBM);
00068     params.put("Record", twilio_Record);
00069     
00070     printf("\nCalling %s\n", twilio_To);
00071     sprintf(uri, twilio_api, twilio_AccountSid, twilio_request, twilio_twiML) ;
00072 
00073     http.basicAuth(twilio_AccountSid, twilio_AuthToken);
00074     http.setHeader(HeaderLines) ;
00075     http.setSSLversion(1) ; /* TLSv1.0 */
00076     
00077     ret = http.post(uri, params, &inText);
00078     if (ret == HTTP_OK) {
00079         printf("Executed POST successfully - read %d characters\n", strlen(str));
00080         printf("Result: %s\n", str);
00081     } else {
00082         printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
00083     }
00084 }