Skip to content
Snippets Groups Projects
BotChat.cs 2.1 KiB
Newer Older
using TMPro;
using UnityEngine;

public class BotChat : MonoBehaviour, IChatSession
{
    [SerializeField] public TextMeshProUGUI _input;
    [SerializeField] private TextMeshProUGUI _output;

    [SerializeField] private string _agentName;
    [SerializeField] private string _userName;

    ChatSession session = null;

    [SerializeField] private BotChat other;

    // Start is called before the first frame update
    void Start()
    {
        _ = ChatSessionManager.Instance.StartSession(this, _agentName, _userName);
    }

    string m = "";
    public void Chat(string message)
    {
        m = message;
        ChatSingle();
    }

    public void ChatSingle()
    {
        if (session == null)
        {
            Debug.Log("Session not yet ready or terminated");
            return;
        }
        Debug.Log("Messsage: " + m);
        session.ChatSingle(m);
    }

    public void EndChatSession()
    {
        if (session == null)
        {
            Debug.Log("Session not yet ready or terminated");
            return;
        }
        session.EndChatSession();
    }


    public void OnSessionReady(ChatSession session)
    {
        this.session = session;
        Debug.Log("Session " + session.SessionId + " ready.");
        Debug.Log("Other session: " + other.session.SessionId);
        if (other.session != null)
        {
            Debug.Log("Initiating conversation.");
            other.Chat("Hi, it's nice to meet you! My name is " + session._agent.Name + "! I'd like to chat about the current eSports landscape a bit! What do you think of games such as counter-strike 2?");
        }
    }

    public void OnChatResponse(string response)
    {
        _output.text = response;
    }

    public void OnSessionError(ChatSession session, int _)
    {
        this.session = null;
    }

    public void OnChatResponse(ChatSession session, string response)
    {
        _output.text = response;

        other.Chat(response);
    }

    public void OnSessionEnd(ChatSession session)
    {
        session = null;
    }
}