Skip to content

Commit

Permalink
Added new methods for working with stickers: getStickerSet, uploadSti…
Browse files Browse the repository at this point in the history
…ckerFile, createNewStickerSet, addStickerToSet, setStickerPositionInSet, and deleteStickerFromSet
  • Loading branch information
pengrad committed Jul 23, 2017
1 parent 46fdd82 commit d77fa4d
Show file tree
Hide file tree
Showing 17 changed files with 251 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.response.BaseResponse;

import java.io.File;

/**
* Stas Parshin
* 23 July 2017
*/
abstract public class AbstractUploadRequest<T extends BaseRequest, R extends BaseResponse> extends BaseRequest<T, R> {

private final boolean isMultipart;

public AbstractUploadRequest(Class<? extends R> responseClass, String paramName, Object data) {
super(responseClass);
if (data instanceof String) {
isMultipart = false;
} else if (data instanceof File) {
isMultipart = true;
} else if (data instanceof byte[]) {
isMultipart = true;
} else {
throw new IllegalArgumentException("Sending data should be String, File or byte[]");
}
add(paramName, data);
}

@Override
public boolean isMultipart() {
return isMultipart;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.response.BaseResponse;

/**
* Stas Parshin
* 23 July 2017
*/
public class AddStickerToSet extends AbstractUploadRequest<AddStickerToSet, BaseResponse> {

public AddStickerToSet(Integer userId, String name, Object pngSticker, String emojis) {
super(BaseResponse.class, "png_sticker", pngSticker);
add("user_id", userId);
add("name", name);
add("emojis", emojis);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.pengrad.telegrambot.request;

import com.google.gson.Gson;
import com.pengrad.telegrambot.model.request.InlineQueryResult;
import com.pengrad.telegrambot.response.BaseResponse;

Expand All @@ -10,12 +9,9 @@
*/
public class AnswerInlineQuery extends BaseRequest<AnswerInlineQuery, BaseResponse> {

// todo remove gson
private static Gson gson = new Gson();

public AnswerInlineQuery(String inlineQueryId, InlineQueryResult... results) {
super(BaseResponse.class);
add("inline_query_id", inlineQueryId).add("results", gson.toJson(results));
add("inline_query_id", inlineQueryId).add("results", serialize(results));
}

public AnswerInlineQuery cacheTime(int cacheTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class AnswerShippingQuery extends BaseRequest<AnswerShippingQuery, BaseRe

public AnswerShippingQuery(String shippingQueryId, ShippingOption... shippingOptions) {
super(BaseResponse.class);
add("shipping_query_id", shippingQueryId).add("ok", true).add("shipping_options", gson.toJson(shippingOptions));
add("shipping_query_id", shippingQueryId).add("ok", true).add("shipping_options", serialize(shippingOptions));
}

public AnswerShippingQuery(String shippingQueryId, String errorMessage) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pengrad.telegrambot.request;

import com.google.gson.Gson;
import com.pengrad.telegrambot.response.BaseResponse;

import java.lang.reflect.Type;
Expand All @@ -12,6 +13,8 @@
*/
abstract public class BaseRequest<T extends BaseRequest, R extends BaseResponse> {

private static final Gson gson = new Gson();

@SuppressWarnings("unchecked")
protected final T thisAsT = (T) this;

Expand Down Expand Up @@ -56,4 +59,10 @@ public String getContentType() {
public int getTimeoutSeconds() {
return 0;
}

// Serialize model objects. Basically convert to json
// todo move to TelegramBotClient, let it serialize everything in request time
protected String serialize(Object o) {
return gson.toJson(o);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.model.MaskPosition;
import com.pengrad.telegrambot.response.BaseResponse;

/**
* Stas Parshin
* 23 July 2017
*/
public class CreateNewStickerSet extends AbstractUploadRequest<CreateNewStickerSet, BaseResponse> {

public CreateNewStickerSet(Integer userId, String name, String title, Object pngSticker, String emojis) {
super(BaseResponse.class, "png_sticker", pngSticker);
add("user_id", userId);
add("name", name);
add("title", title);
add("emojis", emojis);
}

public CreateNewStickerSet containsMasks(boolean containsMasks) {
return add("contains_masks", containsMasks);
}

public CreateNewStickerSet maskPosition(MaskPosition maskPosition) {
return add("mask_position", serialize(maskPosition)).containsMasks(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.response.BaseResponse;

/**
* Stas Parshin
* 23 July 2017
*/
public class DeleteStickerFromSet extends BaseRequest<DeleteStickerFromSet, BaseResponse> {
public DeleteStickerFromSet(String sticker) {
super(BaseResponse.class);
add("sticker", sticker);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.response.GetStickerSetResponse;

/**
* Stas Parshin
* 23 July 2017
*/
public class GetStickerSet extends BaseRequest<GetStickerSet, GetStickerSetResponse> {

public GetStickerSet(String name) {
super(GetStickerSetResponse.class);
add("name", name);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.pengrad.telegrambot.request;

import com.google.gson.Gson;
import com.pengrad.telegrambot.response.GetUpdatesResponse;

/**
Expand All @@ -9,9 +8,6 @@
*/
public class GetUpdates extends BaseRequest<GetUpdates, GetUpdatesResponse> {

// todo remove gson
private static Gson gson = new Gson();

private int timeout = 0;
private int limit = 100;

Expand All @@ -34,7 +30,7 @@ public GetUpdates timeout(int timeout) {
}

public GetUpdates allowedUpdates(String... allowedUpdates) {
return add("allowed_updates", gson.toJson(allowedUpdates));
return add("allowed_updates", serialize(allowedUpdates));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.pengrad.telegrambot.request;

import com.google.gson.Gson;
import com.pengrad.telegrambot.model.request.LabeledPrice;

/**
Expand All @@ -9,14 +8,11 @@
*/
public class SendInvoice extends AbstractSendRequest<SendInvoice> {

// todo remove gson
private static Gson gson = new Gson();

public SendInvoice(Integer chatId, String title, String description, String payload, String providerToken,
String startParameter, String currency, LabeledPrice... prices) {
super(chatId);
add("title", title).add("description", description).add("payload", payload).add("provider_token", providerToken)
.add("start_parameter", startParameter).add("currency", currency).add("prices", gson.toJson(prices));
.add("start_parameter", startParameter).add("currency", currency).add("prices", serialize(prices));
}

public SendInvoice photoUrl(String photoUrl) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.response.BaseResponse;

/**
* Stas Parshin
* 23 July 2017
*/
public class SetStickerPositionInSet extends BaseRequest<SetStickerPositionInSet, BaseResponse> {

public SetStickerPositionInSet(String sticker, int position) {
super(BaseResponse.class);
add("sticker", sticker).add("position", position);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.pengrad.telegrambot.request;

import com.google.gson.Gson;
import com.pengrad.telegrambot.response.BaseResponse;

import java.io.File;
Expand All @@ -11,9 +10,6 @@
*/
public class SetWebhook extends BaseRequest<SetWebhook, BaseResponse> {

// todo remove gson
private static Gson gson = new Gson();

private boolean isMultipart = false;

public SetWebhook() {
Expand All @@ -39,7 +35,7 @@ public SetWebhook maxConnections(int maxConnections) {
}

public SetWebhook allowedUpdates(String... allowedUpdates) {
return add("allowed_updates", gson.toJson(allowedUpdates));
return add("allowed_updates", serialize(allowedUpdates));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.pengrad.telegrambot.request;

import com.pengrad.telegrambot.response.GetFileResponse;

/**
* Stas Parshin
* 23 July 2017
*/
public class UploadStickerFile extends AbstractUploadRequest<UploadStickerFile, GetFileResponse> {

public UploadStickerFile(Integer userId, Object pngSticker) {
super(GetFileResponse.class, "png_sticker", pngSticker);
add("user_id", userId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.pengrad.telegrambot.response;

import com.pengrad.telegrambot.model.StickerSet;

/**
* Stas Parshin
* 23 July 2017
*/
public class GetStickerSetResponse extends BaseResponse {

private StickerSet result;

public StickerSet stickerSet() {
return result;
}

@Override
public String toString() {
return "GetStickerSetResponse{" +
"result=" + result +
'}';
}
}
6 changes: 5 additions & 1 deletion library/src/test/java/com/pengrad/telegrambot/FileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
public class FileTest {

public static void check(File file) {
check(file, true);
}

public static void check(File file, boolean path) {
assertNotNull(file.fileId());
assertNotNull(file.fileSize());
assertNotNull(file.filePath());
if (path) assertNotNull(file.filePath());
}
}
Loading

0 comments on commit d77fa4d

Please sign in to comment.