Skip to content

Commit 3699d2c

Browse files
committed
twitch integration still does not work
1 parent 3171ec0 commit 3699d2c

6 files changed

Lines changed: 61 additions & 16 deletions

File tree

TTSJava/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@
6262
<artifactId>config</artifactId>
6363
<version>1.4.3</version>
6464
</dependency>
65+
<dependency>
66+
<groupId>com.github.twitch4j</groupId>
67+
<artifactId>twitch4j</artifactId>
68+
<version>1.20.0</version>
69+
</dependency>
6570

6671
</dependencies>
6772

TTSJava/src/main/java/simplexity/clitts/SpeechHandling.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
public class SpeechHandling {
1919

20-
static Region AWS_REGION;
20+
static Region awsRegion;
2121
static AmazonPolly polly;
22-
static VoiceId VOICE_ID;
22+
static VoiceId voiceId;
2323

2424
private SpeechHandling() {
2525
polly = new AmazonPollyClient(new BasicAWSCredentials(TTSConfig.getInstance().getAccessID(), TTSConfig.getInstance().getAccessSecret()), new ClientConfiguration());
26-
polly.setRegion(AWS_REGION);
26+
polly.setRegion(awsRegion);
2727
}
2828

2929
private static SpeechHandling instance;
@@ -34,15 +34,14 @@ public static SpeechHandling getInstance() {
3434
}
3535

3636
public void processSpeech(String text) {
37-
TextToSpeech tts = TextToSpeech.getInstance();
3837
String newText = replaceText(text);
3938
boolean useSSML = !text.equals(newText);
4039
try {
4140
InputStream speechStream;
4241
if (!useSSML) {
43-
speechStream = synthesizeSpeech(polly, newText, VOICE_ID);
42+
speechStream = synthesizeSpeech(polly, newText, voiceId);
4443
} else {
45-
speechStream = synthesizeSSMLSpeech(polly, newText, VOICE_ID);
44+
speechStream = synthesizeSSMLSpeech(polly, newText, voiceId);
4645
}
4746
if (speechStream == null) {
4847
System.out.println(Colors.boldRed + "Error: Speech stream is null." + Colors.formatReset);
@@ -63,7 +62,7 @@ public String replaceText(String text) {
6362
for (String key : TTSConfig.getInstance().getVoicePrefixes().keySet()) {
6463
if (text.startsWith(key)) {
6564
text = text.replace(key, "");
66-
SpeechHandling.VOICE_ID = TTSConfig.getInstance().getVoicePrefixes().get(key);
65+
SpeechHandling.voiceId = TTSConfig.getInstance().getVoicePrefixes().get(key);
6766
}
6867
}
6968
return text;

TTSJava/src/main/java/simplexity/clitts/TTSConfig.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.amazonaws.services.polly.model.VoiceId;
66
import com.typesafe.config.Config;
77
import com.typesafe.config.ConfigFactory;
8+
import com.typesafe.config.ConfigRenderOptions;
89
import com.typesafe.config.ConfigValue;
910
import com.typesafe.config.ConfigValueFactory;
1011

@@ -20,6 +21,7 @@ public class TTSConfig {
2021
private Region AWS_REGION;
2122
private VoiceId defaultVoice;
2223
private boolean useTwitch = false;
24+
private Config config;
2325
private String accessID, accessSecret, twitchChannel, twitchAuthCode;
2426

2527
private TTSConfig() {
@@ -33,7 +35,7 @@ public static TTSConfig getInstance() {
3335
}
3436

3537
public void reloadConfig() {
36-
Config config = loadConfig();
38+
config = loadConfig();
3739
reloadReplaceText(config);
3840
reloadVoicePrefixes(config);
3941
reloadRegion(config);
@@ -157,6 +159,17 @@ private void reloadTwitchChannel(Config config) {
157159
}
158160
}
159161

162+
public void updateConfigValue(String key, String value) {
163+
config = config.withValue(key, ConfigFactory.parseString(key + "=\"" + value + "\"").root());
164+
try (FileWriter writer = new FileWriter("application.conf")) {
165+
writer.write(config.root().render(ConfigRenderOptions.defaults()));
166+
}
167+
catch (IOException e) {
168+
System.out.println("Error updating config file.");
169+
e.printStackTrace();
170+
}
171+
}
172+
160173
public String getTwitchChannel() {
161174
return twitchChannel;
162175
}
@@ -199,6 +212,4 @@ public void setTwitchAuthCode(String authCode) {
199212
ConfigValue twitchCode = ConfigValueFactory.fromAnyRef(authCode);
200213
config.withValue("twitch-auth-code", twitchCode);
201214
}
202-
203-
204215
}

TTSJava/src/main/java/simplexity/clitts/TextToSpeech.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package simplexity.clitts;
22

33
import simplexity.clitts.twitch.TwitchAuthServer;
4+
import simplexity.clitts.twitch.TwitchMessaging;
45
import simplexity.clitts.util.Messages;
56

67
import java.io.IOException;
@@ -23,8 +24,8 @@ public static TextToSpeech getInstance() {
2324
public static void main(String[] args) {
2425
TTSConfig.getInstance().reloadConfig();
2526
System.out.println(Messages.startupMessage);
26-
SpeechHandling.VOICE_ID = TTSConfig.getInstance().getDefaultVoice();
27-
SpeechHandling.AWS_REGION = TTSConfig.getInstance().getRegion();
27+
SpeechHandling.voiceId = TTSConfig.getInstance().getDefaultVoice();
28+
SpeechHandling.awsRegion = TTSConfig.getInstance().getRegion();
2829
checkTwitch();
2930
while (runProgram) {
3031
System.out.println(Messages.enterText);
@@ -37,6 +38,7 @@ public static void main(String[] args) {
3738
case ("--help") -> System.out.println(Messages.helpMessage);
3839
case ("--reload") -> {
3940
TTSConfig.getInstance().reloadConfig();
41+
checkTwitch();
4042
System.out.println(Messages.reloadMessage);
4143
}
4244
default -> {
@@ -66,15 +68,15 @@ private static void checkTwitch() {
6668
case ("y") -> {
6769
validInput = true;
6870
checkTwitchAuth();
69-
7071
}
7172
case ("n") -> {
7273
validInput = true;
7374
}
7475
default -> System.out.println(Messages.stayConnectedError);
7576
}
76-
7777
}
78+
if (TTSConfig.getInstance().getTwitchAuthCode() == null) return;
79+
TwitchMessaging.getInstance().setupTwitch(TTSConfig.getInstance().getTwitchAuthCode());
7880
}
7981
}
8082

TTSJava/src/main/java/simplexity/clitts/twitch/TwitchAuthServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ static class AuthHandler implements HttpHandler {
2828
public void handle(HttpExchange t) throws IOException {
2929
String twitchCode = t.getRequestURI().getQuery().split("=")[1];
3030
twitchCode = twitchCode.split("&")[0];
31-
TTSConfig.getInstance().setTwitchAuthCode(twitchCode);
32-
31+
TTSConfig.getInstance().updateConfigValue("twitch-o-auth-code", twitchCode);
32+
TTSConfig.getInstance().reloadConfig();
3333
// Respond to the client
3434
String response = "Twitch authentication successful!";
3535
t.sendResponseHeaders(200, response.length());
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package simplexity.clitts.twitch;
2+
3+
import com.github.philippheuer.credentialmanager.domain.OAuth2Credential;
4+
import com.github.twitch4j.TwitchClient;
5+
import com.github.twitch4j.TwitchClientBuilder;
6+
import simplexity.clitts.TTSConfig;
7+
8+
public class TwitchMessaging {
9+
private TwitchMessaging(){}
10+
private static TwitchMessaging instance;
11+
public static TwitchMessaging getInstance(){
12+
if(instance == null) instance = new TwitchMessaging();
13+
return instance;
14+
}
15+
OAuth2Credential credential;
16+
TwitchClient twitchClient;
17+
18+
public void setupTwitch(String authorization){
19+
credential = new OAuth2Credential("twitch", authorization);
20+
twitchClient = TwitchClientBuilder.builder()
21+
.withChatAccount(credential)
22+
.withEnableChat(true)
23+
.build();
24+
twitchClient.getChat().joinChannel(TTSConfig.getInstance().getTwitchChannel());
25+
twitchClient.getChat().sendMessage(TTSConfig.getInstance().getTwitchChannel(), "Hello world!");
26+
}
27+
28+
}

0 commit comments

Comments
 (0)