Commit 7d83924e authored by Delorus's avatar Delorus
Browse files

Merge branch 'yes-no-bot' into 'develop'

(yesnobot) add new bot

See merge request !46
parents e26e8069 b1a27aa0
Pipeline #483 passed with stage
in 1 minute and 36 seconds
......@@ -10,17 +10,13 @@ import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.telegram.telegrambots.meta.api.methods.PartialBotApiMethod;
import org.telegram.telegrambots.meta.api.methods.send.SendVideo;
import org.telegram.telegrambots.meta.api.objects.InputFile;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import page.devnet.hacks.BufferPipedInputStream;
import page.devnet.pluginmanager.Plugin;
import page.devnet.telegrambot.util.CommandUtils;
import page.devnet.vertxtgbot.tgapi.SendExternalAnimation;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
......@@ -78,21 +74,21 @@ public class YesNoPlugin implements Plugin<Update, List<PartialBotApiMethod<?>>>
var image = tryExecute("yes");
return List.of(
new SendVideo(String.valueOf(message.getChatId()), new InputFile(image, "yes"))
new SendExternalAnimation(String.valueOf(message.getChatId()), image)
);
}
case "no": {
var image = tryExecute("no");
return List.of(
new SendVideo(String.valueOf(message.getChatId()), new InputFile(image, "yes"))
new SendExternalAnimation(String.valueOf(message.getChatId()), image)
);
}
case "maybe": {
var image = tryExecute("maybe");
return List.of(
new SendVideo(String.valueOf(message.getChatId()), new InputFile(image, "yes"))
new SendExternalAnimation(String.valueOf(message.getChatId()), image)
);
}
}
......@@ -100,18 +96,17 @@ public class YesNoPlugin implements Plugin<Update, List<PartialBotApiMethod<?>>>
return Collections.emptyList();
}
private InputStream tryExecute(String type) {
private String tryExecute(String type) {
try {
HttpResponse response = client.execute(HttpHost.create(API_URL), new HttpGet("/api?force=" + type));
try (InputStreamReader reader = new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8)) {
ApiResponse resp = mapper.readValue(reader, ApiResponse.class);
byte[] image = client.execute(new HttpGet(resp.image)).getEntity().getContent().readAllBytes();
return BufferPipedInputStream.fromBytes(image);
return resp.image;
}
} catch (IOException e) {
log.warn(e.getMessage(), e);
return new ByteArrayInputStream(new byte[0]);
return "";
}
}
}
package page.devnet.vertxtgbot.tgapi;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.send.SendAnimation;
import org.telegram.telegrambots.meta.api.objects.ApiResponse;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException;
import java.io.IOException;
/**
* @author mshherbakov
* @since 18.06.2021
*/
@EqualsAndHashCode(callSuper = false)
@Getter
@Setter
@ToString
@Builder
public class SendExternalAnimation extends BotApiMethod<Message> {
@JsonProperty("chat_id")
private final String chatId;
@JsonProperty("animation")
private final String animationUrl;
public SendExternalAnimation(String chatId, String url) {
this.chatId = chatId;
this.animationUrl = url;
}
@Override
public String getMethod() {
return SendAnimation.PATH;
}
@Override
public Message deserializeResponse(String answer) throws TelegramApiRequestException {
try {
ApiResponse<Message> result = OBJECT_MAPPER.readValue(answer, new TypeReference<>() { });
if (result.getOk()) {
return result.getResult();
} else {
throw new TelegramApiRequestException("Error sending animation", result);
}
} catch (IOException e) {
throw new TelegramApiRequestException("Unable to deserialize response", e);
}
}
@Override
public void validate() throws TelegramApiValidationException {
if (chatId == null || chatId.isEmpty()) {
throw new TelegramApiValidationException("ChatId parameter can't be empty", this);
}
if (animationUrl == null || animationUrl.isEmpty()) {
throw new TelegramApiValidationException("Animation parameter can't be empty", this);
}
}
}
......@@ -46,6 +46,9 @@ class VertxWebClientWrapper implements Transport {
if (resp.failed()) {
log.error("Failed to send command to {}: {}", url, resp.cause());
}
if (resp.result().statusCode() >= 300) {
log.error("Failed to send command to {}: {}", url, resp.result().bodyAsJsonObject());
}
});
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment