How to format strings for Particle.publish?

Hey Particle Community,

So. C++ and strings make me kind of squirley. I just never get them right. So here’s a question that I need some help with, because my google-foo has turned up empty. I have a function, to send things out via twilio, and that requires sending some things to the webhook. How do I format strings to pass to Particle.publish properly? Originally I had all my things to pass like this:

Particle.publish("twilio", "{\"TWIL_ID\": \”XXXXXXXXXXXXXXXXXXXXXXXX\”},60,PRIVATE);

But that got pretty long.

So I know this “%s” is wrong, but using a char twilIDI[] = “XXXXX” etc. compiles, but then I think becomes incorrectly formatted.

void sendMessage(String msg)
{
 char publishString[200];

    String twilID = “XXXXXXXXXXXXXXXXXXXXXX”;
    String twilToken = "XXXXXXXXXXXXXXXXXXXXXX";
    String toNumber = “XXXXXXX”;
    String fromNumber = “XXXXXXX”;
    String message = msg;

    sprintf(publishString,"{\"TWIL_ID\": %s,\"TWIL_TOKEN\": %s,\"TO_NUMBER\": %s, \"FROM_NUMBER\": %s, \"SPARK_EVENT_VALUE\": %s}",twilID,twilToken,toNumber,fromNumber,message);
    Particle.publish("twilio",publishString,60, PRIVATE);

    Serial.println("message sent!");
}

Suggestions?

Thanks!

You need to write \"%s\" or have the enclosing double quotes (\") as part of your input strings.

Hm. I get this warning:
warning: format '%s' expects argument of type 'char*', but argument 5 has type 'String' [-Wformat=]

Got it! There might be a more graceful way, but solved my error.

void sendMessage(char *msg)
{
    char publishString[200];

    char twilID[] = “XXX”;
    char twilToken[] = “XXX”;
    char toNumber[] = “XXX”;
    char fromNumber[] = “XXX”;


    sprintf(publishString,"{\"TWIL_ID\": \"%s\",\"TWIL_TOKEN\": \"%s\",\"TO_NUMBER\": \"%s\", \"FROM_NUMBER\": \"%s\", \"SPARK_EVENT_VALUE\": \"%s\"}",twilID,twilToken,toNumber,fromNumber,msg);
    Particle.publish("twilio",publishString,60,PRIVATE);
}

That’s the “better” way, since I don’t like String, but with String you’d either write (const char*)myString or myString.c_str()