Sample code for 30+ languages & platforms
Visual FoxPro Requires Chilkat v11.0.0+

Clickatell One API: Send WhatsApp reply buttons

See more Clickatell Examples

Send an appointment question with Confirm and Reschedule buttons within 24 hours of the customer’s last WhatsApp message. This standalone example ends when Clickatell returns the send result. The response gives the outbound message ID. A customer’s eventual selection is a separate inbound event.

Clickatell API reference

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL loHttp
LOCAL loBody
LOCAL loResp
LOCAL lnSent
LOCAL loReply
LOCAL lnParsed

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
*  Load the key for this One API integration from your application configuration.
loHttp.SetRequestHeader("Authorization","ONE_API_KEY")
loHttp.SetRequestHeader("Accept","application/json")

loBody = CreateObject('Chilkat.JsonObject')
loBody.UpdateString("messages[0].channel","whatsapp")
loBody.UpdateString("messages[0].to","PHONE_NUMBER")
loBody.UpdateString("messages[0].button.body","Can you attend your appointment tomorrow at 10:00?")
*  The visible titles can be translated. Each button has a distinct application ID.
loBody.UpdateString("messages[0].button.items[0].type","reply")
loBody.UpdateString("messages[0].button.items[0].title","Confirm")
loBody.UpdateString("messages[0].button.items[0].postbackData","appt-1042-confirm")
loBody.UpdateString("messages[0].button.items[1].type","reply")
loBody.UpdateString("messages[0].button.items[1].title","Reschedule")
loBody.UpdateString("messages[0].button.items[1].postbackData","appt-1042-reschedule")

loResp = CreateObject('Chilkat.HttpResponse')
lnSent = loHttp.HttpJson("POST","https://platform.clickatell.com/v1/message",loBody,"application/json",loResp)
IF (loResp.StatusCode = 0) THEN
    ? "No HTTP response. Check the message report before resending."
    RELEASE loHttp
    RELEASE loBody
    RELEASE loResp
    CANCEL
ENDIF

loReply = CreateObject('Chilkat.JsonObject')
lnParsed = loReply.Load(loResp.BodyStr)
IF ((lnSent <> 1) OR (loResp.StatusCode <> 202)) THEN
    ? "Send returned HTTP " + STR(loResp.StatusCode)
    IF (loReply.HasMember("error.description") = 1) THEN
        ? loReply.StringOf("error.description")
    ENDIF

    RELEASE loHttp
    RELEASE loBody
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

IF (loReply.HasMember("error.code") = 1) THEN
    IF (loReply.IsNullOf("error.code") <> 1) THEN
        ? "Submission error " + loReply.StringOf("error.code") + "  " + loReply.StringOf("error.description")
        RELEASE loHttp
        RELEASE loBody
        RELEASE loResp
        RELEASE loReply
        CANCEL
    ENDIF

ENDIF

*  HTTP acceptance and the recipient result are separate checks.
IF ((lnParsed <> 1) OR (loReply.JsonTypeOf("messages[0].accepted") <> 5)) THEN
    ? "Missing recipient result. Check the message report."
    RELEASE loHttp
    RELEASE loBody
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

IF (loReply.BoolOf("messages[0].accepted") <> 1) THEN
    ? "Message rejected: " + loReply.StringOf("messages[0].error.code") + "  " + loReply.StringOf("messages[0].error.description")
    RELEASE loHttp
    RELEASE loBody
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

IF (loReply.JsonTypeOf("messages[0].apiMessageId") <> 1) THEN
    ? "Accepted without a message ID. Check the message report."
    RELEASE loHttp
    RELEASE loBody
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

? "Accepted. Message ID: " + loReply.StringOf("messages[0].apiMessageId")

RELEASE loHttp
RELEASE loBody
RELEASE loResp
RELEASE loReply