Twilio call to your phone example
Dependencies: EthernetInterface HTTPClient mbed-rtos mbed
twilio.cpp@0:215c9ef8ad80, 2014-12-08 (annotated)
- Committer:
- wolfSSL
- Date:
- Mon Dec 08 12:13:59 2014 +0000
- Revision:
- 0:215c9ef8ad80
Initial version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 0:215c9ef8ad80 | 1 | /* twilio.c |
wolfSSL | 0:215c9ef8ad80 | 2 | * |
wolfSSL | 0:215c9ef8ad80 | 3 | * Copyright (C) 2006-2014 wolfSSL Inc. |
wolfSSL | 0:215c9ef8ad80 | 4 | * |
wolfSSL | 0:215c9ef8ad80 | 5 | * This file is part of CyaSSL. |
wolfSSL | 0:215c9ef8ad80 | 6 | * |
wolfSSL | 0:215c9ef8ad80 | 7 | * CyaSSL is free software; you can redistribute it and/or modify |
wolfSSL | 0:215c9ef8ad80 | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 0:215c9ef8ad80 | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 0:215c9ef8ad80 | 10 | * (at your option) any later version. |
wolfSSL | 0:215c9ef8ad80 | 11 | * |
wolfSSL | 0:215c9ef8ad80 | 12 | * CyaSSL is distributed in the hope that it will be useful, |
wolfSSL | 0:215c9ef8ad80 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 0:215c9ef8ad80 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 0:215c9ef8ad80 | 15 | * GNU General Public License for more details. |
wolfSSL | 0:215c9ef8ad80 | 16 | * |
wolfSSL | 0:215c9ef8ad80 | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 0:215c9ef8ad80 | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 0:215c9ef8ad80 | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
wolfSSL | 0:215c9ef8ad80 | 20 | */ |
wolfSSL | 0:215c9ef8ad80 | 21 | |
wolfSSL | 0:215c9ef8ad80 | 22 | |
wolfSSL | 0:215c9ef8ad80 | 23 | #include "mbed.h" |
wolfSSL | 0:215c9ef8ad80 | 24 | #include "EthernetInterface.h" |
wolfSSL | 0:215c9ef8ad80 | 25 | #include "HTTPClient.h" |
wolfSSL | 0:215c9ef8ad80 | 26 | |
wolfSSL | 0:215c9ef8ad80 | 27 | HTTPClient http; |
wolfSSL | 0:215c9ef8ad80 | 28 | |
wolfSSL | 0:215c9ef8ad80 | 29 | static char str[1500] ; |
wolfSSL | 0:215c9ef8ad80 | 30 | |
wolfSSL | 0:215c9ef8ad80 | 31 | void twilio_call(char *twilio_AccountSid, |
wolfSSL | 0:215c9ef8ad80 | 32 | char *twilio_AuthToken, |
wolfSSL | 0:215c9ef8ad80 | 33 | char *twilio_From, |
wolfSSL | 0:215c9ef8ad80 | 34 | char *twilio_To, |
wolfSSL | 0:215c9ef8ad80 | 35 | char *twilio_Url) |
wolfSSL | 0:215c9ef8ad80 | 36 | { |
wolfSSL | 0:215c9ef8ad80 | 37 | int ret ; |
wolfSSL | 0:215c9ef8ad80 | 38 | |
wolfSSL | 0:215c9ef8ad80 | 39 | #define ALLOWANCE 100 |
wolfSSL | 0:215c9ef8ad80 | 40 | #define SAFIX 10 |
wolfSSL | 0:215c9ef8ad80 | 41 | |
wolfSSL | 0:215c9ef8ad80 | 42 | static const char twilio_api[] = "https://api.twilio.com/2010-04-01/Accounts/%s/%s.%s" ; |
wolfSSL | 0:215c9ef8ad80 | 43 | static const char twilio_Method[] = "GET" ; |
wolfSSL | 0:215c9ef8ad80 | 44 | static const char twilio_FBMethod[] = "GET" ; |
wolfSSL | 0:215c9ef8ad80 | 45 | static const char twilio_StatusCBM[]= "GET" ; |
wolfSSL | 0:215c9ef8ad80 | 46 | static const char twilio_Record[] = "false" ; |
wolfSSL | 0:215c9ef8ad80 | 47 | |
wolfSSL | 0:215c9ef8ad80 | 48 | static char twilio_request[] = "Calls" ; |
wolfSSL | 0:215c9ef8ad80 | 49 | static char twilio_twiML[] = "xml" ; |
wolfSSL | 0:215c9ef8ad80 | 50 | |
wolfSSL | 0:215c9ef8ad80 | 51 | static char uri[ |
wolfSSL | 0:215c9ef8ad80 | 52 | sizeof(twilio_AccountSid) |
wolfSSL | 0:215c9ef8ad80 | 53 | +sizeof(twilio_AuthToken) |
wolfSSL | 0:215c9ef8ad80 | 54 | +sizeof(twilio_api) |
wolfSSL | 0:215c9ef8ad80 | 55 | +SAFIX+ALLOWANCE] ; |
wolfSSL | 0:215c9ef8ad80 | 56 | static char HeaderLines[] = |
wolfSSL | 0:215c9ef8ad80 | 57 | "User-Agent: CyaSSL-Twilio,1.0\n" |
wolfSSL | 0:215c9ef8ad80 | 58 | "Host: api.twilio.com\n" |
wolfSSL | 0:215c9ef8ad80 | 59 | "Accept: */*\n" ; |
wolfSSL | 0:215c9ef8ad80 | 60 | |
wolfSSL | 0:215c9ef8ad80 | 61 | //POST data |
wolfSSL | 0:215c9ef8ad80 | 62 | HTTPMap params; |
wolfSSL | 0:215c9ef8ad80 | 63 | HTTPText inText(str, 1500); |
wolfSSL | 0:215c9ef8ad80 | 64 | |
wolfSSL | 0:215c9ef8ad80 | 65 | params.put("From", twilio_From); |
wolfSSL | 0:215c9ef8ad80 | 66 | params.put("To", twilio_To); |
wolfSSL | 0:215c9ef8ad80 | 67 | params.put("Url", twilio_Url); |
wolfSSL | 0:215c9ef8ad80 | 68 | params.put("Method", twilio_Method); |
wolfSSL | 0:215c9ef8ad80 | 69 | params.put("FaulbackMethod", twilio_FBMethod); |
wolfSSL | 0:215c9ef8ad80 | 70 | params.put("StatusCallbackMethod", twilio_StatusCBM); |
wolfSSL | 0:215c9ef8ad80 | 71 | params.put("Record", twilio_Record); |
wolfSSL | 0:215c9ef8ad80 | 72 | |
wolfSSL | 0:215c9ef8ad80 | 73 | printf("\nTrying to call %s\n", twilio_To); |
wolfSSL | 0:215c9ef8ad80 | 74 | sprintf(uri, twilio_api, twilio_AccountSid, twilio_request, twilio_twiML) ; |
wolfSSL | 0:215c9ef8ad80 | 75 | |
wolfSSL | 0:215c9ef8ad80 | 76 | http.basicAuth(twilio_AccountSid, twilio_AuthToken); |
wolfSSL | 0:215c9ef8ad80 | 77 | http.setHeader(HeaderLines) ; |
wolfSSL | 0:215c9ef8ad80 | 78 | http.setSSLversion(1) ; /* TLSv1.0 */ |
wolfSSL | 0:215c9ef8ad80 | 79 | |
wolfSSL | 0:215c9ef8ad80 | 80 | ret = http.post(uri, params, &inText); |
wolfSSL | 0:215c9ef8ad80 | 81 | if (!ret) { |
wolfSSL | 0:215c9ef8ad80 | 82 | printf("Executed POST successfully - read %d characters\n", strlen(str)); |
wolfSSL | 0:215c9ef8ad80 | 83 | printf("Result: %s\n", str); |
wolfSSL | 0:215c9ef8ad80 | 84 | } else { |
wolfSSL | 0:215c9ef8ad80 | 85 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
wolfSSL | 0:215c9ef8ad80 | 86 | } |
wolfSSL | 0:215c9ef8ad80 | 87 | } |