Last active
November 29, 2023 08:47
-
-
Save ievgiienko/17a1cb5d9c376e0859c2c3795c1508d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.methods.send.SendPhoto; | |
import org.telegram.telegrambots.meta.api.objects.InputFile; | |
import org.telegram.telegrambots.meta.api.objects.Update; | |
public class MyBot extends TelegramLongPollingBot { | |
public MyBot() { | |
super("ключ бота"); | |
} | |
@Override | |
public void onUpdateReceived(Update update) { | |
var chatId = update.getMessage().getChatId(); | |
var text = update.getMessage().getText(); | |
try { | |
if (text.equals("/start")) { | |
sendMessage(chatId, "Hello!"); | |
} else if (text.equals("btc")) { | |
sendPicture(chatId, "Bitcoin.png"); | |
sendPrice(chatId, "BTC"); | |
} else if (text.equals("eth")) { | |
sendPrice(chatId, "ETH"); | |
} else { | |
sendMessage(chatId, "Unknown command!"); | |
} | |
} catch (Exception e) { | |
System.out.println("Error!"); | |
} | |
} | |
void sendPrice(long chatId, String name) throws Exception { | |
var price = CryptoPrice.spotPrice(name); | |
sendMessage(chatId, name + " price: " + price.getAmount().doubleValue()); | |
} | |
void sendPicture(long chatId, String name) throws Exception { | |
var photo = getClass().getClassLoader().getResourceAsStream(name); | |
var message = new SendPhoto(); | |
message.setChatId(chatId); | |
message.setPhoto(new InputFile(photo, name)); | |
execute(message); | |
} | |
void sendMessage(long chatId, String text) throws Exception { | |
var message = new SendMessage(); | |
message.setChatId(chatId); | |
message.setText(text); | |
execute(message); | |
} | |
@Override | |
public String getBotUsername() { | |
return "it_start_2023_bot"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment