Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(helix): add send chat message endpoint #921

Merged
merged 1 commit into from Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -61,6 +61,7 @@ public enum TwitchScopes {
HELIX_USER_EDIT_FOLLOWS("user:edit:follows"),
HELIX_USER_BLOCKS_READ("user:read:blocked_users"),
HELIX_USER_READ_BROADCAST("user:read:broadcast"),
HELIX_USER_CHAT_WRITE("user:write:chat"),
HELIX_USER_FOLLOWS_READ("user:read:follows"),
HELIX_USER_SUBSCRIPTIONS_READ("user:read:subscriptions"),
HELIX_USER_EMAIL("user:read:email"),
Expand Down
Expand Up @@ -540,6 +540,29 @@ HystrixCommand<EmoteList> getEmoteSets(
@Param("emote_set_id") Collection<String> ids
);

/**
* Sends a message to the broadcaster’s chat room.
* <p>
* If an app access token is used, the chatting user must have authorized to the Client ID with the user:bot scope,
* and the broadcaster or a moderator must have authorized to the Client ID with the channel:bot scope.
*
* @param authToken User access token (scope: user:write:chat) or App access token (with Client ID authorized).
* @param message The message to be sent, including the source and target user IDs.
* @return metadata about the send message attempt
* @see com.github.twitch4j.auth.domain.TwitchScopes#HELIX_USER_CHAT_WRITE
* @see com.github.twitch4j.auth.domain.TwitchScopes#CHAT_CHANNEL_BOT
* @see com.github.twitch4j.auth.domain.TwitchScopes#CHAT_USER_BOT
*/
@RequestLine("POST /chat/messages")
@Headers({
"Authorization: Bearer {token}",
"Content-Type: application/json"
})
HystrixCommand<SentChatMessageWrapper> sendChatMessage(
@Param("token") String authToken,
ChatMessage message
);

/**
* Sends a Shoutout to the specified broadcaster.
* <p>
Expand Down
@@ -0,0 +1,23 @@
package com.github.twitch4j.helix.domain;

import lombok.AccessLevel;
import lombok.Data;
import lombok.Setter;

@Data
@Setter(AccessLevel.PRIVATE)
public class ChatDropReason {

/**
* Code for why the message was dropped.
* <p>
* For example: "channel_settings", "msg_duplicate", "msg_rejected".
*/
private String code;

/**
* Message for why the message was dropped.
*/
private String message;

}
@@ -0,0 +1,53 @@
package com.github.twitch4j.helix.domain;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.With;
import lombok.extern.jackson.Jacksonized;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@With
@Data
@Setter(AccessLevel.PRIVATE)
@Builder(toBuilder = true)
@Jacksonized
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class ChatMessage {

/**
* The ID of the broadcaster whose chat room the message will be sent to.
*/
@NotNull
String broadcasterId;

/**
* The ID of the user sending the message.
* <p>
* This ID must match the user ID in the user access token (if not using an app access token).
*/
@NotNull
String senderId;

/**
* The message to send.
* <p>
* The message is limited to a maximum of 500 characters.
*/
@NotNull
String message;

/**
* The ID of the chat message being replied to, if any.
*/
@Nullable
String replyParentMessageId;

}
@@ -0,0 +1,30 @@
package com.github.twitch4j.helix.domain;

import lombok.AccessLevel;
import lombok.Data;
import lombok.Setter;
import org.jetbrains.annotations.Nullable;

@Data
@Setter(AccessLevel.PRIVATE)
public class SentChatMessage {

/**
* The message id for the message that was sent.
* <p>
* Can be an empty string when {@link #isSent()} is false.
*/
private String messageId;

/**
* Whether the message passed all checks and was sent.
*/
private boolean isSent;

/**
* The reason the message was dropped, if any.
*/
@Nullable
private ChatDropReason dropReason;

}
@@ -0,0 +1,26 @@
package com.github.twitch4j.helix.domain;

import lombok.AccessLevel;
import lombok.Data;
import lombok.Setter;

import java.util.List;

@Data
@Setter(AccessLevel.PRIVATE)
public class SentChatMessageWrapper {

/**
* Contains a single object with metadata about the sent message.
*/
private List<SentChatMessage> data;

/**
* @return metadata about the sent message
*/
public SentChatMessage get() {
if (data == null || data.isEmpty()) return null; // should never happen
return data.get(0);
}

}