Skip to content

Commit e2cb145

Browse files
Merge pull request #8 from rwth-acis/releases/1.1.0
Releases/1.1.0
2 parents 8670179 + 99e099e commit e2cb145

File tree

6 files changed

+162
-56
lines changed

6 files changed

+162
-56
lines changed

.github/workflows/docker-CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Docker Build and Push
22

33
on:
44
push:
5-
branches: [ master, develop , unnamed-module-fix ]
5+
branches: [ master, develop ]
66

77
jobs:
88
test:

README.md

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ This service can be used to conduct surveys from LimeSurvey with bots created by
44

55
Build
66
--------
7+
78
Execute the following command on your shell:
89

910
```shell
10-
gradle clean build
11+
gradle build
1112
```
1213

14+
Make sure the ``shs`` database exists. If not, create a new database with the same name
1315
Start
1416
--------
1517

@@ -22,17 +24,14 @@ bin/start_network.bat
2224
```
2325

2426
Unix/Mac:
27+
2528
```shell
2629
bin/start_network.sh
2730
```
2831

29-
30-
31-
32-
# Starting a survey bot on slack:
32+
# Starting a survey bot on slack
3333

3434
### Creating a slack app
35-
3635

3736
To create a survey chatbot, first a slack app needs to be created. Creating a classic bot app is possible [here](https://api.slack.com/apps?new_classic_app=1). (Wait for the app creation window to pop up, do not click on the green "Create New App" button).
3837
Since the las2peersocial-bot-manager-service uses RTM, a classic app, instead of a new app, is needed.
@@ -46,30 +45,27 @@ Since the las2peersocial-bot-manager-service uses RTM, a classic app, instead of
4645
- channels:read
4746

4847
- chat:write:bot
49-
48+
5049
- bot
51-
50+
5251
- users:read.email (users:read included)
5352

5453
- incoming-webhook should already be there. If not add under basic information: incoming webhooks: activate incoming webhooks sliding button
5554

5655
- Please do not update scopes!
57-
56+
5857
Now install the app to your wished workspace. After this a token will be generated which is used in the redirect url.
5958

6059
1. Find the bot token: On the left side: OAuth and Permissons, the bot user oauth token (starting with xoxb).
61-
60+
6261
2. Activate interactive components (on the left side: Basic Information: Add features and functionality, Interactive Components. After activating this feature, a Request URL is needed.)
6362

6463
3. Configuring the request url:
6564

6665
- The ip address and port where slack posts the request (the address from the sbfmanager), slack app token, the bot name from the frontend, the instance name from the frontend and the buttonintent text are needed.
67-
http://ipAddress:port/SBFManager/bots/botName/appRequestURL/instanceName/buttonIntent/token.
68-
66+
<http://ipAddress:port/SBFManager/bots/botName/appRequestURL/instanceName/buttonIntent/token>.
6967

70-
71-
72-
### Frontend modeling:
68+
### Frontend modeling
7369

7470
1. Create a bot model by following the guide [here](https://github.com/rwth-acis/Social-Bot-Framework).
7571

@@ -85,24 +81,24 @@ Now install the app to your wished workspace. After this a token will be generat
8581

8682
- The bot actions needs the following action parameters: (all Content Type: String, Static and Parameter Type: body)
8783

88-
* Name: NameOfUser and Content: the username of your "https://limesurvey.tech4comp.dbis.rwth-aachen.de/" account;
89-
* Name: Password and Content: the password of your limesurvey account;
90-
* Name: surveyID and Content: The surveyID of the survey you want to conduct. You can find this by logging into your limesurvey account and then clicking on "List surveys". In the survey list you will see on the very left side the Survey ID.
91-
* Name: buttonIntent and Content: The button intent, which is going to be recognized when participants click on answer buttons
92-
* Name: sbfmUrl and Content: The URL where the social-bot-framework-manager can receive requests
93-
* Name: slackToken and Content: The slack token that has been added to the "Messenger" with type "Slack" as "Authentication Token"
94-
* Name: adminmail and Content: the email of the admin user (Content Type: String and Parameter Type: body)
84+
- Name: NameOfUser and Content: the username of your "https://limesurvey.tech4comp.dbis.rwth-aachen.de/" account;
85+
- Name: Password and Content: the password of your limesurvey account;
86+
- Name: surveyID and Content: The surveyID of the survey you want to conduct. You can find this by logging into your limesurvey account and then clicking on "List surveys". In the survey list you will see on the very left side the Survey ID.
87+
- Name: buttonIntent and Content: The button intent, which is going to be recognized when participants click on answer buttons
88+
- Name: sbfmUrl and Content: The URL where the social-bot-framework-manager can receive requests
89+
- Name: slackToken and Content: The slack token that has been added to the "Messenger" with type "Slack" as "Authentication Token"
90+
- Name: adminmail and Content: the email of the admin user (Content Type: String and Parameter Type: body)
9591

9692
4. It is possible to add routines for sending reminders or sending results to LimeSurvey.
9793

9894
- for routine: Action Type: Service, Function Name: reminderRoutine.
99-
95+
10096
- for LimeSurvey results: Action Type: Service, Function Name:sendResultsToLimesurvey.
10197

10298
- for MobSOS surveys results: Action Type: Service, Function Name:sendResultsToMobsosSurveys.
10399

104100
- both of them need the action parameters from the main bot actions
105101

106-
### Creating a LimeSurvey Survey:
102+
### Creating a LimeSurvey Survey
107103

108104
-enable under Participants settings: "Allow multiple responses or update responses with one token."

app/src/main/java/i5/las2peer/services/SurveyHandler/SurveyHandlerService.java

Lines changed: 129 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,115 @@ public Response getTemplate() {
265265
return Response.ok().entity(name).build();
266266
}
267267

268+
@POST
269+
@Path("/surveys")
270+
@Produces(MediaType.APPLICATION_JSON)
271+
@ApiOperation(value = "REPLACE THIS WITH AN APPROPRIATE FUNCTION NAME", notes = "REPLACE THIS WITH YOUR NOTES TO THE FUNCTION")
272+
@ApiResponses(value = {
273+
@ApiResponse(code = HttpURLConnection.HTTP_OK, message = "REPLACE THIS WITH YOUR OK MESSAGE") })
274+
public Response getSurveys(String input) {
275+
Context.get().monitorEvent(MonitoringEvent.MESSAGE_RECEIVED, input);
276+
277+
JSONObject response = new JSONObject();
278+
JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);
279+
280+
JSONArray completeReturnJSON = new JSONArray();
281+
282+
try {
283+
JSONObject bodyInput = (JSONObject) p.parse(input);
284+
System.out.println("received message: " + bodyInput);
285+
286+
String url = bodyInput.getAsString("url");
287+
String loginName = bodyInput.getAsString("loginName");
288+
String loginPassword = bodyInput.getAsString("loginPassword");
289+
290+
MiniClient mini = new MiniClient();
291+
mini.setConnectorEndpoint(url);
292+
HashMap<String, String> head = new HashMap<String, String>();
293+
294+
ClientResponse miniClientResponse = mini.sendRequest("POST", url,
295+
("{\"method\": \"get_session_key\", \"params\": [ \"" + loginName + "\", \"" + loginPassword
296+
+ "\"], \"id\": 1}"),
297+
MediaType.APPLICATION_JSON, "", head);
298+
if (miniClientResponse.getHttpCode() != 200) {
299+
System.out.println("Error: " + miniClientResponse.getHttpCode());
300+
return Response.status(miniClientResponse.getHttpCode()).build();
301+
302+
}
303+
JSONObject miniresJSON = (JSONObject) p.parse(miniClientResponse.getResponse());
304+
String sessionKeyString = miniresJSON.getAsString("result");
305+
306+
ClientResponse clientResponseQuestionInfo = mini.sendRequest("POST", url,
307+
("{\"method\": \"list_surveys\", \"params\": [ \"" + sessionKeyString + "\"], \"id\": 1}"),
308+
MediaType.APPLICATION_JSON, "", head);
309+
JSONObject res = (JSONObject) p.parse(clientResponseQuestionInfo.getResponse());
310+
return Response.ok().entity(res.toJSONString()).build();
311+
312+
} catch (Exception e) {
313+
e.printStackTrace();
314+
}
315+
316+
response.put("text", completeReturnJSON);
317+
Context.get().monitorEvent(MonitoringEvent.RESPONSE_SENDING.toString());
318+
return Response.ok().entity(response).build();
319+
}
320+
321+
@POST
322+
@Path("/limeSurveyFunction")
323+
@Produces(MediaType.APPLICATION_JSON)
324+
@ApiOperation(value = "REPLACE THIS WITH AN APPROPRIATE FUNCTION NAME", notes = "REPLACE THIS WITH YOUR NOTES TO THE FUNCTION")
325+
@ApiResponses(value = {
326+
@ApiResponse(code = HttpURLConnection.HTTP_OK, message = "REPLACE THIS WITH YOUR OK MESSAGE") })
327+
public Response runLimesurveyFunction(String input) {
328+
Context.get().monitorEvent(MonitoringEvent.MESSAGE_RECEIVED, input);
329+
330+
JSONObject response = new JSONObject();
331+
JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);
332+
333+
JSONArray completeReturnJSON = new JSONArray();
334+
335+
try {
336+
JSONObject bodyInput = (JSONObject) p.parse(input);
337+
System.out.println("received message: " + bodyInput);
338+
339+
String url = bodyInput.getAsString("url");
340+
String loginName = bodyInput.getAsString("loginName");
341+
String loginPassword = bodyInput.getAsString("loginPassword");
342+
String command = bodyInput.getAsString("command");
343+
JSONArray params = (JSONArray) bodyInput.get("params");
344+
345+
MiniClient mini = new MiniClient();
346+
mini.setConnectorEndpoint(url);
347+
HashMap<String, String> head = new HashMap<String, String>();
348+
349+
ClientResponse miniClientResponse = mini.sendRequest("POST", url,
350+
("{\"method\": \"get_session_key\", \"params\": [ \"" + loginName + "\", \"" + loginPassword
351+
+ "\"], \"id\": 1}"),
352+
MediaType.APPLICATION_JSON, "", head);
353+
JSONObject miniresJSON = (JSONObject) p.parse(miniClientResponse.getResponse());
354+
String sessionKeyString = miniresJSON.getAsString("result");
355+
params.add(0, sessionKeyString); // add session key to params
356+
357+
JSONObject request = new JSONObject();
358+
request.put("method", command);
359+
request.put("params", params);
360+
request.put("id", 1);
361+
362+
ClientResponse clientResponseQuestionInfo = mini.sendRequest("POST", url,
363+
(request.toJSONString()),
364+
MediaType.APPLICATION_JSON, "", head);
365+
JSONObject res = (JSONObject) p.parse(clientResponseQuestionInfo.getResponse());
366+
return Response.ok().entity(res.toJSONString()).build();
367+
368+
} catch (Exception e) {
369+
e.printStackTrace();
370+
}
371+
372+
response.put("text", completeReturnJSON);
373+
Context.get().monitorEvent(MonitoringEvent.RESPONSE_SENDING.toString());
374+
return Response.ok().entity(response).build();
375+
}
376+
268377
@POST
269378
@Path("/post/{input}")
270379
@Produces(MediaType.TEXT_PLAIN)
@@ -282,8 +391,7 @@ public Response postTemplate(@PathParam("input") String myInput) {
282391
}
283392

284393
@POST
285-
@Path("/getLimeSurveyResponses")
286-
@Consumes(MediaType.TEXT_PLAIN)
394+
@Path("/responses")
287395
@Produces(MediaType.APPLICATION_JSON)
288396
@ApiOperation(
289397
value = "Get results from LimeSurvey.",
@@ -314,6 +422,10 @@ public Response limesurveyConnector(String input) {
314422
String surveyID = bodyInput.getAsString("surveyID");
315423
String documentType = "json";
316424

425+
if (surveyID == null) {
426+
return Response.status(HttpURLConnection.HTTP_BAD_REQUEST).entity("surveyID is null").build();
427+
}
428+
317429
MiniClient mini = new MiniClient();
318430
mini.setConnectorEndpoint(url);
319431
HashMap<String, String> head = new HashMap<String, String>();
@@ -323,12 +435,25 @@ public Response limesurveyConnector(String input) {
323435
String sessionKeyString = miniresJSON.getAsString("result");
324436

325437
// now get question information, to add to response json object
326-
ClientResponse clientResponseQuestionInfo = mini.sendRequest("POST", url, ("{\"method\": \"list_questions\", \"params\": [ \"" + sessionKeyString + "\", \"" + surveyID + "\"], \"id\": 1}"), MediaType.APPLICATION_JSON, "", head);
438+
ClientResponse clientResponseQuestionInfo = mini
439+
.sendRequest(
440+
"POST", url, ("{\"method\": \"list_questions\", \"params\": [ \"" + sessionKeyString
441+
+ "\", \"" + surveyID + "\"], \"id\": 1}"),
442+
MediaType.APPLICATION_JSON, "", head);
443+
444+
if (clientResponseQuestionInfo.getHttpCode() != 200) {
445+
return Response.status(clientResponseQuestionInfo.getHttpCode())
446+
.entity(clientResponseQuestionInfo.getResponse()).build();
447+
}
448+
327449
JSONObject questionInfoJSON = (JSONObject) p.parse(clientResponseQuestionInfo.getResponse());
328450
String questionInfo = questionInfoJSON.getAsString("result");
329451
JSONArray questionListJSON = (JSONArray) p.parse(questionInfo);
330452

331-
ClientResponse clientResponse = mini.sendRequest("POST", url, ("{\"method\": \"export_responses\", \"params\": [ \"" + sessionKeyString + "\", \"" + surveyID + "\", \"" + documentType + "\"], \"id\": 1}"), MediaType.APPLICATION_JSON, "", head);
453+
ClientResponse clientResponse = mini.sendRequest(
454+
"POST", url, ("{\"method\": \"export_responses\", \"params\": [ \"" + sessionKeyString + "\", \""
455+
+ surveyID + "\", \"" + documentType + "\"], \"id\": 1}"),
456+
MediaType.APPLICATION_JSON, "", head);
332457
JSONObject clientResponseJSON = (JSONObject) p.parse(clientResponse.getResponse());
333458
String encodedFile = clientResponseJSON.getAsString("result");
334459

app/src/test/java/i5/las2peer/services/SurveyHandler/ServiceTest.java

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
11
package i5.las2peer.services.SurveyHandler;
22

3-
import java.io.*;
4-
import java.net.HttpURLConnection;
5-
import java.net.URL;
6-
import java.net.URLEncoder;
7-
import java.nio.charset.StandardCharsets;
8-
import java.text.SimpleDateFormat;
9-
import java.util.*;
10-
11-
import i5.las2peer.api.Context;
12-
import i5.las2peer.api.logging.MonitoringEvent;
13-
import i5.las2peer.services.SurveyHandler.database.SurveyHandlerServiceQueries;
14-
import io.swagger.annotations.ApiOperation;
15-
import io.swagger.annotations.ApiResponse;
16-
import io.swagger.annotations.ApiResponses;
17-
import net.minidev.json.JSONArray;
18-
import net.minidev.json.JSONObject;
19-
import net.minidev.json.parser.JSONParser;
20-
import org.glassfish.jersey.server.JSONP;
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.PrintStream;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
218
import org.junit.After;
229
import org.junit.Assert;
2310
import org.junit.Before;
@@ -31,13 +18,9 @@
3118
import i5.las2peer.p2p.LocalNodeManager;
3219
import i5.las2peer.security.UserAgentImpl;
3320
import i5.las2peer.testing.MockAgentFactory;
34-
35-
import javax.ws.rs.Consumes;
36-
import javax.ws.rs.POST;
37-
import javax.ws.rs.Path;
38-
import javax.ws.rs.Produces;
39-
import javax.ws.rs.core.MediaType;
40-
import javax.ws.rs.core.Response;
21+
import net.minidev.json.JSONArray;
22+
import net.minidev.json.JSONObject;
23+
import net.minidev.json.parser.JSONParser;
4124

4225

4326
/**
@@ -554,7 +537,8 @@ public void testLimeSurveyConnector() {
554537
client.setConnectorEndpoint(connector.getHttpEndpoint());
555538
client.setLogin(testAgent.getIdentifier(), testPass);
556539

557-
ClientResponse result = client.sendRequest("POST", mainPath + "getLimeSurveyResponses", "{\"loginName\":" + NameOfUser + ",\"url\":" + uri + ",\"surveyID\":" + surveyID + ",\"loginPassword\":" + Password + "}");
540+
ClientResponse result = client.sendRequest("POST", mainPath + "responses", "{\"loginName\":" + NameOfUser
541+
+ ",\"url\":" + uri + ",\"surveyID\":" + surveyID + ",\"loginPassword\":" + Password + "}");
558542
JSONParser p = new JSONParser();
559543
JSONObject resultJSON = (JSONObject) p.parse(result.getResponse());
560544
String resultString = resultJSON.getAsString("text");

etc/initdb.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
create database shs

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
core.version=1.2.2
22
service.name=i5.las2peer.services.SurveyHandler
33
service.class=SurveyHandlerService
4-
service.version=1.0.1
4+
service.version=1.1.0
55
java.version=17
66

77
las2peer_user1.name=alice

0 commit comments

Comments
 (0)