Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Sticcia committed May 18, 2018
0 parents commit 292c0ad
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="/home/sticcia/Documents/TelegramBot/lib/telegrambots-3.6.1-jar-with-dependencies.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>TelegramBot</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Binary file added bin/BiciSocialeBot.class
Binary file not shown.
Binary file added bin/Main.class
Binary file not shown.
Binary file added lib/telegrambots-3.6.1-jar-with-dependencies.jar
Binary file not shown.
176 changes: 176 additions & 0 deletions src/BiciSocialeBot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import org.telegram.telegrambots.api.methods.AnswerCallbackQuery;
import org.telegram.telegrambots.api.methods.send.SendLocation;
import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.CallbackQuery;
import org.telegram.telegrambots.api.objects.Location;
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.exceptions.TelegramApiException;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class BiciSocialeBot extends TelegramLongPollingBot {
private Location bikeLocation;
private boolean taken;

@Override
public void onUpdateReceived(Update update) {
// Check if the update has a message
if (update.hasMessage()) {
String user_first_name = update.getMessage().getChat().getFirstName();
String user_last_name = update.getMessage().getChat().getLastName();
String user_username = update.getMessage().getChat().getUserName();
long user_id = update.getMessage().getChat().getId();
long chat_id = update.getMessage().getChatId();
String message_text;
String answer;

// Check if update has text
if (update.getMessage().hasText() && update.getMessage().isCommand()) {
message_text = update.getMessage().getText();
switch (message_text) {
case "/start":
case "/start@BiciSocialeBot":
answer = "Welcome " + user_username + "!";
this.sendMessage(chat_id, answer);
break;
case "/help":
case "/help@BiciSocialeBot":
answer = "BiciSocialeBot"
+ "\nIf you move the bike please use /update, then send the location as an answer to the message."
+ "\nIf you want to find the bike use /find.";
this.sendMessage(chat_id, answer);
break;
case "/update":
case "/update@BiciSocialeBot":
answer = "Send me the new location as an answer to this message";
this.sendMessage(chat_id, answer);
break;
case "/find":
case "/find@BiciSocialeBot":
if (getBikeLocation() != null) {
answer = "Lat: " + bikeLocation.getLatitude().toString() + " Long: "
+ bikeLocation.getLongitude().toString();
this.sendReplyMarkup(chat_id, bikeLocation, "Take bike", "take");
} else if (this.taken) {
answer = "Bike is already in use";
this.sendMessage(chat_id, answer);
} else {
answer = "The location of the bike is missing";
this.sendMessage(chat_id, answer);
}

break;
default:
answer = "No such command.\nUse /help for help.";
this.sendMessage(chat_id, answer);
break;
}

log(user_first_name, user_last_name, Long.toString(user_id), message_text, answer);

} else if (update.getMessage().hasLocation()) {
this.bikeLocation = update.getMessage().getLocation();
message_text = "Lat: " + bikeLocation.getLatitude().toString() + " Long: "
+ bikeLocation.getLongitude().toString();

answer = "Thanks for sending me the new location.\nUse /find to find the bike at any moment";
this.sendMessage(chat_id, answer);

log(user_first_name, user_last_name, Long.toString(user_id), message_text, answer);
}
}

if (update.hasCallbackQuery()) {
CallbackQuery query = update.getCallbackQuery();
if (query.getData().equals("take")) {
this.taken = true;
this.answerCallback(query.getId(), "Please don't wreck the bike!");
}
}
}

@Override
public String getBotUsername() {
// Return bot username
// If bot username is @MyAmazingBot, it must return 'MyAmazingBot'
return "BiciSocialeBot";
}

@Override
public String getBotToken() {
// Return bot token from BotFather
return "577331603:AAHLutNZ7Brr98TaX4LjlagRULigAq4Vhzw";
}

public Location getBikeLocation() {
return this.bikeLocation;
}

public void sendReplyMarkup(long id, Location location, String button, String callbackData) {
List<List<InlineKeyboardButton>> keyboard = Arrays.asList(Arrays.asList(new InlineKeyboardButton().setText(button).setCallbackData(callbackData)));
InlineKeyboardMarkup markup = new InlineKeyboardMarkup();
markup.setKeyboard(keyboard);
SendLocation s = new SendLocation().setChatId(id);
s.setLatitude(location.getLatitude());
s.setLongitude(location.getLongitude()).setReplyMarkup(markup);

try {
execute(s); // Sending our message object to user
} catch (TelegramApiException e) {
e.printStackTrace();
}
}

public void answerCallback(String id, String message) {
AnswerCallbackQuery s = new AnswerCallbackQuery();
s.setCallbackQueryId(id);
s.setText(message);

try {
execute(s); // Sending our message object to user
} catch (TelegramApiException e) {
e.printStackTrace();
}
}

public void sendMessage(long id, String message) {
SendMessage s = new SendMessage() // Create a message object object
.setChatId(id).setText(message);

try {
execute(s); // Sending our message object to user
} catch (TelegramApiException e) {
e.printStackTrace();
}
}

public void sendLocation(long id, Location location) {
SendLocation s = new SendLocation().setChatId(id);
s.setLatitude(location.getLatitude());
s.setLongitude(location.getLongitude());

try {
execute(s); // Sending our message object to user
} catch (TelegramApiException e) {
e.printStackTrace();
}
}

private void log(String first_name, String last_name, String user_id, String txt, String bot_answer) {
System.out.println("\n ----------------------------");
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));
System.out
.println("Message from " + first_name + " " + last_name + ". (id = " + user_id + ") \n Text - " + txt);
System.out.println("Bot answer: \n Text - " + bot_answer);
}
}
20 changes: 20 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.TelegramBotsApi;
import org.telegram.telegrambots.exceptions.TelegramApiException;

public class Main {
public static void main(String[] args) {
// Initialize Api Context
ApiContextInitializer.init();

// Instantiate Telegram Bots API
TelegramBotsApi botsApi = new TelegramBotsApi();

// Register our bot
try {
botsApi.registerBot(new BiciSocialeBot());
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
}

0 comments on commit 292c0ad

Please sign in to comment.