BotGame API Logo Alternate

BotGame API - Documentation

Here are some code examples for reference, using dummy data. You would typically replace your API Key with your actual API Key and the Entity and/or Group name with the actual group name.

Do not forget to URL encode your Group and Entity names if they have spaces or unusual characters.

C# Example (Single Entity)

Here is an example of retrieving the attributes of a single Entity in C#.

using System;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        // Set the API key and entity name
        string apiKey = "your-api-key";
        string entityName = "your-entity-name";

        // Construct the URL with query parameters
        string url = $"https://botgameapi.com/app/bga.php?api_key={apiKey}&entity={entityName}";

        // Create an HttpClient instance
        HttpClient client = new HttpClient();

        try
        {
            // Send the GET request and retrieve the response
            HttpResponseMessage response = await client.GetAsync(url);

            // Check if the request was successful
            if (response.IsSuccessStatusCode)
            {
                // Read the response content as a string
                string responseBody = await response.Content.ReadAsStringAsync();

                // Process the response JSON or perform any other required operations
                Console.WriteLine(responseBody);
            }
            else
            {
                // Handle the error if the request was not successful
                Console.WriteLine($"Request failed with status code: {response.StatusCode}");
            }
        }
        catch (Exception ex)
        {
            // Handle any exceptions that occurred during the request
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
        finally
        {
            // Dispose of the HttpClient instance
            client.Dispose();
        }
    }
}

C# Example (Grouped Entities)

Here is an example of retrieving the attributes of all entities in a Group using C# and the ‘WebClient’ class.

using System;
using System.Net;

class Program
{
    static void Main()
    {
        // Set the API key and group name
        string apiKey = "your-api-key";
        string groupName = "your-group-name";

        // Construct the URL with query parameters
        string url = $"https://botgameapi.com/app/bgagroup.php?api_key={apiKey}&group={groupName}&send=all";

        // Create a WebClient instance
        WebClient client = new WebClient();

        try
        {
            // Perform the GET request and get the response as a string
            string response = client.DownloadString(url);

            // Process the response
            Console.WriteLine("Response: " + response);
        }
        catch (WebException ex)
        {
            // Handle any web request errors
            Console.WriteLine("Error: " + ex.Message);
        }

        // Wait for user input before exiting
        Console.ReadLine();
    }
}

C++ Example (Single Entity)

Here is an example of retrieving the attributes of a single Entity in C++ and the libcurl library.

#include <iostream>
#include <string>
#include <curl/curl.h>

// Callback function to write response data
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* response)
{
    size_t totalSize = size * nmemb;
    response->append(static_cast<char*>(contents), totalSize);
    return totalSize;
}

int main()
{
    // Set the API key and entity name
    std::string apiKey = "your-api-key";
    std::string entityName = "your-entity-name";

    // Construct the URL with query parameters
    std::string url = "https://botgameapi.com/app/bga.php?api_key=" + apiKey + "&entity=" + entityName;

    // Initialize libcurl
    CURL* curl = curl_easy_init();
    if (curl)
    {
        // Set the URL to send the GET request
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

        // Set the callback function to handle the response
        std::string response;
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);

        // Perform the request and retrieve the response
        CURLcode res = curl_easy_perform(curl);
        if (res == CURLE_OK)
        {
            // Process the response or perform any other required operations
            std::cout << "Response: " << response << std::endl;
        }
        else
        {
            // Handle the error if the request was not successful
            std::cerr << "Request failed: " << curl_easy_strerror(res) << std::endl;
        }

        // Cleanup
        curl_easy_cleanup(curl);
    }
    else
    {
        std::cerr << "Failed to initialize libcurl" << std::endl;
    }

    return 0;
}

PHP Example (Single Entity)

Here is an example of retrieving the attributes of a single Entity in PHP using the ‘file_get_contents()’ function.

<?php

// Set the API key and entity name
$apiKey = 'your-api-key';
$entityName = 'your-entity-name';

// Construct the URL with query parameters
$url = 'https://botgameapi.com/app/bga.php?api_key=' . urlencode($apiKey) . '&entity=' . urlencode($entityName);

// Send GET request and retrieve the response
$response = file_get_contents($url);

// Handle the response
if ($response !== false) {
    // Process the response or perform any other required operations
    echo "Response: " . $response;
} else {
    // Handle the error if the request failed
    echo "Request failed.";
}

Python Example (Single Entity)

Here is an example of retrieving the attributes of a single Entity in Python using the ‘requests’ library.

import requests

# Set the API key and entity name
api_key = 'your-api-key'
entity_name = 'your-entity-name'

# Construct the URL with query parameters
url = 'https://botgameapi.com/app/bga.php'
params = {
    'api_key': api_key,
    'entity': entity_name
}

# Send GET request and retrieve the response
response = requests.get(url, params=params)

# Handle the response
if response.status_code == 200:
    # Process the response or perform any other required operations
    print("Response:", response.json())
else:
    # Handle the error if the request failed
    print("Request failed.")

Java Example (Single Entity)

