Search in sources :

Example 1 with LiveChatMessageSnippet

use of com.google.api.services.youtube.model.LiveChatMessageSnippet in project api-samples by youtube.

the class InsertLiveChatMessage method main.

/**
 * Inserts a message into a live broadcast.
 *
 * @param args The message to insert (required) followed by the videoId (optional).
 * If the videoId is given, live chat messages will be retrieved from the chat associated with
 * this video. If the videoId is not specified, the signed in user's current live broadcast will
 * be used instead.
 */
public static void main(String[] args) {
    // Get the chat message to insert
    if (args.length == 0) {
        System.err.println("No message specified");
        System.exit(1);
    }
    String message = args[0];
    // This OAuth 2.0 access scope allows for write access to the authenticated user's account.
    List<String> scopes = Lists.newArrayList(YouTubeScopes.YOUTUBE_FORCE_SSL);
    try {
        // Authorize the request.
        Credential credential = Auth.authorize(scopes, "insertlivechatmessage");
        // This object is used to make YouTube Data API requests.
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-insertchatmessage-sample").build();
        // Get the liveChatId
        String liveChatId = args.length == 2 ? GetLiveChatId.getLiveChatId(youtube, args[1]) : GetLiveChatId.getLiveChatId(youtube);
        if (liveChatId != null) {
            System.out.println("Live chat id: " + liveChatId);
        } else {
            System.err.println("Unable to find a live chat id");
            System.exit(1);
        }
        // Insert the message into live chat
        LiveChatMessage liveChatMessage = new LiveChatMessage();
        LiveChatMessageSnippet snippet = new LiveChatMessageSnippet();
        snippet.setType("textMessageEvent");
        snippet.setLiveChatId(liveChatId);
        LiveChatTextMessageDetails details = new LiveChatTextMessageDetails();
        details.setMessageText(message);
        snippet.setTextMessageDetails(details);
        liveChatMessage.setSnippet(snippet);
        YouTube.LiveChatMessages.Insert liveChatInsert = youtube.liveChatMessages().insert("snippet", liveChatMessage);
        LiveChatMessage response = liveChatInsert.execute();
        System.out.println("Inserted message id " + response.getId());
    } catch (GoogleJsonResponseException e) {
        System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("IOException: " + e.getMessage());
        e.printStackTrace();
    } catch (Throwable t) {
        System.err.println("Throwable: " + t.getMessage());
        t.printStackTrace();
    }
}
Also used : LiveChatTextMessageDetails(com.google.api.services.youtube.model.LiveChatTextMessageDetails) Credential(com.google.api.client.auth.oauth2.Credential) LiveChatMessage(com.google.api.services.youtube.model.LiveChatMessage) LiveChatMessageSnippet(com.google.api.services.youtube.model.LiveChatMessageSnippet) IOException(java.io.IOException) YouTube(com.google.api.services.youtube.YouTube) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException)

Example 2 with LiveChatMessageSnippet

use of com.google.api.services.youtube.model.LiveChatMessageSnippet in project api-samples by youtube.

the class ListLiveChatMessages method listChatMessages.

/**
 * Lists live chat messages, polling at the server supplied interval. Owners and moderators of a
 * live chat will poll at a faster rate.
 *
 * @param liveChatId The live chat id to list messages from.
 * @param nextPageToken The page token from the previous request, if any.
 * @param delayMs The delay in milliseconds before making the request.
 */
private static void listChatMessages(final String liveChatId, final String nextPageToken, long delayMs) {
    System.out.println(String.format("Getting chat messages in %1$.3f seconds...", delayMs * 0.001));
    Timer pollTimer = new Timer();
    pollTimer.schedule(new TimerTask() {

        @Override
        public void run() {
            try {
                // Get chat messages from YouTube
                LiveChatMessageListResponse response = youtube.liveChatMessages().list(liveChatId, "snippet, authorDetails").setPageToken(nextPageToken).setFields(LIVE_CHAT_FIELDS).execute();
                // Display messages and super chat details
                List<LiveChatMessage> messages = response.getItems();
                for (int i = 0; i < messages.size(); i++) {
                    LiveChatMessage message = messages.get(i);
                    LiveChatMessageSnippet snippet = message.getSnippet();
                    System.out.println(buildOutput(snippet.getDisplayMessage(), message.getAuthorDetails(), snippet.getSuperChatDetails()));
                }
                // Request the next page of messages
                listChatMessages(liveChatId, response.getNextPageToken(), response.getPollingIntervalMillis());
            } catch (Throwable t) {
                System.err.println("Throwable: " + t.getMessage());
                t.printStackTrace();
            }
        }
    }, delayMs);
}
Also used : LiveChatMessage(com.google.api.services.youtube.model.LiveChatMessage) Timer(java.util.Timer) TimerTask(java.util.TimerTask) LiveChatMessageSnippet(com.google.api.services.youtube.model.LiveChatMessageSnippet) ArrayList(java.util.ArrayList) List(java.util.List) LiveChatMessageListResponse(com.google.api.services.youtube.model.LiveChatMessageListResponse)

Aggregations

LiveChatMessage (com.google.api.services.youtube.model.LiveChatMessage)2 LiveChatMessageSnippet (com.google.api.services.youtube.model.LiveChatMessageSnippet)2 Credential (com.google.api.client.auth.oauth2.Credential)1 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 YouTube (com.google.api.services.youtube.YouTube)1 LiveChatMessageListResponse (com.google.api.services.youtube.model.LiveChatMessageListResponse)1 LiveChatTextMessageDetails (com.google.api.services.youtube.model.LiveChatTextMessageDetails)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1