Browse Source

Initial

pull/1/head
mrbesen 1 year ago
commit
5f7eaaabf2
10 changed files with 313 additions and 0 deletions
  1. +5
    -0
      .gitignore
  2. +32
    -0
      pom.xml
  3. +73
    -0
      src/de/mrbesen/telegram/TelegramAPI.java
  4. +18
    -0
      src/de/mrbesen/telegram/TelegramUser.java
  5. +16
    -0
      src/de/mrbesen/telegram/commands/CommandHandler.java
  6. +62
    -0
      src/de/mrbesen/telegram/commands/CommandManager.java
  7. +17
    -0
      src/de/mrbesen/telegram/event/Event.java
  8. +9
    -0
      src/de/mrbesen/telegram/event/EventHandler.java
  9. +5
    -0
      src/de/mrbesen/telegram/event/EventListener.java
  10. +76
    -0
      src/de/mrbesen/telegram/event/EventManager.java

+ 5
- 0
.gitignore View File

@@ -0,0 +1,5 @@
.classpath
.project
.settings/
target/
bin/

+ 32
- 0
pom.xml View File

@@ -0,0 +1,32 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TelegramAPI</groupId>
<artifactId>TelegramAPI</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source />
<target />
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
</dependencies>
</project>

+ 73
- 0
src/de/mrbesen/telegram/TelegramAPI.java View File

@@ -0,0 +1,73 @@
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;
}
}
}
}

+ 18
- 0
src/de/mrbesen/telegram/TelegramUser.java View File

@@ -0,0 +1,18 @@
package de.mrbesen.telegram;

public class TelegramUser {

private int chatid;
private String uname;
private TelegramAPI api;

public TelegramUser(int chatid, String uname, TelegramAPI api) {
this.chatid = chatid;
this.uname = uname;
this.api = api;
}

public void sendMessage(String msg) {
}
}

+ 16
- 0
src/de/mrbesen/telegram/commands/CommandHandler.java View File

@@ -0,0 +1,16 @@
package de.mrbesen.telegram.commands;

import de.mrbesen.telegram.TelegramUser;

public interface CommandHandler {
/**
* Should return true, if the command is handled correctly, false if the help menu should be displayed
* @param sender
* @param cmd
* @param args
* @return
*/
public boolean onCommand(TelegramUser sender, String cmd, String[] args);

}

+ 62
- 0
src/de/mrbesen/telegram/commands/CommandManager.java View File

@@ -0,0 +1,62 @@
package de.mrbesen.telegram.commands;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

import de.mrbesen.telegram.TelegramUser;

public class CommandManager {
private static String CMDPATTERN = "^[\\w-]+$";
private Multimap<String, CommandHandler> handlerlist = ArrayListMultimap.create();//list of all registered CommandHandler

public CommandManager() { }
void onCommand(String line, TelegramUser sender) {//called by the api (/-prefix allready removed)
line = line.trim();
String[] split = line.split(" ",2);
String cmd = split[0].toLowerCase();
if(cmd.matches(CMDPATTERN)) {
String[] args = new String[0];
if(split.length == 2) {
args = split[1].split(" ");
}
//call
for(CommandHandler cmdhand : handlerlist.get(cmd)) {
try {
boolean result = cmdhand.onCommand(sender, cmd, args);
if(result)
break;
} catch(Throwable t) {
System.err.println("Error, while performing Command. ");
t.printStackTrace();
}
}
} else {
//unknown cmd!
}
}
public void registerCommand(String cmd, CommandHandler handler) {
if(cmd == null) {
throw new NullPointerException("cmd is not allowed to be null");
}
if(handler == null) {
throw new NullPointerException("handler is not allowed to be null");
}
cmd = cmd.trim();
if(cmd.startsWith("/")) {
cmd = cmd.substring(1);
}
if(cmd.isEmpty()) {
throw new RuntimeException("cmd is not allowed to be empty");
}
if(cmd.matches(CMDPATTERN)) {
handlerlist.put(cmd.toLowerCase(), handler);
//registered successfull!
} else {
throw new IllegalArgumentException("cmd contains unallowed characters. Allowed: a-zA-Z0-9_-");
}
}
}

+ 17
- 0
src/de/mrbesen/telegram/event/Event.java View File

@@ -0,0 +1,17 @@
package de.mrbesen.telegram.event;

import de.mrbesen.telegram.TelegramUser;

public class Event {
private TelegramUser user;

public Event(TelegramUser u) {
user = u;
}
public TelegramUser getUser() {
return user;
}

}

+ 9
- 0
src/de/mrbesen/telegram/event/EventHandler.java View File

@@ -0,0 +1,9 @@
package de.mrbesen.telegram.event;

import static java.lang.annotation.ElementType.METHOD;

import java.lang.annotation.Target;

@Target(METHOD)
public @interface EventHandler {
}

+ 5
- 0
src/de/mrbesen/telegram/event/EventListener.java View File

@@ -0,0 +1,5 @@
package de.mrbesen.telegram.event;

public interface EventListener {

}

+ 76
- 0
src/de/mrbesen/telegram/event/EventManager.java View File

@@ -0,0 +1,76 @@
package de.mrbesen.telegram.event;

import java.lang.reflect.Method;
import java.util.ArrayList;

public class EventManager {
private ArrayList<EventListener> listeners = new ArrayList<EventListener>();
private ArrayList<EventMethod> handler = new ArrayList<EventMethod>();
public EventManager() {
}
@SuppressWarnings("unchecked")
public void registerEvent(EventListener listener) {
if(listener == null) {
throw new NullPointerException();
}
if(listeners.contains(listener)) {
throw new RuntimeException("Listener is allready registered.");
}
listeners.add(listener);
//scanning handler
for(Method met : listener.getClass().getMethods()) {
EventHandler h = met.getAnnotation(EventHandler.class);
if(h != null) {//handler annotation vorhanden
if(met.getParameterCount() == 1) {//only one parameter required
Class<?> parameterclass = met.getParameters()[0].getClass();
if(Event.class.isAssignableFrom(parameterclass) && !parameterclass.equals(Event.class)) {//the needed parameter is a Event-subclass)
handler.add(new EventMethod((Class<? extends Event>) parameterclass, met, listener));
} else {
//warn / error?
}
} else {
//warn?
}
}
}
}

public Event callEvent(Event e) {
for(EventMethod met : handler) {
try {
e = met.callEvent(e);
} catch(Throwable t) {
System.err.println("Error while calling Event " + e.getClass().getSimpleName() + " at " + met.listener.getClass().getSimpleName() + ":" + met.met.getName());
t.printStackTrace();
}
}
return e;
}

private class EventMethod {
private EventListener listener;
private Method met;
private Class<? extends Event> eventtype;
private EventMethod(Class<? extends Event> eventtype, Method m, EventListener list) {
this.met = m;
this.eventtype = eventtype;
this.listener = list;
}
private Event callEvent(Event e) {
if(eventtype.equals(e.getClass())) {
try {
met.invoke(listener, e);
} catch(Throwable t) {
t.printStackTrace();
}
}
return e;
}
}
}

Loading…
Cancel
Save