Skip to content

Commit abae574

Browse files
committed
TelegramBot is ready
1 parent fa77df1 commit abae574

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

src/main/java/attestation_2/weatherBot/Subscribers.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,37 @@
55

66
public class Subscribers extends Thread {
77
private static final Calendar day = Calendar.getInstance();
8-
private static final int[] time = new int[]{12, 0, 0};
9-
private static final long period = TimeUnit.MILLISECONDS.convert(30, TimeUnit.SECONDS);
10-
private final HashMap<String, User> subscribers;
8+
private static final int[] time = new int[]{9, 0, 0};
9+
private static final long period = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
10+
private final HashSet<User> subscribers;
1111
private final Timer timer;
1212

1313
public Subscribers() {
14-
subscribers = new HashMap<>();
14+
subscribers = new HashSet<>();
1515
day.set(Calendar.HOUR_OF_DAY, time[0]);
1616
day.set(Calendar.MINUTE, time[1]);
1717
day.set(Calendar.SECOND, time[2]);
1818
timer = new Timer();
1919
}
2020

2121
public void add(User user) {
22-
this.subscribers.put(user.getChatId(), user);
22+
this.subscribers.add(user);
2323
}
2424

2525
public void remove(User user) {
26-
this.subscribers.remove(user.getChatId());
26+
this.subscribers.remove(user);
2727
}
2828

29-
public boolean hasId(String id) { return this.subscribers.containsKey(id); }
29+
public boolean contains(User user) { return this.subscribers.contains(user); }
3030

3131
@Override
3232
public void start() {
3333
timer.schedule(new TimerTask() {
3434
@Override
3535
public void run() {
36-
for (Map.Entry<String, User> entry: subscribers.entrySet()) {
37-
entry.getValue().sendCurrent();
36+
for (User user: subscribers) {
37+
System.out.println(user);
38+
user.sendCurrent();
3839
}
3940
}
4041
}, day.getTime(), period);

src/main/java/attestation_2/weatherBot/TelegramBot.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public TelegramBot() {
2828
super();
2929
User.setTelegramBot(this);
3030
User.setWeatherGetter(weatherGetter);
31+
User.setSubscribers(subscribers);
3132
subscribers.start();
3233
}
3334

@@ -111,11 +112,11 @@ public void command(User user, String command) {
111112
}
112113
}
113114

114-
public void sendMsg(String id, String text, boolean withMenu) {
115+
public void sendMsg(User user, String text) {
115116
try {
116117
SendMessage outMsg = new SendMessage();
117-
setKeyBoard(outMsg, subscribers.hasId(id), withMenu);
118-
outMsg.setChatId(id);
118+
setKeyBoard(outMsg, user.isSubscribed(), user.hasLocation());
119+
outMsg.setChatId(user.getChatId());
119120
outMsg.setText(text);
120121
outMsg.setParseMode("HTML");
121122
execute(outMsg);

src/main/java/attestation_2/weatherBot/User.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import java.io.IOException;
44

55
public class User {
6+
private static Subscribers subscribers;
67
private static TelegramBot telegramBot;
78
private static WeatherGetter weatherGetter;
89
private final String chatId;
910
private String name;
10-
private String location = null;
11+
private String location;
1112

1213
public static void setWeatherGetter(WeatherGetter weatherGetter) {
1314
User.weatherGetter = weatherGetter;
@@ -17,6 +18,10 @@ public static void setTelegramBot(TelegramBot telegramBot) {
1718
User.telegramBot = telegramBot;
1819
}
1920

21+
public static void setSubscribers(Subscribers subscribers) {
22+
User.subscribers = subscribers;
23+
}
24+
2025
public String getChatId() {
2126
return chatId;
2227
}
@@ -29,6 +34,10 @@ public String getLocation() {
2934
return location;
3035
}
3136

37+
public boolean isSubscribed() {
38+
return subscribers.contains(this);
39+
}
40+
3241
public void setLocation(String location) {
3342
this.location = location;
3443
}
@@ -46,17 +55,23 @@ public boolean hasLocation() {
4655
return this.getLocation() != null;
4756
}
4857

58+
public void setSubscribed(boolean val) {
59+
if (val) {
60+
subscribers.add(this);
61+
} else {
62+
subscribers.remove(this);
63+
}
64+
}
65+
4966
public void send(String text) {
50-
System.out.println(hasLocation());
51-
System.out.println(location);
52-
telegramBot.sendMsg(chatId, text, hasLocation());
67+
telegramBot.sendMsg(this, text);
5368
}
5469

5570
public void sendCurrent() {
5671
try {
5772
String jsonString = weatherGetter.getCurrent(getLocation());
5873
String outText = Parser.current(jsonString);
59-
telegramBot.sendMsg(chatId, outText, true);
74+
telegramBot.sendMsg(this, outText);
6075
} catch (IOException e) {
6176
e.printStackTrace();
6277
}

0 commit comments

Comments
 (0)