Triggering flows

Introduction

Triggering a flow on a MessageThread is done by posting a FlowRun to /flow-runs. A FlowRun will start the chat on the specified MessageThread, once created the FlowRun will have the status queued we will directly try to start the flow on the MessageThread, if successful the status will change to running and billing will start.

In this guide we will show you how to create a WhatsApp MessageThread and trigger a flow on it.

Prerequisites

Communication visualized

Example

Firstly we need a MessageThread

curl -X POST "https://api.recrubo.com/public/v2/message-threads" \
    -H "Authorization: Bearer xx-xx-xx-xx-xx" \
    -H "Content-Type: application/vnd.api+json" \
    -d '{
        "data": {
            "type": "messageThreads",
            "attributes": {
                "channel": "whatsapp",
                "identifier": "31612345678"
            }
        }
    }'

Secondly we can trigger the flow on our MessageThread, as the channel we are using is Whatsapp we are required to set a messageTemplate as initial message.

curl -X POST "https://api.recrubo.com/public/v2/flow-runs" \
    -H "Authorization: Bearer xx-xx-xx-xx-xx" \
    -H "Content-Type: application/vnd.api+json" \
    -d '{
        "data": {
            "type": "flowRuns",
            "attributes": {
                "flowId": "eb3c92e9-2548-4e4c-95d5-adfb94270c3f",
                "messageThreadId": "7125853f-26ac-4a65-9044-cab092806249",
                "state": {},
                "initialMessage": {
                    "type": "template",
                    "template": {
                        "id": "7fqa9715-5e7c-4328-ab47-bb9e595438c9",
                        "components": [
                            {
                                "type": "body",
                                "parameters": [
                                    {
                                        "type": "text",
                                        "text": "John"
                                    }
                                ]
                            }
                        ]
                    }
                }
            }
        }
    }'

The initial response you will get will look something like this:

{
    "data": {
        "id": "cbb3835f-137e-49ed-b5cb-e72eefde7da2",
        "type": "flowRuns",
        "attributes": {
            "createdAt": "2024-04-16T07:05:39+00:00",
            "updatedAt": "2024-04-16T07:05:39+00:00",
            "flowId": "eb432e9-2548-4e4c-95d5-adfb94270c3f",
            "messageThreadId": "7125853f-26ac-4a65-9044-cab092806249",
            "state": {},
            "initialMessage": {
                "type": "template",
                "template": {
                    "id": "7w9a9715-5e7c-4328-ab47-bb9e595438c9",
                    "components": [
                        {
                            "type": "body",
                            "parameters": [
                                {
                                    "type": "text",
                                    "text": "John"
                                }
                            ]
                        }
                    ]
                }
            },
            "context": {},
            "status": "created",
            "cost": 0
        }
    }
}

Note how status is queued and cost is 0 in the background we will try to start the flow on the messageThread. You will we notified with a WebhookPayload once the flow has started.

Last updated