-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelegramBot
More file actions
60 lines (52 loc) · 2.05 KB
/
Copy pathTelegramBot
File metadata and controls
60 lines (52 loc) · 2.05 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
package com.sample.TelegramBot.test;
import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.TelegramBotsApi;
import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Message;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.exceptions.TelegramApiException;
import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
public class SimpleBot extends TelegramLongPollingBot {
public static void main(String[] args) {
ApiContextInitializer.init();
TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
try {
telegramBotsApi.registerBot(new SimpleBot());
} catch (TelegramApiRequestException e) {
e.printStackTrace();
}
}
@Override
public String getBotUsername() {
return "Archi";
}
@Override
public String getBotToken() {
return "312207039:AAFtCfv3755h_tYni8ANHqf8VYqJ7JjU344";
}
@Override
public void onUpdateReceived(Update update) {
Message message = update.getMessage();
if (message != null && message.hasText()) {
if (message.getText().equals("/help"))
sendMsg(message, "Привет, я робот");
if (message.getText().equals("Привет")||(message.getText().equals("привет")))
sendMsg(message, "Привет, Товарищ!");
else
sendMsg(message,"Пока, пока!");
}
}
private void sendMsg(Message message, String text) {
SendMessage sendMessage = new SendMessage();
sendMessage.enableMarkdown(true);
sendMessage.setChatId(message.getChatId().toString());
sendMessage.setReplyToMessageId(message.getMessageId());
sendMessage.setText(text);
try {
sendMessage(sendMessage);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
}