using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; using org.mariuszgromada.math.mxparser; using TwitchChatTTS.Hermes.Socket; using TwitchChatTTS.Seven.Socket; using TwitchChatTTS.Chat.Emotes; using CommonSocketLibrary.Abstract; using CommonSocketLibrary.Common; using TwitchChatTTS.OBS.Socket; using TwitchChatTTS.Twitch.Socket.Messages; using TwitchChatTTS.Twitch.Socket; using TwitchChatTTS.Chat.Commands; using System.Text; using TwitchChatTTS.Veadotube; using TwitchChatTTS.Bus; using System.Reactive.Linq; using System.Net.WebSockets; namespace TwitchChatTTS { public class TTS : IHostedService { public const int MAJOR_VERSION = 4; public const int MINOR_VERSION = 7; private readonly User _user; private readonly HermesApiClient _hermesApiClient; private readonly SevenApiClient _sevenApiClient; private readonly HermesSocketClient _hermes; private readonly OBSSocketClient _obs; private readonly SevenSocketClient _seven; private readonly TwitchWebsocketClient _twitch; private readonly VeadoSocketClient _veado; private readonly ICommandFactory _commandFactory; private readonly ICommandManager _commandManager; private readonly IEmoteDatabase _emotes; private readonly ServiceBusCentral _bus; private readonly Configuration _configuration; private readonly ILogger _logger; public TTS( User user, HermesApiClient hermesApiClient, SevenApiClient sevenApiClient, [FromKeyedServices("hermes")] SocketClient hermes, [FromKeyedServices("obs")] SocketClient obs, [FromKeyedServices("7tv")] SocketClient seven, [FromKeyedServices("twitch")] SocketClient twitch, [FromKeyedServices("veadotube")] SocketClient veado, ICommandFactory commandFactory, ICommandManager commandManager, IEmoteDatabase emotes, ServiceBusCentral bus, Configuration configuration, ILogger logger ) { _user = user; _hermesApiClient = hermesApiClient; _sevenApiClient = sevenApiClient; _hermes = (hermes as HermesSocketClient)!; _obs = (obs as OBSSocketClient)!; _seven = (seven as SevenSocketClient)!; _twitch = (twitch as TwitchWebsocketClient)!; _veado = (veado as VeadoSocketClient)!; _commandFactory = commandFactory; _commandManager = commandManager; _emotes = emotes; _bus = bus; _configuration = configuration; _logger = logger; } public async Task StartAsync(CancellationToken cancellationToken) { Console.Title = "TTS - Twitch Chat"; Console.OutputEncoding = Encoding.UTF8; License.iConfirmCommercialUse("abcdef"); if (string.IsNullOrWhiteSpace(_configuration.Hermes?.Token)) { _logger.Error("Tom to Speech API token not set in the yml file."); return; } try { var hermesVersion = await _hermesApiClient.GetLatestTTSVersion(); if (hermesVersion == null) { _logger.Error("Failed to fetch latest TTS version. Something went wrong."); return; } if (hermesVersion.MajorVersion > TTS.MAJOR_VERSION || hermesVersion.MajorVersion == TTS.MAJOR_VERSION && hermesVersion.MinorVersion > TTS.MINOR_VERSION) { _logger.Information($"A new update for TTS is avaiable! Version {hermesVersion.MajorVersion}.{hermesVersion.MinorVersion} is available at {hermesVersion.Download}"); var changes = hermesVersion.Changelog.Split("\n"); if (changes != null && changes.Any()) _logger.Information("Changelog:\n - " + string.Join("\n - ", changes) + "\n\n"); await Task.Delay(15 * 1000); } } catch { _logger.Warning("Failed to check for version updates."); } var disposables = new List(); // 7tv var connected = _bus.GetTopic("tts_connected"); disposables.Add(connected.FirstAsync().Subscribe(async (data) => { if (data.Value is not User user) { _logger.Warning("Something went wrong. Unable to fetch 7tv data."); return; } if (user.TwitchUserId == default) { _logger.Warning("Unable to fetch 7tv data. If this is wrong, ensure your Tom to Speech token is valid."); return; } var emoteSet = await _sevenApiClient.FetchChannelEmoteSet(_user.TwitchUserId); if (emoteSet != null) { _user.SevenEmoteSetId = emoteSet.Id; _logger.Debug($"Fetched the 7tv emote set id [emote set id: {emoteSet.Id}]"); } await InitializeEmotes(_sevenApiClient, emoteSet); await InitializeSevenTv(); })); disposables.Add(connected.FirstAsync().Subscribe(async (data) => { if (data.Value is not User user) { _logger.Warning("Something went wrong. Not connecting to Twitch."); return; } if (user.TwitchUserId == default) { _logger.Warning("Not connecting to Twitch. If this is wrong, ensure your Tom to Speech token is valid."); return; } try { await _twitch.Connect(); } catch (Exception e) { _logger.Error(e, "Failed to connect to Twitch websocket server."); } })); _commandManager.Update(_commandFactory); await InitializeVeadotube(); await InitializeHermesWebsocket(); await InitializeObs(); // Check if user has successfully connected. await Task.Run(async () => { await Task.Delay(TimeSpan.FromSeconds(5)); if (_user.TwitchUserId == default) _logger.Warning("Ensure your Tom to Speech token in the tts.config.yml file is valid."); }); } public Task StopAsync(CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) _logger.Warning("Application has stopped due to cancellation token."); else _logger.Warning("Application has stopped."); return Task.CompletedTask; } private async Task InitializeHermesWebsocket() { try { _hermes.Initialize(); await _hermes.Connect(); } catch (WebSocketException e) when (e.Message.Contains("The server returned status code '502'")) { _logger.Error("Could not connect to Tom to Speech server."); } catch (Exception e) { _logger.Error(e, "Connecting to hermes failed. Skipping hermes websockets."); } } private async Task InitializeSevenTv() { try { _seven.Initialize(); await _seven.Connect(); } catch (Exception e) { _logger.Error(e, "Connecting to 7tv failed. Skipping 7tv websockets."); } } private async Task InitializeObs() { try { _obs.Initialize(); await _obs.Connect(); } catch (Exception) { _logger.Warning("Connecting to obs failed. Skipping obs websockets."); } } private async Task InitializeVeadotube() { try { _veado.Initialize(); await _veado.Connect(); } catch (Exception e) { _logger.Warning(e, "Failed to connect to Veado websocket server."); } } private async Task InitializeEmotes(SevenApiClient sevenapi, EmoteSet? channelEmotes) { var globalEmotes = await sevenapi.FetchGlobalSevenEmotes(); if (channelEmotes != null && channelEmotes.Emotes.Any()) { _logger.Information($"Loaded {channelEmotes.Emotes.Count()} 7tv channel emotes."); foreach (var entry in channelEmotes.Emotes) _emotes.Add(entry.Name, entry.Id); } if (globalEmotes != null && globalEmotes.Any()) { _logger.Information($"Loaded {globalEmotes.Count()} 7tv global emotes."); foreach (var entry in globalEmotes) _emotes.Add(entry.Name, entry.Id); } } } }