From 4a5c1ed44950425397a3689453f233b70a9cdb66 Mon Sep 17 00:00:00 2001 From: nzBryce101 Date: Tue, 2 Apr 2024 18:20:24 +1300 Subject: [PATCH] start of YT chatbot --- src/MCchatbot.java | 91 +++++++++++++++++++ ...6t72m9h4dj.apps.googleusercontent.com.json | 1 + 2 files changed, 92 insertions(+) create mode 100644 src/MCchatbot.java create mode 100644 src/main/resources/client_secret_788006318148-mkmrd4c8fcrve7atovf34v6t72m9h4dj.apps.googleusercontent.com.json diff --git a/src/MCchatbot.java b/src/MCchatbot.java new file mode 100644 index 0000000..8bc5d6b --- /dev/null +++ b/src/MCchatbot.java @@ -0,0 +1,91 @@ +import com.google.api.client.googleapis.auth.oauth2.Credential; +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.googleapis.http.HttpRequest; +import com.google.api.client.googleapis.http.HttpRequestInitializer; +import com.google.api.client.googleapis.json.JsonFactory; +import com.google.api.client.goolgeapis.json.jackson2.JacksonFactory; +import com.google.api.services.youtube.YouTube; +import com.google.api.services.youtube.model.LiveChatMessage; +import com.google.api.services.youtube.model.LiveChatMessageListResponse; +import com.google.api.services.youtube.model.LiveChatMessageSnippet; +import com.google.api.services.youtube.model.LiveChatTextMessageDetails; +import com.google.api.services.youtube.model.LiveChatTextMessageDetails.MessageTextDetails; + +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.List; + +public class MCchatbot { + + private static final String APPLICATION_NAME = "MCchatbot"; + private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); + private static YouTube youtube; + + public static void main(String[] args) throws GeneralSecurityException, IOException { + // Set up credentials + Credential credential = authorize(); + + // Set up YouTube API client + youtube = new YouTube.Builder( + GoogleNetHttpTransport.newTrustedTransport(), + JSON_FACTORY, + new HttpRequestInitializer() { + @Override + public void initialize(HttpRequest request) throws IOException { + request.setInterceptor(credential); + } + }) + .setApplicationName(APPLICATION_NAME) + .build(); + + // Get live chat ID for your live stream + String liveChatId = getLiveChatId("5lAKSeFQS2M"); + + + // Poll for new chat messages every few seconds + while (true) { + List messages = getLiveChatMessages(liveChatId); + for (LiveChatMessage message : messages) { + String author = message.getSnippet().getAuthorChannelId(); + String text = message.getSnippet().getDisplayMessage(); + System.out.println("New message from " + author + ": " + text); + // Add your logic to handle incoming messages here + } + try { + Thread.sleep(5000); // Poll every 5 seconds + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + private static Credential authorize() throws IOException, GeneralSecurityException { + // Load client secrets JSON file (you need to create this file with your OAuth client ID and secret) + GoogleCredential credential = GoogleCredential.fromStream( + YouTubeLiveChatBot.class.getResourceAsStream("/client_secrets.json")) + .createScoped(List.of("https://www.googleapis.com/auth/youtube")); + + return credential; + } + + private static String getLiveChatId(String liveStreamId) throws IOException { + YouTube.LiveChatMessages.List liveChatRequest = youtube.liveChatMessages() + .list("id,snippet") + .setLiveChatId(liveStreamId); + LiveChatMessageListResponse response = liveChatRequest.execute(); + List messages = response.getItems(); + if (messages != null && !messages.isEmpty()) { + return messages.get(0).getSnippet().getLiveChatId(); + } + return null; + } + + private static List getLiveChatMessages(String liveChatId) throws IOException { + YouTube.LiveChatMessages.List liveChatRequest = youtube.liveChatMessages() + .list("id,snippet") + .setLiveChatId(liveChatId); + LiveChatMessageListResponse response = liveChatRequest.execute(); + return response.getItems(); + } +} diff --git a/src/main/resources/client_secret_788006318148-mkmrd4c8fcrve7atovf34v6t72m9h4dj.apps.googleusercontent.com.json b/src/main/resources/client_secret_788006318148-mkmrd4c8fcrve7atovf34v6t72m9h4dj.apps.googleusercontent.com.json new file mode 100644 index 0000000..8375e17 --- /dev/null +++ b/src/main/resources/client_secret_788006318148-mkmrd4c8fcrve7atovf34v6t72m9h4dj.apps.googleusercontent.com.json @@ -0,0 +1 @@ +{"web":{"client_id":"788006318148-mkmrd4c8fcrve7atovf34v6t72m9h4dj.apps.googleusercontent.com","project_id":"flash-bloom-419102","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"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"}} \ No newline at end of file