Skip to content

Instantly share code, notes, and snippets.

@ievgiienko
Created June 1, 2023 19:33
Show Gist options
  • Save ievgiienko/3200cf1d96ecd5619a4bd635cc00663d to your computer and use it in GitHub Desktop.
Save ievgiienko/3200cf1d96ecd5619a4bd635cc00663d to your computer and use it in GitHub Desktop.
package org.example;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
public class Main {
public static void main(String[] args) throws Exception {
var api = new TelegramBotsApi(DefaultBotSession.class);
api.registerBot(new MyBot());
}
}
package org.example;
import net.thauvin.erik.crypto.CryptoPrice;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
public class MyBot extends TelegramLongPollingBot {
public MyBot() {
super("тут ваш токен, що зренерував для вас BotFather");
}
@Override
public void onUpdateReceived(Update update) {
var chatId = update.getMessage().getChatId();
var text = update.getMessage().getText();
try {
var message = new SendMessage();
message.setChatId(chatId);
if (text.equals("/start")) {
message.setText("Hello!");
} else if (text.equals("btc")) {
var price = CryptoPrice.spotPrice("BTC");
message.setText("BTC price: " + price.getAmount().doubleValue());
} else if (text.equals("eth")) {
var price = CryptoPrice.spotPrice("ETH");
message.setText("ETH price: " + price.getAmount().doubleValue());
} else {
message.setText("Unknown command!");
}
execute(message);
} catch (Exception e) {
System.out.println("Error!");
}
}
@Override
public String getBotUsername() {
return "тут ваше ім'я бота";
}
}
<dependencies>
<dependency>
<groupId>net.thauvin.erik</groupId>
<artifactId>cryptoprice</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>6.5.0</version>
</dependency>
</dependencies>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment