Browse Source

1. Moved transifex bot

master
Rubenlagus 10 years ago
parent
commit
ef9bb72d58
  1. 44
      src/main/java/org/telegram/updateshandlers/TransifexHandlers.java
  2. 3
      src/main/resources/localisation/strings.properties

44
src/main/java/org/telegram/updateshandlers/TransifexHandlers.java

@ -1,6 +1,7 @@
package org.telegram.updateshandlers; package org.telegram.updateshandlers;
import org.telegram.BotConfig; import org.telegram.BotConfig;
import org.telegram.BuildVars;
import org.telegram.Commands; import org.telegram.Commands;
import org.telegram.database.DatabaseManager; import org.telegram.database.DatabaseManager;
import org.telegram.services.LocalisationService; import org.telegram.services.LocalisationService;
@ -10,10 +11,14 @@ import org.telegram.telegrambots.api.methods.send.SendDocument;
import org.telegram.telegrambots.api.methods.send.SendMessage; import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Message; import org.telegram.telegrambots.api.objects.Message;
import org.telegram.telegrambots.api.objects.Update; import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.api.objects.replykeyboard.buttons.InlineKeyboardButton;
import org.telegram.telegrambots.bots.TelegramLongPollingBot; import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.logging.BotLogger; import org.telegram.telegrambots.logging.BotLogger;
import java.io.InvalidObjectException; import java.io.InvalidObjectException;
import java.util.ArrayList;
import java.util.List;
/** /**
* @author Ruben Bermudez * @author Ruben Bermudez
@ -32,21 +37,30 @@ public class TransifexHandlers extends TelegramLongPollingBot {
@Override @Override
public void onUpdateReceived(Update update) { public void onUpdateReceived(Update update) {
try { try {
sendTransifexFile(update); handleUpdate(update);
} catch (Exception e) { } catch (Throwable e) {
BotLogger.error(LOGTAG, e); BotLogger.error(LOGTAG, e);
} }
} }
private void handleUpdate(Update update) throws InvalidObjectException, TelegramApiException {
if (update.hasMessage() && update.getMessage().hasText()) {
Message message = update.getMessage();
if (BuildVars.ADMINS.contains(message.getFrom().getId())) {
sendTransifexFile(message);
} else {
sendMovedToMessage(message);
}
}
}
@Override @Override
public String getBotUsername() { public String getBotUsername() {
return BotConfig.TRANSIFEX_USER; return BotConfig.TRANSIFEX_USER;
} }
private void sendTransifexFile(Update update) throws InvalidObjectException { private void sendTransifexFile(Message message) throws InvalidObjectException {
Message message = update.getMessage(); String language = DatabaseManager.getInstance().getUserLanguage(message.getFrom().getId());
if (message != null && message.hasText()) {
String language = DatabaseManager.getInstance().getUserLanguage(update.getMessage().getFrom().getId());
String text = message.getText(); String text = message.getText();
String[] parts = text.split(" ", 2); String[] parts = text.split(" ", 2);
SendDocument sendDocument = null; SendDocument sendDocument = null;
@ -106,5 +120,23 @@ public class TransifexHandlers extends TelegramLongPollingBot {
} }
} }
} }
private void sendMovedToMessage(Message message) throws InvalidObjectException, TelegramApiException {
String language = DatabaseManager.getInstance().getUserLanguage(message.getFrom().getId());
SendMessage answer = new SendMessage();
answer.setChatId(message.getChatId().toString());
answer.setReplyToMessageId(message.getMessageId());
answer.setText(LocalisationService.getInstance().getString("movedToLangBot", language));
InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
List<List<InlineKeyboardButton>> rows = new ArrayList<>();
List<InlineKeyboardButton> row = new ArrayList<>();
InlineKeyboardButton button = new InlineKeyboardButton();
button.setText(LocalisationService.getInstance().getString("checkLangBot", language));
button.setUrl("https://telegram.me/langbot");
row.add(button);
rows.add(row);
inlineKeyboardMarkup.setKeyboard(rows);
answer.setReplyMarkup(inlineKeyboardMarkup);
sendMessage(answer);
} }
} }

3
src/main/resources/localisation/strings.properties

@ -20,7 +20,8 @@ directionsNotFound= Directions not found between %s and %s.
errorFetchingDirections= Error fetching directions info errorFetchingDirections= Error fetching directions info
directionsStep= %s during %s (%s) directionsStep= %s during %s (%s)
languageModified= Your language setting has been updated. languageModified= Your language setting has been updated.
movedToLangBot=This bot has been moved to @langbot, enjoy it!
checkLangBot=Check @langbot
helpWeatherMessage= Curious about the weather?\ helpWeatherMessage= Curious about the weather?\
\nJust send me these commands and you'll know a lot better\:\ \nJust send me these commands and you'll know a lot better\:\

Loading…
Cancel
Save