Here is an example of retrieving the attributes of a single Entity in Java.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class BgaApiClient {
    public static void main(String[] args) {
        // Set the API key and entity name
        String apiKey = "your-api-key";
        String entityName = "your-entity-name";

        // Construct the URL with query parameters
        String urlString = "https://botgameapi.com/app/bga.php?api_key=" + apiKey + "&entity=" + entityName;

        try {
            // Create a URL object from the string
            URL url = new URL(urlString);

            // Open a connection to the URL
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            // Get the response code
            int responseCode = connection.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK) {
                // Read the response
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                StringBuilder response = new StringBuilder();

                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();

                // Process the response or perform any other required operations
                System.out.println("Response: " + response.toString());
            } else {
                // Handle the error if the request failed
                System.out.println("Request failed. Response Code: " + responseCode);
            }

            // Close the connection
            connection.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

LUA Example (Single Entity)

Here is an example of retrieving the attributes of a single Entity in LUA using the ‘LuaSocket’ library.

local http = require("socket.http")
local ltn12 = require("ltn12")
local json = require("json")

-- Set the API key and entity name
local apiKey = "your-api-key"
local entityName = "your-entity-name"

-- Construct the URL with query parameters
local url = "https://botgameapi.com/app/bga.php?api_key=" .. apiKey .. "&entity=" .. entityName

-- Perform the GET request
local response = {}
local result, responseCode, responseHeaders, responseStatus = http.request{
    url = url,
    method = "GET",
    sink = ltn12.sink.table(response)
}

-- Check the response code
if responseCode == 200 then
    -- Convert the response to a string
    local responseBody = table.concat(response)

    -- Process the response or perform any other required operations
    local responseData = json.decode(responseBody)
    print("Response: " .. json.encode(responseData))
else
    -- Handle the error if the request failed
    print("Request failed. Response Code: " .. responseCode)
end

Visual Basic Example (Grouped Entities)

Here is an example of retrieving the attributes of Grouped Entities in VB using the ‘WebClient’ class.

Imports System.Net
Imports System.IO
Imports System.Text

Module MainModule
    Sub Main()
        ' Set the API key and group name
        Dim apiKey As String = "your-api-key"
        Dim groupName As String = "your-group-name"

        ' Construct the URL with query parameters
        Dim url As String = "https://botgameapi.com/app/bgagroup.php?api_key=" & apiKey & "&group=" & groupName & "&send=all"

        ' Create a WebClient instance
        Dim client As New WebClient()

        ' Perform the GET request
        Dim response As String = client.DownloadString(url)

        ' Process the response
        Console.WriteLine("Response: " & response)

        ' Wait for user input before closing the console window
        Console.ReadLine()
    End Sub
End Module

C# Unity Expression Evaluation Example

Here is an example of using the mathematical expression evaluator using Unity and Unity’s ‘UnityWebRequest’

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

public class MathEvaluator : MonoBehaviour
{
    private const string API_URL = "https://botgameapi.com/app/eval";
    private const string API_KEY = "YOUR_API_KEY_HERE"; // Replace with your API key

    void Start()
    {
        // Test the API with an expression
        EvaluateExpression("32*32+(6+4)", OnEvaluationSuccess, OnEvaluationFailed);
    }

    public void EvaluateExpression(string expression, System.Action<float> onSuccess, System.Action<string> onError)
    {
        StartCoroutine(EvaluateExpressionCoroutine(expression, onSuccess, onError));
    }

    private IEnumerator EvaluateExpressionCoroutine(string expression, System.Action<float> onSuccess, System.Action<string> onError)
    {
        // URL Encode the expression
        string encodedExpression = UnityWebRequest.EscapeURL(expression);

        // Create the URL
        string fullURL = $"{API_URL}?api_key={API_KEY}&expr={encodedExpression}";

        using (UnityWebRequest www = UnityWebRequest.Get(fullURL))
        {
            yield return www.SendWebRequest();

            if (www.result != UnityWebRequest.Result.Success)
            {
                onError?.Invoke($"Request failed: {www.error}");
            }
            else
            {
                // Parse the response
                string jsonResponse = www.downloadHandler.text;

                // In this example, we're using Unity's JsonUtility, but you can use other libraries
                // like Newtonsoft.Json for more complex use cases.
                EvaluationResponse response = JsonUtility.FromJson<EvaluationResponse>(jsonResponse);

                if (!string.IsNullOrEmpty(response.error))
                {
                    onError?.Invoke(response.error);
                }
                else
                {
                    onSuccess?.Invoke(response.result);
                }
            }
        }
    }

    private void OnEvaluationSuccess(float result)
    {
        Debug.Log($"Expression evaluated successfully. Result: {result}");
    }

    private void OnEvaluationFailed(string error)
    {
        Debug.LogError($"Expression evaluation failed: {error}");
    }

    [System.Serializable]
    public class EvaluationResponse
    {
        public float result;
        public string error;
    }
}

Important Points:

  1. Make sure you’ve added the correct API key where it says YOUR_API_KEY_HERE.
  2. Unity’s JsonUtility is quite limited. For more complex JSON parsing, you might consider using the Newtonsoft.Json library or a similar one.
  3. This script assumes that if the API response contains an “error” field, it’s an error. Otherwise, it takes the “result” as the evaluation of the expression.

 

To test this script in Unity:

  1. Attach the script to any GameObject.
  2. Play the scene in Unity Editor.
  3. Check the Unity Console for the result or error message.
10 views