Skip to content

Commit 863a611

Browse files
committed
Added the method copyMessage, which sends a copy of any message.
1 parent e29f0a4 commit 863a611

File tree

5 files changed

+156
-29
lines changed

5 files changed

+156
-29
lines changed

library/src/main/java/com/pengrad/telegrambot/model/MessageEntity.java

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.pengrad.telegrambot.model;
22

33
import java.io.Serializable;
4+
import java.util.Objects;
45

56
/**
67
* stas
@@ -21,6 +22,12 @@ public enum Type {
2122
private User user;
2223
private String language;
2324

25+
public MessageEntity(Type type, Integer offset, Integer length) {
26+
this.type = type;
27+
this.offset = offset;
28+
this.length = length;
29+
}
30+
2431
public Type type() {
2532
return type;
2633
}
@@ -45,30 +52,37 @@ public String language() {
4552
return language;
4653
}
4754

55+
public MessageEntity url(String url) {
56+
this.url = url;
57+
return this;
58+
}
59+
60+
public MessageEntity user(User user) {
61+
this.user = user;
62+
return this;
63+
}
64+
65+
public MessageEntity language(String language) {
66+
this.language = language;
67+
return this;
68+
}
69+
4870
@Override
4971
public boolean equals(Object o) {
5072
if (this == o) return true;
5173
if (o == null || getClass() != o.getClass()) return false;
52-
5374
MessageEntity that = (MessageEntity) o;
54-
55-
if (type != that.type) return false;
56-
if (offset != null ? !offset.equals(that.offset) : that.offset != null) return false;
57-
if (length != null ? !length.equals(that.length) : that.length != null) return false;
58-
if (url != null ? !url.equals(that.url) : that.url != null) return false;
59-
if (user != null ? !user.equals(that.user) : that.user != null) return false;
60-
return language != null ? language.equals(that.language) : that.language == null;
75+
return type == that.type &&
76+
Objects.equals(offset, that.offset) &&
77+
Objects.equals(length, that.length) &&
78+
Objects.equals(url, that.url) &&
79+
Objects.equals(user, that.user) &&
80+
Objects.equals(language, that.language);
6181
}
6282

6383
@Override
6484
public int hashCode() {
65-
int result = type != null ? type.hashCode() : 0;
66-
result = 31 * result + (offset != null ? offset.hashCode() : 0);
67-
result = 31 * result + (length != null ? length.hashCode() : 0);
68-
result = 31 * result + (url != null ? url.hashCode() : 0);
69-
result = 31 * result + (user != null ? user.hashCode() : 0);
70-
result = 31 * result + (language != null ? language.hashCode() : 0);
71-
return result;
85+
return Objects.hash(type, offset, length, url, user, language);
7286
}
7387

7488
@Override
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.pengrad.telegrambot.model;
2+
3+
import java.io.Serializable;
4+
import java.util.Objects;
5+
6+
/**
7+
* Stas Parshin
8+
* 06 November 2020
9+
*/
10+
public class MessageId implements Serializable {
11+
private final static long serialVersionUID = 0L;
12+
13+
private Integer message_id;
14+
15+
public Integer messageId() {
16+
return message_id;
17+
}
18+
19+
@Override
20+
public boolean equals(Object o) {
21+
if (this == o) return true;
22+
if (o == null || getClass() != o.getClass()) return false;
23+
MessageId messageId = (MessageId) o;
24+
return Objects.equals(message_id, messageId.message_id);
25+
}
26+
27+
@Override
28+
public int hashCode() {
29+
return Objects.hash(message_id);
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return "MessageId{" +
35+
"message_id=" + message_id +
36+
'}';
37+
}
38+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.pengrad.telegrambot.request;
2+
3+
import com.pengrad.telegrambot.model.MessageEntity;
4+
import com.pengrad.telegrambot.model.request.Keyboard;
5+
import com.pengrad.telegrambot.model.request.ParseMode;
6+
import com.pengrad.telegrambot.response.MessageIdResponse;
7+
8+
/**
9+
* Stas Parshin
10+
* 06 November 2020
11+
*/
12+
public class CopyMessage extends BaseRequest<CopyMessage, MessageIdResponse> {
13+
14+
public CopyMessage(Object chatId, Object fromChatId, int messageId) {
15+
super(MessageIdResponse.class);
16+
add("chat_id", chatId).add("from_chat_id", fromChatId).add("message_id", messageId);
17+
}
18+
19+
public CopyMessage caption(String caption) {
20+
return add("caption", caption);
21+
}
22+
23+
public CopyMessage parseMode(ParseMode parseMode) {
24+
return add("parse_mode", parseMode.name());
25+
}
26+
27+
public CopyMessage captionEntities(MessageEntity... entities) {
28+
return add("caption_entities", entities);
29+
}
30+
31+
public CopyMessage disableNotification(boolean disableNotification) {
32+
return add("disable_notification", disableNotification);
33+
}
34+
35+
public CopyMessage allowSendingWithoutReply(boolean allowSendingWithoutReply) {
36+
return add("allow_sending_without_reply", allowSendingWithoutReply);
37+
}
38+
39+
public CopyMessage replyToMessageId(int replyToMessageId) {
40+
return add("reply_to_message_id", replyToMessageId);
41+
}
42+
43+
public CopyMessage replyMarkup(Keyboard replyMarkup) {
44+
return add("reply_markup", replyMarkup);
45+
}
46+
47+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.pengrad.telegrambot.response;
2+
3+
import com.pengrad.telegrambot.model.Message;
4+
import com.pengrad.telegrambot.model.MessageId;
5+
6+
/**
7+
* Stas Parshin
8+
* 06 November 2020
9+
*/
10+
public class MessageIdResponse extends BaseResponse {
11+
12+
private MessageId result;
13+
14+
public MessageId result() {
15+
return result;
16+
}
17+
18+
public Integer messageId() {
19+
return result.messageId();
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return "MessageIdResponse{" +
25+
"result=" + result +
26+
'}';
27+
}
28+
}

library/src/test/java/com/pengrad/telegrambot/TelegramBotTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,7 @@
6969
import com.pengrad.telegrambot.passport.SecureValue;
7070
import com.pengrad.telegrambot.passport.SetPassportDataErrors;
7171
import com.pengrad.telegrambot.request.*;
72-
import com.pengrad.telegrambot.response.BaseResponse;
73-
import com.pengrad.telegrambot.response.GetChatAdministratorsResponse;
74-
import com.pengrad.telegrambot.response.GetChatMembersCountResponse;
75-
import com.pengrad.telegrambot.response.GetFileResponse;
76-
import com.pengrad.telegrambot.response.GetMeResponse;
77-
import com.pengrad.telegrambot.response.GetMyCommandsResponse;
78-
import com.pengrad.telegrambot.response.GetStickerSetResponse;
79-
import com.pengrad.telegrambot.response.GetUpdatesResponse;
80-
import com.pengrad.telegrambot.response.GetUserProfilePhotosResponse;
81-
import com.pengrad.telegrambot.response.GetWebhookInfoResponse;
82-
import com.pengrad.telegrambot.response.MessagesResponse;
83-
import com.pengrad.telegrambot.response.PollResponse;
84-
import com.pengrad.telegrambot.response.SendResponse;
85-
import com.pengrad.telegrambot.response.StringResponse;
72+
import com.pengrad.telegrambot.response.*;
8673
import okhttp3.OkHttpClient;
8774
import okhttp3.Request;
8875
import okhttp3.Response;
@@ -684,6 +671,19 @@ public void forwardMessage() {
684671
assertNotNull(message.forwardSenderName());
685672
}
686673

674+
@Test
675+
public void copyMessage() {
676+
MessageIdResponse response = bot.execute(new CopyMessage(chatId, chatId, forwardMessageId)
677+
.caption("new **caption**")
678+
.parseMode(ParseMode.MarkdownV2)
679+
.captionEntities(new MessageEntity(MessageEntity.Type.bold, 0, 1))
680+
.allowSendingWithoutReply(false)
681+
.replyToMessageId(1)
682+
.disableNotification(true)
683+
);
684+
assertTrue(response.messageId() > 0);
685+
}
686+
687687
@Test
688688
public void sendAudio() {
689689
Message message = bot.execute(new SendAudio(chatId, audioFileId)).message();

0 commit comments

Comments
 (0)