TelegramAPI/src/de/mrbesen/telegram/TelegramAPI.java

74 lines
1.3 KiB
Java

package de.mrbesen.telegram;
import org.json.JSONObject;
public class TelegramAPI implements Runnable {
private int delay = 1500;
private String apikey;
private Thread thread;
private boolean run = true;
private static final String api_url = "https://api.telegram.org/bot";
public TelegramAPI(String apikey) {
this.apikey = apikey;
}
public void setDelay(int d) {
if(d < 0)
throw new IllegalArgumentException("delay is not allowed to be negative.");
delay = d;
}
public int getDelay() {
return delay;
}
public void start() {
if(thread == null) {
run = true;
thread = new Thread(this, "TelegramAPI");
thread.start();
} else {
throw new IllegalStateException("Still Running.");
}
}
JSONObject request(String request) {
String url = api_url + apikey + "/" + request;
//do https stuff
return null;//error
}
public void stop() {
run = false;
thread.interrupt();
Thread.yield();//try to not get into that loop
while(isRunning()) {
thread.interrupt();
Thread.yield();
try {
Thread.sleep(10);
} catch(InterruptedException e) {}
}
thread = null;
}
public boolean isRunning() {
return thread.isAlive();
}
@Override
public void run() {
while(run) {
//do stuff
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
break;
}
}
}
}