-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBotClient.java
More file actions
78 lines (70 loc) · 2.9 KB
/
Copy pathBotClient.java
File metadata and controls
78 lines (70 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class BotClient extends Client {
public static void main(String[] args) {
new BotClient().run();
}
@Override
protected SocketThread getSocketThread() {
return new BotSocketThread();
}
@Override
protected boolean shouldSendTextFromConsole() {
return false;
}
@Override
protected String getUserName() {
return String.format("date_bot_%s", (int)(Math.random()*100));
}
public class BotSocketThread extends SocketThread{
@Override
protected void clientMainLoop() throws IOException, ClassNotFoundException {
sendTextMessage("Hey chat. I'm a bot. Commands I can understand: date, day, month, year, time, hour, minuets, seconds.");
super.clientMainLoop();
}
@Override
protected void processIncomingMessage(String message) {
if (message != null) {
ConsoleHelper.writeMessage(message);
SimpleDateFormat format = null;
if (message.contains(": ")) {
String[] arr = message.split(": ");
if (arr.length == 2 && arr[1] != null) {
String name = arr[0];
String text = arr[1];
switch (text) {
case "дата":
format = new SimpleDateFormat("d.MM.YYYY");
break;
case "день":
format = new SimpleDateFormat("d");
break;
case "месяц":
format = new SimpleDateFormat("MMMM");
break;
case "год":
format = new SimpleDateFormat("YYYY");
break;
case "время":
format = new SimpleDateFormat("H:mm:ss");
break;
case "час":
format = new SimpleDateFormat("H");
break;
case "минуты":
format = new SimpleDateFormat("m");
break;
case "секунды":
format = new SimpleDateFormat("s");
break;
}
if (format != null) {
sendTextMessage(String.format("Информация для %s: %s", name, format.format(Calendar.getInstance().getTime())));
}
}
}
}
}
}
}