Skip to content

Commit 45cf6ec

Browse files
committed
It sends messages yay
1 parent b69b54c commit 45cf6ec

File tree

14 files changed

+153
-38
lines changed

14 files changed

+153
-38
lines changed

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@
9191
<artifactId>json</artifactId>
9292
<version>20240303</version>
9393
</dependency>
94+
<dependency>
95+
<groupId>org.slf4j</groupId>
96+
<artifactId>slf4j-api</artifactId>
97+
<version>2.0.13</version>
98+
</dependency>
99+
<dependency>
100+
<groupId>ch.qos.logback</groupId>
101+
<artifactId>logback-classic</artifactId>
102+
<version>1.5.6</version>
103+
</dependency>
104+
94105
</dependencies>
95106

96107
</project>

src/main/java/simplexity/Main.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
package simplexity;
22

33
import com.amazonaws.regions.Region;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
46
import simplexity.amazon.PollyHandler;
57
import simplexity.amazon.SpeechHandler;
68
import simplexity.commands.CommandManager;
79
import simplexity.commands.ExitCommand;
810
import simplexity.commands.HelpCommand;
911
import simplexity.commands.ReloadCommand;
1012
import simplexity.config.TTSConfig;
11-
import simplexity.httpserver.AuthServer;
1213
import simplexity.messages.Errors;
13-
import simplexity.messages.Output;
1414
import simplexity.twitch.TwitchClientHandler;
1515
import simplexity.twitch.TwitchSetup;
1616

1717
import java.util.Scanner;
1818

