Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.n1netails</groupId>
<artifactId>n1netails-telegram-client</artifactId>
<version>0.2.1</version>
<version>0.3.0</version>
<packaging>jar</packaging>

<name>n1netails-telegram-client</name>
Expand Down Expand Up @@ -54,6 +54,30 @@
<artifactId>json</artifactId>
<version>20240303</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
34 changes: 32 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ Install the telegram client by adding the following dependency:
<dependency>
<groupId>com.n1netails</groupId>
<artifactId>n1netails-telegram-client</artifactId>
<version>0.2.0</version>
<version>0.3.0</version>
</dependency>
```

Gradle (Groovy)
```groovy
implementation 'com.n1netails:n1netails-telegram-client:0.2.0'
implementation 'com.n1netails:n1netails-telegram-client:0.3.0'
```

## Configure
Expand Down Expand Up @@ -168,6 +168,26 @@ public class ExampleService {
String botToken = "your-telegram-bot-token";
telegramClient.sendMessage(chatId, botToken, telegramMessage);
}

public void telegramGifNotificationExample() {
TelegramMessage telegramMessage = new TelegramMessage("Check out this GIF!", "https://media1.giphy.com/media/v1.Y2lkPTc5MGI3NjExaDRhOWtpMnVsM2NiMzJ4aXpoOXpuamZzcHpudG4zbzIzenVlaHN0eSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/xsE65jaPsUKUo/giphy.gif", false);
// replace with your telegram chat id
String chatId = "your-telegram-chat-id";
// replace with your telegram bot token
String botToken = "your-telegram-bot-token";
telegramClient.sendMessage(chatId, botToken, telegramMessage);
}

public void telegramGifNotificationWithCtaButtonsExample() {
Button button = new Button("Visit N1netails", "https://n1netails.com");
InlineKeyboardMarkup keyboardMarkup = new InlineKeyboardMarkup(Collections.singletonList(Collections.singletonList(button)));
TelegramMessage telegramMessage = new TelegramMessage("Check out this GIF!", "https://media1.giphy.com/media/v1.Y2lkPTc5MGI3NjExaDRhOWtpMnVsM2NiMzJ4aXpoOXpuamZzcHpudG4zbzIzenVlaHN0eSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/xsE65jaPsUKUo/giphy.gif", false, keyboardMarkup);
// replace with your telegram chat id
String chatId = "your-telegram-chat-id";
// replace with your telegram bot token
String botToken = "your-telegram-bot-token";
telegramClient.sendMessage(chatId, botToken, telegramMessage);
}
}
```

