├── README.md ├── pom.xml └── src └── main └── java ├── MainClass.java └── ZaurEduBot.java /README.md: -------------------------------------------------------------------------------- 1 | # telegram-bot-java-tutorial 2 | TelegramBots / Telegram Bot in java implementation tutorial example / sample 3 | 4 | 1. clone the project 5 | 2. open pom.xml with IntelliJ IDEA 6 | 3. Create your own bot with @Botfather 7 | 4. replace my token with your own (from @Botfather) 8 | 5. Run from MainClass 9 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | zaurkhb.com 8 | zauredu_bot 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | 14 | 15 | org.telegram 16 | telegrambots 17 | 3.5 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/main/java/MainClass.java: -------------------------------------------------------------------------------- 1 | import org.telegram.telegrambots.ApiContextInitializer; 2 | import org.telegram.telegrambots.TelegramBotsApi; 3 | import org.telegram.telegrambots.exceptions.TelegramApiException; 4 | 5 | public class MainClass { 6 | 7 | public static void main(String[] args) { 8 | 9 | 10 | ApiContextInitializer.init(); 11 | TelegramBotsApi telegramBotsApi = new TelegramBotsApi(); 12 | try { 13 | telegramBotsApi.registerBot(new ZaurEduBot()); 14 | 15 | } catch (TelegramApiException e) { 16 | e.printStackTrace(); 17 | } 18 | 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/ZaurEduBot.java: -------------------------------------------------------------------------------- 1 | import org.telegram.telegrambots.api.methods.send.SendMessage; 2 | import org.telegram.telegrambots.api.objects.Update; 3 | import org.telegram.telegrambots.bots.TelegramLongPollingBot; 4 | import org.telegram.telegrambots.exceptions.TelegramApiException; 5 | 6 | public class ZaurEduBot extends TelegramLongPollingBot { 7 | 8 | 9 | public void onUpdateReceived(Update update) { 10 | 11 | // System.out.println(update.getMessage().getText()); 12 | // System.out.println(update.getMessage().getFrom().getFirstName() ); 13 | 14 | String command=update.getMessage().getText(); 15 | 16 | SendMessage message = new SendMessage(); 17 | 18 | if(command.equals("/myname")){ 19 | 20 | System.out.println(update.getMessage().getFrom().getFirstName()); 21 | 22 | message.setText(update.getMessage().getFrom().getFirstName()); 23 | } 24 | 25 | if (command.equals("/mylastname")){ 26 | 27 | System.out.println(update.getMessage().getFrom().getLastName()); 28 | message.setText(update.getMessage().getFrom().getLastName()); 29 | } 30 | 31 | if (command.equals("/myfullname")){ 32 | System.out.println(update.getMessage().getFrom().getFirstName()+" "+update.getMessage().getFrom().getLastName()); 33 | message.setText(update.getMessage().getFrom().getFirstName()+" "+update.getMessage().getFrom().getLastName()); 34 | } 35 | 36 | message.setChatId(update.getMessage().getChatId()); 37 | 38 | 39 | try { 40 | execute(message); 41 | } catch (TelegramApiException e) { 42 | e.printStackTrace(); 43 | } 44 | 45 | 46 | } 47 | 48 | public String getBotUsername() { 49 | return "zauredu_bot"; 50 | } 51 | 52 | public String getBotToken() { 53 | return "428789405:AAHeQGtTmq34PYdijLUEb16SVgARgLDXPNA"; 54 | } 55 | } 56 | --------------------------------------------------------------------------------