Integrating GGWP’s Chat with External Chat Services: Unity Example 

At GGWP, we aim to simplify game moderation integration. A common question is how exactly to integrate our chat moderation if using an existing chat service such as Unity Vivox, Agora, or Photon. In this post, we’ll guide you on integrating GGWP Chat APIs using Unity Vivox as an example with a sample POST API request code snippet.

Why Use RESTful APIs?

RESTful APIs are a popular choice for exchanging data between game engines such as Unity and external servers or services. They provide a structured and scalable way to send and receive data, making them an excellent choice for various use cases, such as filtering messages, getting the reputation score of the users, and more!

Prerequisites

Before we dive into the integration process, here are some prerequisites you’ll need:

  • Unity installed on your computer (the code examples are based on Unity version 2019.4 or later)
  • Basic knowledge of C# programming
  • Access to GGWP API key

Integrating Unity with a Custom RESTful API

Step 1: Create a Unity Project

Start by creating a new Unity project or open an existing one where you want to integrate the RESTful API functionality.

Step 2: Create a singleton

In this example, we will be using a singleton to interact with our RESTful API. A singleton is a design pattern where only one instance of a class exists and is globally accessible. Here is a sample singleton class script to get you started:

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

public class APIClient : MonoBehaviour
{
    public static APIClient Instance { get; private set; }
    private const string apiUrl = "https://api.ggwp.com/chat/v2/message";
    [SerializeField] private string ggwpApiKey;

    private void Awake()
    {
        if (Instance != null && Instance != this)
        {
            Destroy(this);
        }
        else
        {
            Instance = this;
        }
    }

	public void SendRequest(string jsonData)
    {
        StartCoroutine(SendPostRequest(jsonData));
    }
    private IEnumerator SendPostRequest(string jsonData)
    {
        UnityWebRequest request = new UnityWebRequest(apiUrl, "POST");
        byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonData);
        request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
        request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
        request.SetRequestHeader("Content-Type", "application/json");
        request.SetRequestHeader("x-api-key", ggwpApiKey);
        yield return request.SendWebRequest();
        if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
        {
            Debug.LogError("POST request error: " +request.error);
        }
        else
        {
            Debug.Log("POST request successful!");
        }
    }
}

Create an empty GameObject and attach this script to it. You can then edit the ggwpApiKey variable from the inspector. The `SendRequest` method sends a POST request to the API with the provided JSON data.Note that although we chose to use a singleton here because of its globally accessible property, there are other ways to implement this. 

Step 3: Interact with the singleton

You can call the `SendRequest` method from any other script in your Unity project to interact with our API. Here’s an example of how to call it:

using UnityEngine;
using System.Collections;

public class GameLogic : MonoBehaviour
{
	private void Start()
	{
    	    // Example JSON data to send to the API
    	    string jsonData = "{" + 
				"\"session_id\": \"your_session_id_here\"," + 
				"\"user_id\": \"your_user_id_here\"," + 
				"\"message\": \"your_message_here\"" + 
			"}";
    	    // Call the API using the SendPostRequest method
    	    APIClient.Instance.SendRequest(jsonData);
	}
}

Create another GameObject and attach the GameLogic script to it. Adjust the JSON data and method call according to your functional requirements.

Conclusion

Integrating with the GGWP RESTful API endpoints allows you to expand your game’s capabilities by fetching and sending data to GGWP services for advance moderations! Check out our API documentation here. If you have any questions or need further assistance with API integration or wish to receive access to the pay as you go trial, feel free to contact us. We’re here to help you create a safe and positive community for all. Happy coding, and may your Unity games reach new heights!