Expand All @@ -176,6 +196,16 @@ public class ExampleService {
<img src="telegram-message.png" alt="N1netails telegram message simple" width="500" style="display: block; margin: auto;"/>
</div>

#### Example message GIF output
<div align="center">
<img src="telegram-gif-message.png" alt="N1netails telegram message simple" width="500" style="display: block; margin: auto;"/>
</div>

#### Example message GIF with CTA Buttons output
<div align="center">
<img src="telegram-gif-cta-message.png" alt="N1netails telegram message simple" width="500" style="display: block; margin: auto;"/>
</div>

# Develop
## Build
Build the project using the following command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
public class TelegramMessage {

private String text;
private String animation;
private boolean disableNotification;
private InlineKeyboardMarkup replyMarkup;

Expand All @@ -20,6 +21,18 @@ public class TelegramMessage {
*/
public TelegramMessage() {}

/**
* Telegram Message Constructor
* @param text telegram content
* @param animation animation url
* @param disableNotification disable telegram notification
*/
public TelegramMessage(String text, String animation, boolean disableNotification) {
this.text = text;
this.animation = animation;
this.disableNotification = disableNotification;
}

/**
* Telegram Message Constructor
* @param text telegram content
Expand All @@ -41,4 +54,18 @@ public TelegramMessage(String text, boolean disableNotification, InlineKeyboardM
this.disableNotification = disableNotification;
this.replyMarkup = replyMarkup;
}

/**
* Telegram Message Constructor
* @param text telegram content
* @param animation animation url
* @param disableNotification disable telegram notification
* @param replyMarkup the reply markup
*/
public TelegramMessage(String text, String animation, boolean disableNotification, InlineKeyboardMarkup replyMarkup) {
this.text = text;
this.animation = animation;
this.disableNotification = disableNotification;
this.replyMarkup = replyMarkup;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,29 @@ public BotService() {}
*/
public void send(String chatId, String botToken, TelegramMessage message) throws TelegramClientException {
try {
String apiUrl = "https://api.telegram.org/bot" + botToken + "/sendMessage";
boolean isAnimation = message.getAnimation() != null && !message.getAnimation().isEmpty();
String method = isAnimation ? "sendAnimation" : "sendMessage";
String apiUrl = "https://api.telegram.org/bot" + botToken + "/" + method;

JSONObject jsonPayload = new JSONObject();
jsonPayload.put("chat_id", chatId);
jsonPayload.put("text", message.getText());

if (isAnimation) {
jsonPayload.put("animation", message.getAnimation());
if (message.getText() != null) {
jsonPayload.put("caption", message.getText());
}
} else {
jsonPayload.put("text", message.getText());
}

jsonPayload.put("disable_notification", message.isDisableNotification());

if (message.getReplyMarkup() != null) {
jsonPayload.put("reply_markup", new JSONObject(message.getReplyMarkup()));
}

HttpURLConnection conn = (HttpURLConnection) new URL(apiUrl).openConnection();
HttpURLConnection conn = openConnection(apiUrl);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json");
Expand All @@ -66,4 +78,8 @@ public void send(String chatId, String botToken, TelegramMessage message) throws
throw new TelegramClientException("Failed to send Telegram message", e);
}
}

protected HttpURLConnection openConnection(String apiUrl) throws Exception {
return (HttpURLConnection) new URL(apiUrl).openConnection();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.n1netails.n1netails.telegram.service;

import com.n1netails.n1netails.telegram.model.Button;
import com.n1netails.n1netails.telegram.model.InlineKeyboardMarkup;
import com.n1netails.n1netails.telegram.model.TelegramMessage;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.util.Collections;
import java.net.HttpURLConnection;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

public class BotServiceTest {

private BotService botService;
private HttpURLConnection mockConnection;
private ByteArrayOutputStream outputStream;
private String lastApiUrl;

@BeforeEach
void setUp() throws Exception {
mockConnection = mock(HttpURLConnection.class);
outputStream = new ByteArrayOutputStream();
when(mockConnection.getOutputStream()).thenReturn(outputStream);
when(mockConnection.getResponseCode()).thenReturn(200);

botService = new BotService() {
@Override
protected HttpURLConnection openConnection(String apiUrl) throws Exception {
lastApiUrl = apiUrl;
return mockConnection;
}
};
}

@Test
void testSendTextMessage() throws Exception {
TelegramMessage message = new TelegramMessage("Hello", false);
botService.send("chat123", "token123", message);

assertEquals("https://api.telegram.org/bottoken123/sendMessage", lastApiUrl);
JSONObject payload = new JSONObject(outputStream.toString());
assertEquals("chat123", payload.getString("chat_id"));
assertEquals("Hello", payload.getString("text"));
}

@Test
void testSendAnimationMessage() throws Exception {
TelegramMessage message = new TelegramMessage("My GIF", "http://example.com/gif.gif", true);
botService.send("chat123", "token123", message);

assertEquals("https://api.telegram.org/bottoken123/sendAnimation", lastApiUrl);
JSONObject payload = new JSONObject(outputStream.toString());
assertEquals("chat123", payload.getString("chat_id"));
assertEquals("http://example.com/gif.gif", payload.getString("animation"));
assertEquals("My GIF", payload.getString("caption"));
assertEquals(true, payload.getBoolean("disable_notification"));
}

@Test
void testSendAnimationWithKeyboard() throws Exception {
Button button = new Button("Click me", "http://example.com");
InlineKeyboardMarkup keyboard = new InlineKeyboardMarkup(Collections.singletonList(Collections.singletonList(button)));
TelegramMessage message = new TelegramMessage("My GIF", "http://example.com/gif.gif", false, keyboard);
botService.send("chat123", "token123", message);

assertEquals("https://api.telegram.org/bottoken123/sendAnimation", lastApiUrl);
JSONObject payload = new JSONObject(outputStream.toString());
assertEquals("chat123", payload.getString("chat_id"));
assertEquals("http://example.com/gif.gif", payload.getString("animation"));
assertEquals("My GIF", payload.getString("caption"));
JSONObject replyMarkup = payload.getJSONObject("reply_markup");
assertEquals("Click me", replyMarkup.getJSONArray("inline_keyboard").getJSONArray(0).getJSONObject(0).getString("text"));
}
}
Binary file added telegram-gif-cta-message.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added telegram-gif-message.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading