Using twilio REST api on spark ! Could use some help

I’m trying to get my spark core to send an SMS using the twilio REST api. Been really unsuccesful so far. Any tips or pointings out mistakes would be greatly appreciated. I’m new to spark aswell as the REST API scene.
My acc SID and my auth token were replaced in the code with ACC_SID_GOES_HERE and AUTH_TOKEN_HERE.

Twilio requires you to send info such as To, From, Body and I’m not sure how that would be implemented as parameters of the Post function ?

F.ex

to : “my Phone number"
from :” twilio phone number"
body: “nice text im sending you”

how would I send this info to twlio ? and am i even using the right approach to the REST api ?

https://www.twilio.com/docs/api/rest/sending-messages - here’s what I found so far and here’s my code

// This #include statement was automatically added by the Spark IDE.
#include "application.h"
#include "HttpClient/HttpClient.h"

int led=D7;

unsigned int nextTime = 0;    // Next time to contact the server
HttpClient http;

http_header_t headers[] = {
    { "Accept" , "*/*"},
    { NULL, NULL } // NOTE: Always terminate headers will NULL
};


http_request_t request;
http_response_t response;



void setup() {
  pinMode(led, OUTPUT); // set the D7 LED as output
  Spark.function("blink",blinkfunc);    // a POST request for "blink" will reference blinkfunc, defined below

  
  request.hostname = "https://ACC_SID_GOES_HERE:AUTH_TOKEN_HERE@api.twilio.com/2010-04-01";
    request.port = 80;
    request.path = "/Accounts/ACCSID_GOES_HERE/Messages";
    
     http.post(request, response, headers);
}

// call the below function when the POST request matches it
int blinkfunc(String command) {
    
    http.post(request, response, headers);
    digitalWrite(led, HIGH);    // turn it on
    delay(1000);    // wait 1 second
    digitalWrite(led, LOW); // turn it off
    delay(1000);    // wait another second
    return 1;   // return 1 to show that this worked.
    
}


void loop() {
//not doing anything here

}

I’ve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy

Blinkfunc is working fine btw - just having trouble with the twilio API - Im using blinkfunc to check the LED to make sure that the function was run and my http-post called

@Kevinruder, the Core doesn’t do https, only http :frowning:

oh LoL I didn’t know there was a difference !

1 Like

@peekay123 is right about the HTTPS request which is something the Core will not do, and while there are work-arounds I would suggest using an IFTTT recipe with a Spark trigger and SMS action or hang in there for Spark Webhooks which will blow your mind :smile:

2 Likes

@Kevinruder I published a recipe you can use right away… it works great!

##Spark Core to SMS IFTTT Recipe

##Modify this code however you like:

// Sends a text message every 30 seconds, pulses the RGB red during publish
void setup() {

}

void loop() {
    RGB.control(true);
    Spark.publish("my-sms", "Hello Sparky!", 60, PRIVATE);
    RGB.color(255,0,0);
    delay(2000);
    RGB.control(false);
    delay(28000);
}
3 Likes

The Temboo library is another solution for accessing the Twilio functionality :smile:

@bDub @techbutler Thanks for all the replies - really helpful IFTTT seems like a really cool tool. I’ll look into the temboo library aswell