Twilio phone calls by Dropbox phone book

Dependencies:   EthernetInterface HTTPClient mbed-rtos mbed

This simple Twilio example works with HTTPS based Twilio API from mbed, makes phone calls and talks text speech message by referring phone book file in Dropbox.

For using this example:

- Prepare mbed with Ethernet connection.

- Go to http://twilio.com, and sign up.

- Copy Twilio Sid, Token, phone number and TwiML URL in twilio.cpp.

static char twilio_AccountSid[] = "TWILIO_ACCOUNT_SID" ;
static char twilio_AuthToken[]  = "AUTH_TOKEN" ;
static const char twilio_From[] = "PHONE_NUMBER_FROM" ;
static const char twilio_Url[]  = "http://twimlbin.com/TWIML_PATH" ;

- Copy Dropbox file link URL to dropbox.cpp.

static char Dropbox_URL[] = "https://www.dropbox.com/PHONE_LIST_FILE" ;

- The demo makes a call from Twilio to the phone numbers listed on the Dropbox phone book. Phone book is list of the number CSV file.

- Set up a TwiML web page defining Twilio action, set the url to twilio_Url.

TwiML first step example:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
	<Say language="ja-JP">もしもし。聞こえますか?こちらツイリオです</Say>
</Response>

- Build, download it to mbed, and you will get a call on your phone.

Have fun!

For FRDM-k64f: http://mbed.org/users/wolfSSL/code/CyaSSL-Twilio-Dropbox-FRDM/

日本語:http://mbed.org/users/wolfSSL/code/CyaSSL-Twilio/wiki/CyaSSL-Twilio-Dropbox-jp

Files at this revision

API Documentation at this revision

Comitter:
wolfSSL
Date:
Mon Jul 21 11:29:59 2014 +0000
Commit message:
Twilio phone calls with Dropbox phone book;

Changed in this revision

EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
HTTPClient.lib Show annotated file Show diff for this revision Revisions of this file
dropbox.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
twilio.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Mon Jul 21 11:29:59 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/EthernetInterface/#e6b79f0ccd95
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPClient.lib	Mon Jul 21 11:29:59 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/wolfSSL/code/HTTPClient/#5d4739eae63e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dropbox.cpp	Mon Jul 21 11:29:59 2014 +0000
@@ -0,0 +1,63 @@
+/* dropbox.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"
+
+extern HTTPClient http;
+
+HTTPResult dropbox_get(const char *url, char *buff, int size)
+{
+    HTTPResult ret ;
+#define LOCATION_SIZE 128
+    static char location[LOCATION_SIZE] ;
+    static const char HeaderLines[] =
+        "User-Agent: curl/7.33.0\r\n"
+        "Accept: */*\r\n" ;
+
+    http.setHeader(HeaderLines) ;
+    http.setLocationBuf(location, LOCATION_SIZE) ;
+
+    ret = http.get(url, buff, size) ;
+    if ((ret == HTTP_OK) || (ret == HTTP_REDIRECT)) {
+        if(ret == HTTP_REDIRECT) {
+            /* goto next */
+        }
+    } else {
+        printf("++ Err = %d - HTTP ret = %d ++\n",
+               ret, http.getHTTPResponseCode());
+        return ret ;
+    }
+
+    if(ret != HTTP_REDIRECT) {
+        printf("No Redirection. Not reached at the contents.") ;
+    } else {
+        ret = http.get(location, buff, size) ;
+        if ((ret == HTTP_OK) || (ret == HTTP_REDIRECT)) {
+            /* goto next */
+        } else {
+            printf("++ Err = %d - HTTP ret = %d ++\n",
+                   ret, http.getHTTPResponseCode());
+            return ret ;
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Jul 21 11:29:59 2014 +0000
@@ -0,0 +1,76 @@
+/* main.cpp */
+/* Copyright (C) 2014 wolfSSL, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "HTTPClient.h"
+
+HTTPClient http;
+extern HTTPResult dropbox_get(const char *url, char * buff, int size) ;
+extern HTTPResult twilio_main(const char *phone) ;
+ 
+static char Dropbox_URL[] = "https://www.dropbox.com/PHONE_LIST_FILE" ;
+
+#define BUFF_SIZE 256
+static char buff[BUFF_SIZE] ;
+
+void thread_main(void const *av) {
+    dropbox_get(Dropbox_URL, buff, BUFF_SIZE) ;
+    char *p = buff ;
+    bool endflg = false ;
+    for(int i = 0 ; i<BUFF_SIZE; i++) {
+        if((p[i] == '\0') || (p[i] == '\n'))
+            endflg = true ;
+        if((p[i] == ',') || (p[i] == '\n') || (p[i] == '\0')){
+            p[i] = '\0' ;
+            twilio_main(p) ;
+            if(endflg)break ;
+            p = &(p[i+1]) ;
+        }
+    }
+}
+
+int main()
+{
+    int ret ;
+    void const *av ;
+    
+    EthernetInterface eth;
+
+    printf("Dropbox Starting,...\n") ;
+    ret = eth.init(); //Use DHCP
+    while(1) {
+        ret = eth.connect();
+        if(ret == 0)break ;
+    }
+    printf("IP = %s\n", eth.getIPAddress());
+    
+    //#define BOARD_FRDM_K64F
+    #ifdef BOARD_FRDM_K64F
+    #define STACK_SIZE 10000
+        Thread t( thread_main, NULL, osPriorityNormal, STACK_SIZE);
+    #else
+        thread_main(av) ;
+    #endif
+
+    while (true) {
+        Thread::wait(1000);
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Mon Jul 21 11:29:59 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#ff95651f53c7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Jul 21 11:29:59 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/04dd9b1680ae
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/twilio.cpp	Mon Jul 21 11:29:59 2014 +0000
@@ -0,0 +1,84 @@
+/* twilio.cpp */
+/* Copyright (C) 2014 wolfSSL, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include "mbed.h"
+#include "HTTPClient.h"
+
+extern HTTPClient http;
+
+static char str[1500] ;
+
+void twilio_main(const char *phone)
+{
+    int ret ;
+
+#define ALLOWANCE 100
+#define SAFIX     10
+
+    static char twilio_AccountSid[] = "TWILIO_ACCOUNT_SID" ;
+    static char twilio_AuthToken[]  = "AUTH_TOKEN" ;
+    static const char twilio_From[] = "PHONE_NUMBER_FROM" ;
+    static const char twilio_Url[]  = "http://twimlbin.com/TWIML_PATH" ;
+    
+    static const char *twilio_To   = phone ;
+    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("\nCalling %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 == HTTP_OK) {
+        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());
+    }
+}
\ No newline at end of file