1919
public class Main {
20+
private static final Logger logger = LoggerFactory.getLogger(Main.class);
2021
private static CommandManager commandManager;
2122
private static PollyHandler pollyHandler;
2223
private static SpeechHandler speechHandler;
@@ -25,6 +26,7 @@ public class Main {
2526
public static Scanner scanner;
2627

2728
public static void main(String[] args) {
29+
logger.info("Starting application");
2830
scanner = new Scanner(System.in);
2931
commandManager = new CommandManager();
3032
registerCommands(commandManager);
@@ -36,13 +38,15 @@ public static void main(String[] args) {
3638
twitchSetup.setup();
3739
twitchClientHandler.setupClient();
3840
twitchClientHandler.getTwitchClient().getChat().joinChannel("RhythmWeHear");
41+
3942
while (true) {
4043
String input = scanner.nextLine();
4144
if (input.equals("--exit")) {
4245
return;
4346
}
4447
if (!commandManager.runCommand(input)) {
4548
twitchClientHandler.getTwitchClient().getChat().sendMessage("RhythmWeHear", input);
49+
4650
speechHandler.processSpeech(input);
4751
} else {
4852
System.out.println("command executed");
@@ -79,7 +83,6 @@ public static PollyHandler createPollyHandler() {
7983
}
8084

8185

82-
8386
public static PollyHandler getPollyHandler() {
8487
return pollyHandler;
8588
}

src/main/java/simplexity/amazon/SpeechHandler.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
import com.amazonaws.services.polly.model.VoiceId;
88
import javazoom.jl.player.FactoryRegistry;
99
import javazoom.jl.player.advanced.AdvancedPlayer;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
import org.slf4j.event.Level;
1013
import simplexity.Main;
1114
import simplexity.config.TTSConfig;
1215
import simplexity.messages.Errors;
16+
import simplexity.util.Util;
1317

1418
import java.io.InputStream;
1519

1620
public class SpeechHandler {
17-
21+
private static final Logger logger = LoggerFactory.getLogger(SpeechHandler.class);
1822
private VoiceId voiceId;
1923

2024
public SpeechHandler() {
@@ -33,7 +37,7 @@ public void processSpeech(String text) {
3337
speechStream = synthesizeSpeech(processedText, voiceId);
3438
}
3539
if (speechStream == null) {
36-
System.out.println(Errors.CAUGHT_EXCEPTION.replace("%error%", "Speech stream is null"));
40+
Util.logAndPrint(logger, Errors.CAUGHT_EXCEPTION.replace("%error%", "Speech stream is null"), Level.ERROR);
3741
return;
3842
}
3943
playSpeech(speechStream);
@@ -64,7 +68,7 @@ public InputStream synthesizeSSMLSpeech(String text, VoiceId voice) {
6468
SynthesizeSpeechResult synthesizeSpeechResult = Main.getPollyHandler().getPolly().synthesizeSpeech(synthesizeSpeechRequest);
6569
return synthesizeSpeechResult.getAudioStream();
6670
} catch (RuntimeException exception) {
67-
System.out.println(Errors.CAUGHT_EXCEPTION.replace("%error%", exception.getMessage()));
71+
Util.logAndPrint(logger, Errors.CAUGHT_EXCEPTION.replace("%error%", exception.getMessage()), Level.ERROR);
6872
return null;
6973
}
7074
}
@@ -84,7 +88,7 @@ public void playSpeech(InputStream speechStream) {
8488
player = new AdvancedPlayer(speechStream, FactoryRegistry.systemRegistry().createAudioDevice());
8589
player.play();
8690
} catch (Exception exception) {
87-
System.out.println(Errors.CAUGHT_EXCEPTION.replace("%error%", exception.getMessage()));
91+
Util.logAndPrint(logger, Errors.CAUGHT_EXCEPTION.replace("%error%", exception.getMessage()), Level.ERROR);
8892
}
8993
}
9094
}

src/main/java/simplexity/commands/HelpCommand.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
import simplexity.Main;
44
import simplexity.messages.Output;
5-
import simplexity.util.ConsoleColors;
6-
7-
import java.util.Arrays;
85

96
public class HelpCommand extends Command{
107
public HelpCommand(String name, String usage) {
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
package simplexity.commands;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.slf4j.event.Level;
36
import simplexity.config.TTSConfig;
47
import simplexity.messages.Output;
8+
import simplexity.util.Util;
59

610
public class ReloadCommand extends Command {
11+
private static final Logger logger = LoggerFactory.getLogger(ReloadCommand.class);
712
public ReloadCommand(String name, String usage) {
813
super(name, usage);
914
}
1015

1116
@Override
1217
public void execute() {
1318
TTSConfig.getInstance().reloadConfig();
14-
System.out.println(Output.RELOAD_MESSAGE);
19+
Util.logAndPrint(logger, Output.RELOAD_MESSAGE, Level.ERROR);
1520
}
1621
}

src/main/java/simplexity/config/ConfigDefaults.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public class ConfigDefaults {
66
public static final String AWS_SECRET_KEY = "aws-secret-key= \"\"\n";
77
public static final String TWITCH_CHANNEL = "twitch-channel= \"\"\n";
88
public static final String CONNECT_TO_TWITCH = "connect-to-twitch= false\n";
9+
public static final String TWITCH_APP_CLIENT_ID = "twitch-app-client-id= \"\"\n";
10+
public static final String TWITCH_APP_CLIENT_SECRET = "twitch-app-client-secret= \"\"\n";
911
public static final String REPLACE_TEXT = """
1012
replace-text {
1113
"**"= "<prosody volume=\\"x-loud\\" pitch=\\"low\\" rate=\\"slow\\">"
@@ -35,5 +37,4 @@ public class ConfigDefaults {
3537
"Bri:"= "Brian"
3638
}
3739
""";
38-
public static final String TWITCH_OAUTH = "twitch-oauth= %code%";
3940
}

src/main/java/simplexity/config/SimplexityFileHandler.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package simplexity.config;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.slf4j.event.Level;
36
import simplexity.messages.Errors;
7+
import simplexity.util.Util;
48

59
import java.io.File;
610
import java.io.FileWriter;
711

812
public class SimplexityFileHandler {
913

14+
private static final Logger logger = LoggerFactory.getLogger(SimplexityFileHandler.class);
1015
private SimplexityFileHandler(){}
1116
private static SimplexityFileHandler instance;
1217
public static SimplexityFileHandler getInstance(){
@@ -32,20 +37,22 @@ private void createConfigFile(){
3237
writer.write(ConfigDefaults.AWS_SECRET_KEY);
3338
writer.write(ConfigDefaults.CONNECT_TO_TWITCH);
3439
writer.write(ConfigDefaults.TWITCH_CHANNEL);
40+
writer.write(ConfigDefaults.TWITCH_APP_CLIENT_ID);
41+
writer.write(ConfigDefaults.TWITCH_APP_CLIENT_SECRET);
3542
writer.write(ConfigDefaults.REPLACE_TEXT);
3643
writer.write(ConfigDefaults.DEFAULT_VOICE);
3744
writer.write(ConfigDefaults.VOICE_PREFIXES);
38-
} catch (Exception e){
39-
System.out.println(Errors.CAUGHT_EXCEPTION.replace("%error%", e.getMessage()));
45+
} catch (Exception exception){
46+
Util.logAndPrint(logger, Errors.CAUGHT_EXCEPTION.replace("%error%", exception.getMessage()), Level.ERROR);
4047
}
4148
}
4249

4350
public static void createTwitchFile(String accessToken){
4451
File file = new File("twitch-oauth.conf");
4552
try (FileWriter writer = new FileWriter(file)) {
4653
writer.write("twitch-oauth=\"" + accessToken + "\"");
47-
} catch (Exception e){
48-
System.out.println(Errors.CAUGHT_EXCEPTION.replace("%error%", e.getMessage()));
54+
} catch (Exception exception){
55+
Util.logAndPrint(logger, Errors.CAUGHT_EXCEPTION.replace("%error%", exception.getMessage()), Level.ERROR);
4956
}
5057
}
5158
}

src/main/java/simplexity/config/TTSConfig.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@
55
import com.amazonaws.services.polly.model.VoiceId;
66
import com.typesafe.config.Config;
77
import com.typesafe.config.ConfigFactory;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
import org.slf4j.event.Level;
811
import simplexity.messages.Errors;
12+
import simplexity.util.Util;
913

1014
import java.io.File;
1115
import java.util.HashMap;
1216

1317
public class TTSConfig {
1418

19+
private static final Logger logger = LoggerFactory.getLogger(TTSConfig.class);
1520
private final HashMap<String, String> replaceText = new HashMap<>();
1621
private final HashMap<String, VoiceId> voicePrefixes = new HashMap<>();
1722
private Region awsRegion;
1823
private VoiceId defaultVoice;
19-
private String awsAccessID, awsSecretKey, twitchChannel;
24+
private String awsAccessID, awsSecretKey, twitchChannel, twitchAppClientId, twitchAppClientSecret;
2025
private boolean connectToTwitch;
2126
private String twitchOAuth;
2227
private TTSConfig(){}
@@ -86,7 +91,7 @@ private void reloadVoicePrefixes(Config config) {
8691
VoiceId voiceId = VoiceId.fromValue(String.valueOf(entry.getValue().unwrapped()));
8792
voicePrefixes.put(entry.getKey().replace("\"", ""), voiceId);
8893
} catch (IllegalArgumentException e) {
89-
System.out.println(Errors.INVALID_VOICE.replace("%voice%", entry.getValue().unwrapped().toString()));
94+
Util.logAndPrint(logger, Errors.INVALID_VOICE.replace("%voice%", entry.getValue().unwrapped().toString()), Level.ERROR);
9095
}
9196
});
9297
}
@@ -96,7 +101,7 @@ private void reloadRegion(Config config) {
96101
try {
97102
awsRegion = Region.getRegion(Regions.valueOf(region));
98103
} catch (IllegalArgumentException e) {
99-
System.out.println(Errors.INVALID_REGION.replace("%region%", region));
104+
Util.logAndPrint(logger, Errors.INVALID_REGION.replace("%region%", region), Level.ERROR);
100105
awsRegion = Region.getRegion(Regions.US_EAST_1);
101106
}
102107
}
@@ -106,7 +111,7 @@ private void reloadDefaultVoice(Config config){
106111
try {
107112
defaultVoice = VoiceId.fromValue(voiceString);
108113
} catch (IllegalArgumentException e) {
109-
System.out.println(Errors.INVALID_DEFAULT_VOICE.replace("%voice%", voiceString));
114+
Util.logAndPrint(logger, Errors.INVALID_DEFAULT_VOICE.replace("%voice%", voiceString), Level.ERROR);
110115
defaultVoice = VoiceId.Brian;
111116
}
112117
}
@@ -115,6 +120,9 @@ private void reloadStrings(Config config){
115120
awsAccessID = config.getString("aws-access-id");
116121
awsSecretKey = config.getString("aws-secret-key");
117122
twitchChannel = config.getString("twitch-channel");
123+
twitchAppClientId = config.getString("twitch-app-client-id");
124+
twitchAppClientSecret = config.getString("twitch-app-client-secret");
125+
118126
}
119127

120128
private void reloadBooleans(Config config){
@@ -140,4 +148,12 @@ public boolean isConnectToTwitch() {
140148
public String getTwitchOAuth() {
141149
return twitchOAuth;
142150
}
151+
152+
public String getTwitchAppClientId() {
153+
return twitchAppClientId;
154+
}
155+
156+
public String getTwitchAppClientSecret() {
157+
return twitchAppClientSecret;
158+
}
143159
}

src/main/java/simplexity/httpserver/AuthHandler.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
import com.sun.net.httpserver.HttpHandler;
55
import org.json.JSONException;
66
import org.json.JSONObject;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import org.slf4j.event.Level;
710
import simplexity.config.SimplexityFileHandler;
11+
import simplexity.config.TTSConfig;
812
import simplexity.messages.Errors;
13+
import simplexity.util.Util;
914

1015
import java.io.BufferedReader;
1116
import java.io.IOException;
@@ -15,6 +20,7 @@
1520
import java.net.URL;
1621

1722
public class AuthHandler implements HttpHandler {
23+
private static final Logger logger = LoggerFactory.getLogger(AuthHandler.class);
1824
@Override
1925
public void handle(HttpExchange exchange) {
2026
try {
@@ -29,11 +35,20 @@ public void handle(HttpExchange exchange) {
2935

3036
if (!twitchCode.isEmpty()) {
3137
// Exchange the authorization code for an access token
32-
String clientId = "";
33-
String clientSecret = "";
38+
String clientId = TTSConfig.getInstance().getTwitchAppClientId();
39+
String clientSecret = TTSConfig.getInstance().getTwitchAppClientSecret();
3440
String redirectUri = "http://localhost:3000";
3541
String tokenUrl = "https://id.twitch.tv/oauth2/token";
3642

43+
if (clientId == null || clientSecret == null || clientId.isEmpty() || clientSecret.isEmpty()) {
44+
String errorResponse = "<h1>Twitch app credentials not found!</h1>";
45+
exchange.sendResponseHeaders(500, errorResponse.length());
46+
OutputStream os = exchange.getResponseBody();
47+
os.write(errorResponse.getBytes());
48+
os.close();
49+
return;
50+
}
51+
3752
URL url = new URL(tokenUrl);
3853
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
3954
connection.setRequestMethod("POST");
@@ -85,7 +100,7 @@ public void handle(HttpExchange exchange) {
85100
os.close();
86101
}
87102
} catch (IOException | JSONException e) {
88-
System.out.println(Errors.CAUGHT_EXCEPTION.replace("%error%", e.getMessage()));
103+
Util.logAndPrint(logger, Errors.CAUGHT_EXCEPTION.replace("%error%", e.getMessage()), Level.TRACE);
89104
}
90105
}
91106
}

src/main/java/simplexity/httpserver/AuthServer.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
package simplexity.httpserver;
22

33
import com.sun.net.httpserver.HttpServer;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.slf4j.event.Level;
47
import simplexity.messages.Errors;
8+
import simplexity.util.Util;
59

610
import java.io.IOException;
711
import java.net.InetSocketAddress;
812

913
public class AuthServer {
14+
private static final Logger logger = LoggerFactory.getLogger(AuthServer.class);
1015
public static HttpServer server;
1116

1217
public static void run() {
1318
try {
1419
setupServer();
1520
} catch (Exception exception) {
16-
System.out.println(Errors.CAUGHT_EXCEPTION.replace("%error%", exception.getMessage()));
21+
Util.logAndPrint(logger, Errors.CAUGHT_EXCEPTION.replace("%error%", exception.getMessage()), Level.TRACE);
1722
}
1823
}
1924

@@ -28,7 +33,7 @@ private static void setupServer() {
2833
server.setExecutor(null);
2934
server.start();
3035
} catch (IOException exception) {
31-
System.out.println(Errors.CAUGHT_EXCEPTION.replace("%error%", exception.getMessage()));
36+
Util.logAndPrint(logger, Errors.CAUGHT_EXCEPTION.replace("%error%", exception.getMessage()), Level.TRACE);
3237
}
3338
}
3439

0 commit comments

Comments
 (0)