Build a Riddle from spreadsheet data

In this demo you see how to use the Riddle API to build Predictor Riddles "Pick The Winner". All the data is stored in a table and every row will be taken to build the Riddle.

For example, you could maintain all the World Cup matches in a database and create the appropriate "Pick The Winner" on time for each match day.

Step 1

Create an API Access Token here: Access Token. Important: Delete this token after you finish testing here.

Step 2

Insert an access token and modify the data in the table if you like and click on "Build Riddles". The Riddles will be created in your account and appear below.

How it works

We export the data from the table the JSON format that is required for the Riddle API.

Example Riddle JSON for each row

{
    type: "Predictor",
    publish: true,

    build: {
        title: {{ ROW.TITLE }},

        blocks: [
            {
                title: {{ ROW.TITLE }},
                type: "PickTheWinner",
                items: [{
                    title: {{ ROW.TEAM A }}
                    backgroundColor: {{ ROW.TEAM A BACKGROUND COLOR }},
                    textColor: {{ ROW.TEAM A TEXT COLOR }},
                    },
                    {
                        title: {{ ROW.TEAM B }}
                        backgroundColor: {{ ROW.TEAM B BACKGROUND COLOR }},
                        textColor: {{ ROW.TEAM B TEXT COLOR }},
                    }
                ],
            }
        ],
        result: {
            title: "Thank you!"
        }
    }
}

Example Symfony PHP controller

This is an example of how you can create a Riddle in PHP with Symfony framework using the Riddle API.

<?php

// src/Controller/RiddleApiController.php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;

class ResponseDTO
{
    public string $message;
    public int $status;
    public $data;
}

class RiddleDTO
{
    public string $UUID;
    public string $title;
    public string $type;
}

class RiddleApiController extends AbstractController
{
    #[Route('/api/riddle-builder', name: 'app_riddle_builder', methods: ['POST'])]
    public function buildRiddle(Request $request, HttpClientInterface $httpClient, SerializerInterface $serializer): JsonResponse
    {
        // read the access token from the request json body post
        $data = json_decode($request->getContent(), true);
        $riddleAccessToken = $data['riddleAccessToken'] ?? null;

        if ($riddleAccessToken === null) {
            return new JsonResponse(['message' => 'Missing riddleAccessToken'], 400);
        }

        $riddleData = $data['riddleData'] ?? null;

        if ($riddleData === null) {
            return new JsonResponse(['message' => 'Missing riddleData'], 400);
        }

        $client = $httpClient->withOptions([
            'base_uri' => 'https://www.riddle.com',
            'headers' => [
                'X-RIDDLE-BEARER' => 'Bearer ' . $riddleAccessToken,
            ]
        ]);

        $response = $client->request(
            'POST',
            '/api/v3/riddle-builder',
            [
                'json' => $riddleData
            ]
        );

        $statusCode = $response->getStatusCode();

        $responseDTO = new ResponseDTO();
        $responseDTO->status = $statusCode;

        if ($statusCode !== 200) {
            $responseDTO->message = 'Error';

            return new JsonResponse($responseDTO, $statusCode);
        }

        $content = $response->toArray();

        $responseDTO->message = 'Success';

        $riddleDTO= new RiddleDTO();
        $riddleDTO->UUID = $content['data']['UUID'];
        $riddleDTO->title = $content['data']['title'];
        $riddleDTO->type = $content['data']['type'];

        $responseDTO->data = $riddleDTO;

        return new JsonResponse($responseDTO, $statusCode);
    }
}