Skip to content

Commit

Permalink
Add TelegramChat object (not all fields are present in the current ve…
Browse files Browse the repository at this point in the history
…rsion)
  • Loading branch information
EXH3Y committed Oct 28, 2016
1 parent fded065 commit e32a7c0
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 2 deletions.
57 changes: 57 additions & 0 deletions src/main/java/exh3y/telebot/data/TelegramChat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package exh3y.telebot.data;

import org.json.JSONObject;

import exh3y.telebot.data.enums.ETelegramChatType;

public class TelegramChat extends JSONObject {

private ETelegramChatType type;

/**
* Creates a new TelegramChat object from a given JSON-String
*
* @param chat
* @since 0.0.6
*/
public TelegramChat(JSONObject chat) {

super(chat.toString());

this.type = ETelegramChatType.getEnumByName(chat.getString("type"));
}

/**
* Returns the chat's type
*
* @return The chat's type
* @since 0.0.6
*/
public ETelegramChatType getType() {

return type;
}

/**
* Returns the chat's id
*
* @return The chat's id
* @since 0.0.6
*/
public int getId() {

return this.getInt("id");
}

/**
* Returns the chat's title
*
* @return The title
* @since 0.0.6
*/
public String getTitle() {

return this.getString("title");
}

}
20 changes: 18 additions & 2 deletions src/main/java/exh3y/telebot/data/TelegramMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

public class TelegramMessage extends JSONObject {

private TelegramChat chat;

/**
* <p>
* Creates a new TelegramMessage object from a given JSON-String
Expand All @@ -15,6 +17,7 @@ public class TelegramMessage extends JSONObject {
public TelegramMessage(JSONObject message) {

super(message.toString());
chat = new TelegramChat(this.getJSONObject("chat"));
}

/**
Expand Down Expand Up @@ -50,10 +53,11 @@ public String getText() {
*
* @return The chatID
* @since 0.0.3
* @deprecated You should use the methods provided by TelegramChat using message.getChat().
*/
public int getChatId() {

return this.getJSONObject("chat").getInt("id");
return this.chat.getId();
}

/**
Expand All @@ -74,10 +78,22 @@ public int getMessageId() {
*
* @return The type of the chat
* @since 0.0.5
* @deprecated You should use the methods provided by TelegramChat using message.getChat().
*/
public String getChatType() {

return this.getJSONObject("chat").getString("type");
return this.chat.getString("type");
}

/**
* Returns the message's chat
*
* @return The chat
* @since 0.0.6
*/
public TelegramChat getChat() {

return chat;
}

}
42 changes: 42 additions & 0 deletions src/main/java/exh3y/telebot/data/enums/ETelegramChatType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package exh3y.telebot.data.enums;

/**
* Contains all possible chat types.
*
* @since 0.0.6
*/
public enum ETelegramChatType {

PRIVATE("private"),
GROUP("group"),
SUPERGROUP("supergroup"),
CHANNEL("channel");

private String chatTypeString;
ETelegramChatType(String chatTypeString) {
this.chatTypeString = chatTypeString;
}

public String getChatTypeString() { return chatTypeString; }

/**
* Returns the ETelegramChatType (determined by the content of the type field)
*
* @param name
* @return The ETelegramChatType
* @since 0.0.6
*/
public static ETelegramChatType getEnumByName(String name) {

for (ETelegramChatType singleEnum : ETelegramChatType.values()) {

if (singleEnum.getChatTypeString().equals(name)) {
return singleEnum;
}
}

return null;

}

}

0 comments on commit e32a7c0

Please sign in to comment.