it.netgrid.got.telegram.TelegramConnector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of telegram-connector Show documentation
Show all versions of telegram-connector Show documentation
Bauer's stand-alone connector to the Telegram API
package it.netgrid.got.telegram;
import java.io.IOException;
import it.netgrid.bauer.EventHandler;
import it.netgrid.bauer.Topic;
import it.netgrid.got.telegram.events.ContactMessage;
import it.netgrid.got.telegram.events.ErrorEvent;
import it.netgrid.got.telegram.events.TextMessage;
import it.netgrid.got.telegram.events.TelegramMessage;
import it.netgrid.got.telegram.properties.TelegramPropertiesConfiguration;
import com.google.inject.Inject;
import me.shib.java.lib.jtelebot.models.types.ChatId;
import me.shib.java.lib.jtelebot.models.types.KeyboardButton;
import me.shib.java.lib.jtelebot.models.types.ParseMode;
import me.shib.java.lib.jtelebot.models.types.ReplyKeyboardMarkup;
import me.shib.java.lib.jtelebot.models.types.ReplyMarkup;
import me.shib.java.lib.jtelebot.models.updates.Message;
import me.shib.java.lib.jtelebot.models.updates.Update;
import me.shib.java.lib.jtelebot.service.TelegramBot;
public class TelegramConnector implements Runnable {
private final TelegramBot bot;
private final Topic sendTopic;
private final Topic errorTopic;
private Message message;
private Topic getTopic;
private TelegramPropertiesConfiguration configuration;
@Inject
public TelegramConnector(TelegramBot bot, Topic sendTopic, Topic errorTopic,
Topic getTopic, TelegramPropertiesConfiguration configuration) {
this.bot = bot;
this.sendTopic = sendTopic;
this.errorTopic = errorTopic;
this.getTopic = getTopic;
this.configuration = configuration;
}
@Override
public void run() {
listenOnTopic();
Update[] updates;
TelegramMessage event;
try {
while ((updates = bot.getUpdates()) != null) {
for (Update update : updates) {
message = update.getMessage();
String numb = "";
boolean isnt_phone = false;
try {
numb = message.getContact().getPhone_number();
} catch (NullPointerException e) {
isnt_phone = true;
}
if (isnt_phone) {
event = new TextMessage(message.getChat().getId(), message.getChat().getUsername(),
message.getText(), message.getDate());
sendTopic.post(event);
} else {
event = new ContactMessage(message.getChat().getId(), message.getContact().getUser_id(), numb,
message.getDate());
sendTopic.post(event);
}
}
}
} catch (IOException e) {
errorTopic.post(new ErrorEvent("telegram-connector", e.getMessage()));
e.printStackTrace();
}
}
public void listenOnTopic() {
getTopic.addHandler(new EventHandler() {
@Override
public Class getEventClass() {
return TextMessage.class;
}
@Override
public String getName() {
return configuration.getReceiveTopic();
}
@Override
public boolean handle(TextMessage event) throws Exception {
boolean isContactRequest = false;
if (isContactRequest) {
// TODO aggiungere gestione richieste contatti ad alfred
sendContactRequest();
} else {
try {
send(event.getChat_id(), event.getContent());
} catch (Exception e) {
}
}
return true;
}
});
}
public void send(Long chatId, String response) throws IOException {
bot.sendMessage(new ChatId(chatId), response);
}
public void send(Long chatId, String message, ReplyMarkup rm) throws IOException {
bot.sendMessage(new ChatId(chatId), message, ParseMode.Markdown, false, this.message.getMessage_id(), rm);
}
public void sendContactRequest() throws IOException {
KeyboardButton kb = new KeyboardButton("Inviami il tuo numero");
kb.requestContact();
KeyboardButton[][] kbm = new KeyboardButton[1][1];
kbm[0][0] = kb;
ReplyMarkup rm = new ReplyKeyboardMarkup(kbm, false, true);
send(this.message.getChat().getId(), "Inviami il tuo numero", rm);
}
}