Created
May 22, 2023 12:18
-
-
Save juarezjuniorgithub/c44efec1e7324a060716e8dd53f116c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.oracle.dev.jdbc; | |
import org.eclipse.microprofile.config.inject.ConfigProperty; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import jakarta.annotation.PostConstruct; | |
import jakarta.annotation.PreDestroy; | |
import jakarta.enterprise.context.ApplicationScoped; | |
import jakarta.json.JsonObject; | |
import jakarta.ws.rs.client.Client; | |
import jakarta.ws.rs.client.ClientBuilder; | |
import jakarta.ws.rs.client.WebTarget; | |
import jakarta.ws.rs.core.Response; | |
@ApplicationScoped | |
public class OracleTelegramBot { | |
private static final Logger logger = LoggerFactory.getLogger(OracleTelegramBot.class); | |
@ConfigProperty(name = "telegram.token") | |
private String token; | |
@ConfigProperty(name = "telegram.chatId") | |
private String chatId; | |
private Client client; | |
private WebTarget baseTarget; | |
public void sendMessage(String message) { | |
Response response = baseTarget.path("sendMessage").queryParam("chat_id", chatId).queryParam("text", message) | |
.request().get(); | |
JsonObject json = response.readEntity(JsonObject.class); | |
boolean ok = json.getBoolean("ok", false); | |
if (!ok) { | |
logger.error("Send message failed!"); | |
} | |
} | |
@PostConstruct | |
void initClient() { | |
client = ClientBuilder.newClient(); | |
baseTarget = client.target("https://api.telegram.org/bot{token}").resolveTemplate("token", this.token); | |
} | |
@PreDestroy | |
private void closeClient() { | |
client.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment