Skip to content

Commit

Permalink
feat: implement post DM V2 endpoints (#420)
Browse files Browse the repository at this point in the history
* feat_impl_post_dm_endpoints

* adding constructor

* improving signatures
  • Loading branch information
redouane59 authored Oct 29, 2022
1 parent 703e6c9 commit b8bf00f
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 62 deletions.
40 changes: 40 additions & 0 deletions src/main/java/io/github/redouane59/twitter/ITwitterClientV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.github.scribejava.core.model.Response;
import io.github.redouane59.twitter.dto.dm.DirectMessage;
import io.github.redouane59.twitter.dto.dm.DmParameters;
import io.github.redouane59.twitter.dto.dm.DmParameters.DmMessage;
import io.github.redouane59.twitter.dto.dm.PostDmResponse;
import io.github.redouane59.twitter.dto.endpoints.AdditionalParameters;
import io.github.redouane59.twitter.dto.list.TwitterList;
import io.github.redouane59.twitter.dto.list.TwitterListList;
Expand Down Expand Up @@ -687,4 +690,41 @@ public interface ITwitterClientV2 {
* https://api.twitter.com/2/dm_conversations/with/:participant_id/dm_events. Messages are returned in reverse chronological order.
*/
DirectMessage getDirectMessagesByUser(String participantId, AdditionalParameters additionalParameters);

/**
* Creates a Direct Message on behalf of an authenticated user, and adds it to the specified conversation calling
* https://api.twitter.com/2/dm_conversations/:dm_conversation_id/messages.
*/
PostDmResponse createDirectMessage(String conversationId, String text);

/**
* Creates a Direct Message on behalf of an authenticated user, and adds it to the specified conversation calling
* https://api.twitter.com/2/dm_conversations/:dm_conversation_id/messages.
*/
PostDmResponse createDirectMessage(String conversationId, DmMessage message);

/**
* Creates a new group conversation and adds a Direct Message to it on behalf of an authenticated user calling
* https://api.twitter.com/2/dm_conversations
*/
PostDmResponse createGroupDmConversation(DmParameters parameters);

/**
* Creates a new group conversation and adds a Direct Message to it on behalf of an authenticated user calling
* https://api.twitter.com/2/dm_conversations
*/
PostDmResponse createGroupDmConversation(List<String> participantIds, String text);

/**
* Creates a one-to-one Direct Message and adds it to the one-to-one conversation. This method either creates a new one-to-one conversation or
* retrieves the current conversation and adds the Direct Message to it calling https://api.twitter.com/2/dm_conversations/with/:participant_id/messages.
*/
PostDmResponse createUserDmConversation(String participantId, String text);

/**
* Creates a one-to-one Direct Message and adds it to the one-to-one conversation. This method either creates a new one-to-one conversation or
* retrieves the current conversation and adds the Direct Message to it calling https://api.twitter.com/2/dm_conversations/with/:participant_id/messages.
*/
PostDmResponse createUserDmConversation(String participantId, DmMessage message);

}
75 changes: 68 additions & 7 deletions src/main/java/io/github/redouane59/twitter/TwitterClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import io.github.redouane59.twitter.dto.collections.CollectionsResponse;
import io.github.redouane59.twitter.dto.collections.TimeLineOrder;
import io.github.redouane59.twitter.dto.dm.DirectMessage;
import io.github.redouane59.twitter.dto.dm.deprecatedV1.DmEvent;
import io.github.redouane59.twitter.dto.dm.DmParameters;
import io.github.redouane59.twitter.dto.dm.DmParameters.DmMessage;
import io.github.redouane59.twitter.dto.dm.PostDmResponse;
import io.github.redouane59.twitter.dto.dm.deprecatedV1.DmListAnswer;
import io.github.redouane59.twitter.dto.endpoints.AdditionalParameters;
import io.github.redouane59.twitter.dto.getrelationship.IdList;
Expand Down Expand Up @@ -861,6 +863,59 @@ public DirectMessage getDirectMessagesByUser(final String participantId, final A

}

@Override
public PostDmResponse createDirectMessage(final String conversationId, String text) {
return createDirectMessage(conversationId, DmMessage.builder().text(text).build());
}

@Override
public PostDmResponse createDirectMessage(final String conversationId, DmMessage message) {
String url = getUrlHelper().getPostConversationDmUrl(conversationId);
String body;
try {
body = JsonHelper.toJson(message);
} catch (JsonProcessingException e) {
LOGGER.error(e.getMessage(), e);
throw new IllegalArgumentException();
}
return getRequestHelperV1().postRequestWithBodyJson(url, null, body, PostDmResponse.class).orElseThrow(NoSuchElementException::new);
}

public PostDmResponse createGroupDmConversation(List<String> participantIds, String text) {
return createGroupDmConversation(DmParameters.builder()
.participantIds(participantIds)
.message(DmMessage.builder().text(text).build())
.build());
}

public PostDmResponse createGroupDmConversation(DmParameters parameters) {
String url = getUrlHelper().getCreateDmConversationUrl();
String body;
try {
body = JsonHelper.toJson(parameters);
} catch (JsonProcessingException e) {
LOGGER.error(e.getMessage(), e);
throw new IllegalArgumentException();
}
return getRequestHelperV1().postRequestWithBodyJson(url, null, body, PostDmResponse.class).orElseThrow(NoSuchElementException::new);
}

public PostDmResponse createUserDmConversation(String participantId, String text) {
return createUserDmConversation(participantId, DmMessage.builder().text(text).build());
}

public PostDmResponse createUserDmConversation(String participantId, DmMessage message) {
String url = getUrlHelper().getPostUserDmUrl(participantId);
String body;
try {
body = JsonHelper.toJson(message);
} catch (JsonProcessingException e) {
LOGGER.error(e.getMessage(), e);
throw new IllegalArgumentException();
}
return getRequestHelperV1().postRequestWithBodyJson(url, null, body, PostDmResponse.class).orElseThrow(NoSuchElementException::new);
}


@Override
public Tweet getTweet(String tweetId) {
Expand Down Expand Up @@ -1414,18 +1469,24 @@ public List<io.github.redouane59.twitter.dto.dm.deprecatedV1.DirectMessage> getD

@Override
public io.github.redouane59.twitter.dto.dm.deprecatedV1.DirectMessage getDm(String dmId) {
String url = urlHelper.getDmUrl(dmId);
DmEvent result = getRequestHelper().getRequest(url, DmEvent.class).orElseThrow(NoSuchElementException::new);
String url = urlHelper.getDmUrl(dmId);
io.github.redouane59.twitter.dto.dm.deprecatedV1.DmEvent
result =
getRequestHelper().getRequest(url, io.github.redouane59.twitter.dto.dm.deprecatedV1.DmEvent.class).orElseThrow(NoSuchElementException::new);
return result.getEvent();
}

@Override
public DmEvent postDm(final String text, final String userId) {
String url = urlHelper.getPostDmUrl();
public io.github.redouane59.twitter.dto.dm.deprecatedV1.DmEvent postDm(final String text, final String userId) {
String url = urlHelper.getPostConversationDmUrl();
try {
String body = JsonHelper.toJson(
DmEvent.builder().event(new io.github.redouane59.twitter.dto.dm.deprecatedV1.DirectMessage(text, userId)).build());
return getRequestHelperV1().postRequestWithBodyJson(url, null, body, DmEvent.class).orElseThrow(NoSuchElementException::new);
io.github.redouane59.twitter.dto.dm.deprecatedV1.DmEvent.builder()
.event(new io.github.redouane59.twitter.dto.dm.deprecatedV1.DirectMessage(text,
userId))
.build());
return getRequestHelperV1().postRequestWithBodyJson(url, null, body, io.github.redouane59.twitter.dto.dm.deprecatedV1.DmEvent.class)
.orElseThrow(NoSuchElementException::new);
} catch (JsonProcessingException e) {
LOGGER.error(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.redouane59.twitter.dto.dm;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import lombok.Builder;
import lombok.Getter;

@Builder
public class DmParameters {

@JsonProperty("conversation_type")
@Builder.Default
private String conversationType = "Group";
@JsonProperty("message")
private DmMessage message;
@JsonProperty("participant_ids")
private List<String> participantIds;

@Builder
@Getter
public static class DmMessage {

private String text;
private String attachments; // @to be improved
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.redouane59.twitter.dto.dm;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;

@Getter
public class PostDmResponse {

private PostDmEvent data;

@Getter
public static class PostDmEvent {

@JsonProperty("dm_conversation_id")
private String dmConversationId;
@JsonProperty("dm_event_id")
private String dmEventId;

}
}
Loading

0 comments on commit b8bf00f

Please sign in to comment.