Enter a word";
break;
case "ru":
text += "Введите слово";
break;
case "tr":
text += "Bir kelime girin";
break;
}
sendMessage.setText(text);
sendMessage.setParseMode("HTML");
executeMsg(sendMessage,message.getChatId());
}
private void translator(Message message) {
SendMessage sendMessage;
if(userState.containsKey(message.getChatId())) {
if (userState.get(message.getChatId()).equals("langFrom")) {
sendMessage = new SendMessage();
sendMessage.setText("Translator\nChoose From language");
sendMessage.setParseMode("HTML");
sendMessage.setReplyMarkup(db.getInlineKeyboard(Arrays.asList(new String[]{
"\uD83C\uDFF4\uDB40\uDC67\uDB40\uDC62\uDB40\uDC65\uDB40\uDC6E\uDB40\uDC67\uDB40\uDC7F English",
"\uD83C\uDDF9\uD83C\uDDF7 türk",
"\uD83C\uDDF7\uD83C\uDDFA Русский"}),
Arrays.asList(new String[]{"en", "tr", "ru"})));
executeMsg(sendMessage, message.getChatId());
}
}
}
private void start(Message message) {
SendMessage sendMessage = new SendMessage();
sendMessage.setText("Assalamu Alaykum Translator botga hush kelibsiz");
sendMessage.setParseMode("HTML");
sendMessage.setReplyMarkup(db.getReplyKeyboard(Arrays.asList(new String[]{"\uD83D\uDD01 Choose language", "\uD83D\uDCC3 About program"})));
executeMsg(sendMessage,message.getChatId());
}
private void executeMsg(SendMessage sendMessage, Long chatId) {
sendMessage.setChatId(chatId.toString());
try {
execute(sendMessage);
} catch (TelegramApiException e) {
throw new RuntimeException(e);
}
}
@Override
public String getBotUsername() {
return "";
}
@Override
public String getBotToken() {
return "";
}
}
package service;
import model.Word;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardButton;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Database {
private static Database db = new Database();
public static Database getDB(){
return db;
}
public String from;
public String to;
public InlineKeyboardMarkup chooseLangMarkup = getInlineKeyboard(Arrays.asList(new String[]{
"\uD83C\uDFF4\uDB40\uDC67\uDB40\uDC62\uDB40\uDC65\uDB40\uDC6E\uDB40\uDC67\uDB40\uDC7F English",
"\uD83C\uDDF9\uD83C\uDDF7 türk",
"\uD83C\uDDF7\uD83C\uDDFA Русский"}),
Arrays.asList(new String[]{"en", "tr", "ru"}));
public InlineKeyboardMarkup getInlineKeyboard(List names, List callback) {
InlineKeyboardButton button;
List row = new ArrayList<>();
List> keyboard = new ArrayList<>();
int k = 0;
for (String name : names) {
button = new InlineKeyboardButton();
button.setText(name);
button.setCallbackData(callback.get(k++));
row.add(button);
if(k%2==0) {
keyboard.add(row);
row = new ArrayList<>();
}
if(k==callback.size()){
keyboard.add(row);
}
}
InlineKeyboardMarkup markup = new InlineKeyboardMarkup();
markup.setKeyboard(keyboard);
return markup;
}
public ReplyKeyboardMarkup getReplyKeyboard(List names) {
KeyboardButton button ;
KeyboardRow row = new KeyboardRow();
List keyboard = new ArrayList<>();
int k=0;
for (String name : names) {
button = new KeyboardButton();
button.setText(name);
k++;
row.add(button);
if(k%2==0) {
keyboard.add(row);
row = new KeyboardRow();
}
if(k==names.size()){
keyboard.add(row);
}
}
ReplyKeyboardMarkup markup = new ReplyKeyboardMarkup();
markup.setSelective(true);
markup.setOneTimeKeyboard(false);
markup.setResizeKeyboard(true);
markup.setKeyboard(keyboard);
return markup;
}
public Word createWord(String line) {
Word word = new Word();
String s = ""; int fir = 0,last = 0;
System.out.println("working");
if(line.contains("def\":[{\"text\":\"")){
fir = line.indexOf("def\":[{\"text\":\"");
last = line.indexOf("\",\"pos\":\"");
System.out.println(fir+ " " + last);
s=line.substring(fir+15,last);
word.setText(s);
System.out.println(line);
line = line.substring(last+3);
System.out.println("cut: " + line);
}
if(line.contains("pos\":\"")){
fir = line.indexOf("pos\":\"");
last = line.indexOf("\",\"ts\"");
System.out.println(fir+ " " + last);
s=line.substring(fir+6,last);
word.setPos(s);
line = line.substring(last+3);
}
if(line.contains("tr\":[{\"text\":\"")){
fir = line.indexOf("text\":\"");
last = line.indexOf("\",\"pos");
System.out.println(fir+ " " + last);
s=line.substring(fir+7,last);
word.setTr(s);
line = line.substring(last+5);
}
LocalDateTime localDateTime = LocalDateTime.now();
word.setTime(localDateTime.toString());
return word;
}
}
Do'stlaringiz bilan baham: |