Skip to content

Commit

Permalink
Merge pull request #240 from rwth-acis/develop
Browse files Browse the repository at this point in the history
xAPI update, asynchronous Flag, Callback function
  • Loading branch information
Yinnii authored Apr 17, 2024
2 parents 0b5abbf + dfc3127 commit f376bd8
Show file tree
Hide file tree
Showing 19 changed files with 573 additions and 99 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker-CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Docker Build and Push
on:
push:

branches: [ master, develop, ma-lakhoune]
branches: [ master, develop ]


jobs:
Expand Down
2 changes: 2 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ set_in_service_config mongoAuth ${MONGO_AUTH}
set_in_service_config lrsURL ${LRS_URL}
set_in_service_config lrsURLStatic ${LRS_URL}
set_in_service_config lrsAuthToken ${LRS_AUTH_TOKEN}
set_in_service_config xapiUrl ${XAPI_URL}
set_in_service_config xapiHomepage ${XAPI_HOMEPAGE}

check_if_exists "$WEBCONNECTOR_URL" "WEBCONNECTOR_URL"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ mongoHost = 127.0.0.1:27017
mongoAuth = admin
webconnectorUrl = http://127.0.0.1:8080
restarterBotName =
restarterBotPW =
restarterBotPW =
xapiUrl =
xapiHomepage =

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public ChatMediator(String authToken) {
* replies to it later on.
* @param id An ID for the sent chat message, e.g. to be able to recognize
*/
public abstract Boolean sendMessageToChannel(String channel, String text, HashMap<String, IncomingMessage> hashMap, String type, Optional<String> id);

public abstract Boolean sendMessageToChannel(String channel, String text, HashMap<String, IncomingMessage> hashMap, String type, IncomingMessage currentMessage, Optional<String> id);
public abstract void editMessage(String channel, String messageId, String message, Optional<String> id);

public void editMessage(String channel, String messageId, String message) {
Expand All @@ -64,7 +64,7 @@ public void updateBlocksMessageToChannel(String channel, String blocks, String a
* @param text The content of the chat message
*/
public Boolean sendMessageToChannel(String channel, String text, String type ) {
return sendMessageToChannel(channel, text, null,type);
return sendMessageToChannel(channel, text, null, type, null, null);
}
/**
* Sends a chat message to a channel.
Expand All @@ -74,10 +74,12 @@ public Boolean sendMessageToChannel(String channel, String text, String type ) {
* @param hashMap An ID for the sent chat message, e.g. to be able to recognize
* replies to it later on.
*/
public Boolean sendMessageToChannel(String channel, String text, HashMap<String, IncomingMessage> hashMap, String type) {
return sendMessageToChannel(channel, text, hashMap,type,null);
public Boolean sendMessageToChannel(String channel, String text, HashMap<String, IncomingMessage> hashMap, String type, IncomingMessage currentMessage) {
System.out.println("SEND MESSAGE TO CHANNEL:" + text);

return sendMessageToChannel(channel, text, hashMap, type, currentMessage, null);
}

/**
* Sends a file message to a channel as well as an optional text message.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
public class InteractiveChatElement {
private String intent;
private String label;
private boolean isFile;
private boolean isFile;
private boolean rateable;
// private boolean async;

public InteractiveChatElement(String intent, String label) {
this(intent, label, false);
this(intent, label, false, false);
}

public InteractiveChatElement(String intent, String label, boolean isFile){
public InteractiveChatElement(String intent, String label, boolean isFile, boolean rateable){
this.intent = intent;
this.label = label;
this.isFile = isFile;
this.rateable = rateable;
}

public String getIntent() {
Expand All @@ -35,4 +38,20 @@ public boolean isFile() {
public void setFile(boolean isFile) {
this.isFile = isFile;
}

public boolean isRateable(){
return rateable;
}

public void setRateable(boolean rateable){
this.rateable = rateable;
}

// public boolean isAsync(){
// return async;
// }

// public void setAsync(boolean async){
// this.async = async;
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected String getBotId() throws AuthTokenException{
}

@Override
public Boolean sendMessageToChannel(String channel, String text, HashMap<String, IncomingMessage> hashMap, String type, Optional<String> id) {
public Boolean sendMessageToChannel(String channel, String text, HashMap<String, IncomingMessage> hashMap, String type, IncomingMessage currentMessage, Optional<String> id) {
HashMap<String,String> args = new HashMap<String,String>();
args.put("messages[0][touserid]", channel);
args.put("messages[0][text]", text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public MoodleForumMediator(String authToken) throws IOException, AuthTokenExcept
}

@Override
public Boolean sendMessageToChannel(String channel, String text, HashMap<String, IncomingMessage> hashMap, String type, Optional<String> id) {
public Boolean sendMessageToChannel(String channel, String text, HashMap<String, IncomingMessage> hashMap, String type, IncomingMessage currentMessage, Optional<String> id) {
Boolean messageSent = Boolean.FALSE;
try {
// Get sequence IDs and find origin post
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public RESTfulChatMediator(String authToken) {
}

@Override
public Boolean sendMessageToChannel(String channel, String text, HashMap<String, IncomingMessage> hashMap, String type, Optional<String> id) {
RESTfulChatResponse rcr = new RESTfulChatResponse(text, hashMap,type);
public Boolean sendMessageToChannel(String channel, String text, HashMap<String, IncomingMessage> hashMap, String type, IncomingMessage currentMessage, Optional<String> id) {
RESTfulChatResponse rcr = new RESTfulChatResponse(text, hashMap, type, currentMessage);
chatsession.put(channel, rcr);
return Boolean.TRUE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,42 @@ public class RESTfulChatResponse {
private JSONObject reqBody;
private boolean isFile;
private boolean rateable;
private boolean asynchron;


public RESTfulChatResponse(String text, HashMap<String, IncomingMessage> hashMap, String type) {
public RESTfulChatResponse(String text, HashMap<String, IncomingMessage> hashMap, String type, IncomingMessage currentMessage) {
this(text);
reqBody = new JSONObject();
HashSet<InteractiveChatElement> icel = new HashSet<InteractiveChatElement>();
setType(type);
isFile = false;
asynchron = false;
if(hashMap != null){
for (Entry<String, IncomingMessage> entry : hashMap.entrySet()) {
String key = entry.getKey();
IncomingMessage value = entry.getValue();
String intent = key;
if(intent==null||intent=="") intent = value.getIntentKeyword();
InteractiveChatElement ice = new InteractiveChatElement(intent, value.getIntentLabel(), value.expectsFile());
if(value.expectsFile()){
InteractiveChatElement ice = new InteractiveChatElement(intent, value.getIntentLabel(), value.expectsFile(), value.isRateable());
icel.add(ice);
if(entry.getValue().expectsFile()){
isFile = true;
}
if(value.isRateable()){
rateable = true;
}
icel.add(ice);
// if(entry.getValue().isRateable()){
// rateable = true;
// }
// if(entry.getValue().getAsynchron()){
// asynchron = true;
// }
}
}
if (currentMessage != null) {
if(currentMessage.isRateable()){
rateable = true;
}
if(currentMessage.getAsynchron()) {
asynchron = true;
}
}

interactiveElements = Arrays.asList(icel.toArray());
}

Expand Down Expand Up @@ -104,4 +115,13 @@ public boolean isRateable() {
public void setRateable(boolean rateable) {
this.rateable = rateable;
}

public boolean asynchron() {
return asynchron;
}

public void setAsynchron(boolean asynchron) {
this.asynchron = asynchron;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.json.JSONArray;
import org.json.JSONObject;

import com.google.gson.JsonObject;
import com.rocketchat.common.data.lightdb.document.UserDocument;
import com.rocketchat.common.data.model.ErrorObject;
import com.rocketchat.common.data.model.UserObject;
Expand Down Expand Up @@ -58,6 +59,9 @@
import i5.las2peer.services.socialBotManagerService.database.SQLDatabase;
import i5.las2peer.services.socialBotManagerService.model.IncomingMessage;
import i5.las2peer.services.socialBotManagerService.nlu.RasaNlu;
import io.swagger.util.Json;
import net.minidev.json.parser.JSONParser;
import net.minidev.json.parser.ParseException;

public class RocketChatMediator extends ChatMediator implements ConnectListener, LoginListener,
RoomListener.GetRoomListener, SubscribeListener, GetSubscriptionListener, SubscriptionListener {
Expand Down Expand Up @@ -98,6 +102,8 @@ public RocketChatMediator(String authToken, SQLDatabase database, RasaNlu rasa)
r = clientLoginTest.sendRequest("POST", "", reqBody.toString(), MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON, headers);
if(r.getHttpCode() != 200){
throw new AuthTokenException("Authentication Token is faulty!");
} else if (r.getHttpCode() == 200) {
System.out.println("Login successful");
}

RocketChatAPI.LOGGER.setLevel(Level.OFF);
Expand All @@ -117,6 +123,19 @@ public RocketChatMediator(String authToken, SQLDatabase database) {
client.setReconnectionStrategy(new ReconnectionStrategy(4, 2000));
client.setPingInterval(15000);
client.connect(this);
// MiniClient clientLoginTest = new MiniClient();
// clientLoginTest.setConnectorEndpoint(url + "/api/v1/login");
// HashMap<String, String> headers = new HashMap<String, String>();
// ClientResponse r = null;
// JSONObject reqBody = new JSONObject();
// reqBody.put("username", username);
// reqBody.put("password", password);
// r = clientLoginTest.sendRequest("POST", "", reqBody.toString(), MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON, headers);
// if(r.getHttpCode() != 200){
// System.out.println("Authentication Token is faulty!");
// } else if (r.getHttpCode() == 200) {
// System.out.println("Login successful");
// }
RocketChatAPI.LOGGER.setLevel(Level.OFF);
messageCollector.setDomain(url);
//conversationPathCollector.setDomain(url);
Expand Down Expand Up @@ -185,7 +204,7 @@ public void sendFileMessageToChannel(String channel, String fileBody, String fil
}

@Override
public Boolean sendMessageToChannel(String channel, String text, HashMap<String, IncomingMessage> hashMap, String type, Optional<String> id) {
public Boolean sendMessageToChannel(String channel, String text, HashMap<String, IncomingMessage> hashMap, String type, IncomingMessage currentMessage, Optional<String> id) {
System.out.println(text);
ChatRoom room = client.getChatRoomFactory().getChatRoomById(channel);
Boolean messageSent = Boolean.FALSE;
Expand Down Expand Up @@ -279,6 +298,50 @@ public void onUploadComplete(int arg0, com.rocketchat.core.model.FileObject arg1
return messageSent;
}

public void sendMessageToChannelCallback(String channel, String text, String type) {
System.out.println(text);

JSONObject request = new JSONObject();
JSONObject response = new JSONObject();
request.put("rid", channel);
request.put("msg", text);
MiniClient clientLogin = new MiniClient();

clientLogin.setConnectorEndpoint(url + "/api/v1/login");
HashMap<String, String> headers = new HashMap<String, String>();
ClientResponse r = null;
JSONObject reqBody = new JSONObject();
reqBody.put("username", username);
reqBody.put("password", password);
System.out.println("Log into rocketchat to get auth token.");
r = clientLogin.sendRequest("POST", "", reqBody.toString(), MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON, headers);
if(r.getHttpCode() != 200){
System.out.println(r.getHttpCode());
System.out.println("Authentication Token R is faulty!");
} else if (r.getHttpCode() == 200) {
response = new JSONObject(r.getResponse());
System.out.println(response);
System.out.println("Login successful");
}

clientLogin.setConnectorEndpoint(url + "/api/v1/chat.sendMessage");
ClientResponse r1 = null;
JSONObject reqBodyMessage = new JSONObject();
JSONObject data = response.getJSONObject("data");
headers.put("X-User-Id", data.getString("userId"));
headers.put("X-Auth-Token", data.getString("authToken"));
System.out.println(data.getString("userId"));
System.out.println(data.getString("authToken"));
reqBodyMessage.put("message", request);
r1 = clientLogin.sendRequest("POST", "", reqBodyMessage.toString(), MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON, headers);
if(r1.getHttpCode() != 200){
System.out.println(r1.getHttpCode());
System.out.println("Authentication Token R1 is faulty!");
} else if (r1.getHttpCode() == 200) {
System.out.println("Message sent.");
}
}

@Override
public Vector<ChatMessage> getMessages() {
Vector<ChatMessage> messages = this.messageCollector.getMessages();
Expand All @@ -294,6 +357,7 @@ public Vector<ChatMessage> getMessages() {
@Override
public String getChannelByEmail(String email) {
List<UserDocument> users = client.getDbManager().getUserCollection().getData();
System.out.println(users.toString());
for (UserDocument u : users) {
// TODO Email Matching
return u.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void updateBlocksMessageToChannel(String channel, String blocks, String a
}

@Override
public Boolean sendMessageToChannel(String channel, String text, HashMap<String, IncomingMessage> hashMap, String type, Optional<String> id) {
public Boolean sendMessageToChannel(String channel, String text, HashMap<String, IncomingMessage> hashMap, String type, IncomingMessage currentMessage, Optional<String> id) {
MessageBuilder msg = Message.builder().id(System.currentTimeMillis()).channel(channel).text(text);
Boolean messageSent = Boolean.FALSE;
if (id.isPresent()) {
Expand All @@ -162,7 +162,7 @@ public Boolean sendMessageToChannel(String channel, String text, HashMap<String,
rtm.sendMessage(message);
System.out.println("Sent message with Exception: " + e.getMessage());
if (e.getMessage().toLowerCase().equals("timeout")) {
sendMessageToChannel(channel, text, null, "text", id);
sendMessageToChannel(channel, text, null, "text", currentMessage, id);
}
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@
import com.google.gson.JsonParser;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.model.File;
import com.pengrad.telegrambot.model.request.ChatAction;
import com.pengrad.telegrambot.model.request.ReplyKeyboardRemove;
import com.pengrad.telegrambot.request.GetFile;
import com.pengrad.telegrambot.request.GetMe;
import com.pengrad.telegrambot.request.SendChatAction;
import com.pengrad.telegrambot.request.SendDocument;
import com.pengrad.telegrambot.request.SendMessage;
import com.pengrad.telegrambot.response.BaseResponse;
import com.pengrad.telegrambot.response.GetFileResponse;
import com.pengrad.telegrambot.response.GetMeResponse;
Expand Down Expand Up @@ -199,7 +192,7 @@ public String getBotName() {
* Sends a plain text message to telegram messenger channel
*/
@Override
public Boolean sendMessageToChannel(String channel, String text, HashMap<String, IncomingMessage> hashMap, String type, Optional<String> id) {
public Boolean sendMessageToChannel(String channel, String text, HashMap<String, IncomingMessage> hashMap, String type, IncomingMessage currentMessage, Optional<String> id) {

System.out.println("send plain message to telegram channel " + channel + ", size: " + text.length());
assert channel != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void handleEvent(JSONObject parsedEvent) {
* @param id
*/
@Override
public Boolean sendMessageToChannel(String channel, String text, HashMap<String, IncomingMessage> hashMap, String type, Optional<String> id) {
public Boolean sendMessageToChannel(String channel, String text, HashMap<String, IncomingMessage> hashMap, String type, IncomingMessage currentMessage,Optional<String> id) {
String repositoryFullName = channel.split("#")[0];
int number = Integer.parseInt(channel.split("#")[1]);
Boolean messageSent = Boolean.FALSE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public ChatStatement() {
context = new xAPIContext();
}

public static ChatStatement generate(String from, String to, String text, String time, String platform) {
public static ChatStatement generate(String from, String to, String text, String time, String platform, String xapiUrl) {
ChatStatement chatStatement = new ChatStatement();
// actor
chatStatement.getActor().setName(from);
Expand All @@ -54,7 +54,7 @@ public static ChatStatement generate(String from, String to, String text, String
chatStatement.getActor().setObjectType("Agent");
// verb
chatStatement.getVerb().getDisplay().setEnEN("messaged");
chatStatement.getVerb().setId("https://tech4comp.de/xapi/verb/messaged");
chatStatement.getVerb().setId(xapiUrl + "/definitions/chat/verbs/sent");
// object
chatStatement.getObject().setName(to);
chatStatement.getObject().getAccount().setName(to);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class xAPIExtension {

@SerializedName("https://tech4comp.de/xapi/context/extensions/messageInfo")
@SerializedName("https://xapi.tech4comp.dbis.rwth-aachen.de/definitions/chat/activities/message")
private xAPIMessageInfo messageInfo;

public xAPIExtension() {
Expand Down
Loading

0 comments on commit f376bd8

Please sign in to comment.