Commit 5d016ca2ed19d9dd1e071e3043d7fcc6b3cebdbb

Authored by Neocortexxx
2 parents ed8363df 7d394baa

Merge branch 'development' of https://git.ziemers.de/edu/BeuthBot into development

docker/rasa_nlu/volumes/data/api/intents/showFood.json
... ... @@ -10,6 +10,7 @@
10 10 "parameters": [
11 11 {
12 12 "id": "7b5df330-e1fc-4bb4-86de-5a51be29c836",
  13 + "required": false,
13 14 "dataType": "@date",
14 15 "name": "date",
15 16 "value": "$date",
... ... @@ -17,6 +18,7 @@
17 18 },
18 19 {
19 20 "id": "f3da5ec9-d3e8-4263-9bc3-7d600784bd0d",
  21 + "required": false,
20 22 "dataType": "@dishtype",
21 23 "name": "dishtype",
22 24 "value": "$dishtype",
... ... @@ -24,6 +26,7 @@
24 26 },
25 27 {
26 28 "id": "499e1a33-d7ef-4446-ae7d-30ab94c9e3a5",
  29 + "required": false,
27 30 "dataType": "@healthy",
28 31 "name": "healthy",
29 32 "value": "$healthy",
... ... @@ -31,6 +34,7 @@
31 34 },
32 35 {
33 36 "id": "f53af932-a4f2-4fe5-be2d-eb96a30e9400",
  37 + "required": false,
34 38 "dataType": "@ingredients",
35 39 "name": "ingredients",
36 40 "value": "$ingredients",
... ... @@ -38,6 +42,7 @@
38 42 },
39 43 {
40 44 "id": "b8c8ab1d-4190-4200-9540-2893a084a1fd",
  45 + "required": false,
41 46 "dataType": "@price",
42 47 "name": "price",
43 48 "value": "$price",
... ... @@ -45,6 +50,7 @@
45 50 },
46 51 {
47 52 "id": "92607e59-96c8-4c61-8dc0-670c7b8c2b43",
  53 + "required": false,
48 54 "dataType": "@dishcategory",
49 55 "name": "dishcategory",
50 56 "value": "$dishcategory",
... ...
docker/wildfly/volumes/conf/beuthbot.properties
... ... @@ -18,7 +18,7 @@ API_AI_TOKEN = XXXXX
18 18 TELEGRAM_WEBHOOK_URL = /telegram/getUpdates
19 19  
20 20 # Your Bot Token e.g.: 000000000:AAAAa0aAA_aaA-Aaaa0A0Aa_a0aa0A0AAAA
21   -TELEGRAM_BOT_TOKEN = 452768505:AAHNXEYi9nSHcmVc4b3799I00uTZmK53wl4\
  21 +TELEGRAM_BOT_TOKEN = 451726698:AAH2qwZaWOgn1IU1CjJU-HjHDMODG6DEkN0
22 22  
23 23  
24 24  
... ...
services/Common/src/main/java/de/bht/beuthbot/model/EntityName.java
... ... @@ -5,7 +5,7 @@ package de.bht.beuthbot.model;
5 5 */
6 6 public enum EntityName {
7 7  
8   - DATE("date"), DISH_TYPE("dishtype"), HEALTHY("healthy"), INGREDIENTS("ingredients"), DISH_CATEGORY("dishcategory"),
  8 + DATE("date"), TIME("time"), DISH_TYPE("dishtype"), HEALTHY("healthy"), INGREDIENTS("ingredients"), DISH_CATEGORY("dishcategory"),
9 9 DISH_NAME("dishname"), REMINDER_CYCLE("reminderCycle");
10 10  
11 11 private String text;
... ...
services/Common/src/main/java/de/bht/beuthbot/utils/DateStringExtractor.java 0 → 100644
  1 +package de.bht.beuthbot.utils;
  2 +
  3 +import javax.ejb.Local;
  4 +import java.time.DayOfWeek;
  5 +import java.time.LocalDate;
  6 +import java.time.format.TextStyle;
  7 +import java.util.Locale;
  8 +
  9 +public class DateStringExtractor {
  10 + public static LocalDate getLocalDateFromString(String dateString) {
  11 + LocalDate retrievedDate = LocalDate.now(); //initialize as current date
  12 + DayOfWeek today = retrievedDate.getDayOfWeek();
  13 +
  14 + if(dateString.equals("TODAY")) {
  15 + return retrievedDate;
  16 + } else if(dateString.equals("TOMORROW")) {
  17 + retrievedDate = retrievedDate.plusDays(1);
  18 + } else if(dateString.equals("DAYAFTERTOMORROW")) {
  19 + retrievedDate = retrievedDate.plusDays(2);
  20 + } else if(dateString.equals("MONDAY") || dateString.equals("TUESDAY") || dateString.equals("WEDNESDAY") || dateString.equals("THURSDAY") || dateString.equals("FRIDAY") || dateString.equals("SATURDAY") || dateString.equals("SUNDAY")) {
  21 + DayOfWeek dayFromString = DayOfWeek.valueOf(dateString);
  22 + long distanceBetweenDates = dayFromString.getValue() - today.getValue();
  23 + //if the distance between given days is equal to or smaller than 0 we can infer that the user is referring to the upcoming week
  24 + if(distanceBetweenDates <= 0) {
  25 + distanceBetweenDates += 7;
  26 + }
  27 + retrievedDate = retrievedDate.plusDays(distanceBetweenDates);
  28 +
  29 + }
  30 +
  31 + return retrievedDate;
  32 + }
  33 +
  34 + //This function takes a LocalDate and returns its Weekday Name in form of a string
  35 + public static String getWeekDayFromLocalDate(LocalDate date, boolean pretty, Locale locale) {
  36 + String weekDay = date.getDayOfWeek().getDisplayName(TextStyle.FULL, locale);
  37 + if(pretty) { //if pretty is set to true, we will return pretty names like "Today" and "Tomorrow"
  38 + if(LocalDate.now().getDayOfWeek().getValue() == date.getDayOfWeek().getValue()) { //TODAY
  39 + if(locale == Locale.GERMAN) {
  40 + weekDay = "heute";
  41 + } else {
  42 + weekDay = "today";
  43 + }
  44 + } else if(LocalDate.now().plusDays(1).getDayOfWeek().getValue() == date.getDayOfWeek().getValue()) { //TOMORROW
  45 + if(locale == Locale.GERMAN) {
  46 + weekDay = "morgen";
  47 + } else {
  48 + weekDay = "tomorrow";
  49 + }
  50 + }
  51 + }
  52 + return weekDay;
  53 + }
  54 +
  55 + public static String getGermanWeekDayFromLocalDate(LocalDate date, boolean pretty) {
  56 + return getWeekDayFromLocalDate(date, pretty, Locale.GERMAN);
  57 + }
  58 +}
... ...
services/MainBot/src/main/java/de/bht/beuthbot/canteen/model/CanteenData.java
1 1 package de.bht.beuthbot.canteen.model;
2 2  
  3 +import de.bht.beuthbot.utils.DateStringExtractor;
3 4 import org.slf4j.Logger;
4 5 import org.slf4j.LoggerFactory;
5 6  
... ... @@ -84,8 +85,10 @@ public class CanteenData {
84 85 public final List<Dish> getDishesFiltered(final String pDate, final String pHealthy, final String pDishType,
85 86 final String pMarkings, final String pDishCategory) {
86 87  
87   - final LocalDate date = ("tomorrow".equals(pDate) ? LocalDate.now().plusDays(1) : LocalDate.now());
88   -
  88 + logger.debug("pDate: " + pDate);
  89 + //final LocalDate date = ("tomorrow".equals(pDate) ? LocalDate.now().plusDays(1) : LocalDate.now());
  90 + final LocalDate date = DateStringExtractor.getLocalDateFromString(pDate);
  91 + logger.debug("resulting local date: " + date);
89 92 final TrafficLight trafficLight = (pHealthy != null && !"".equals(pHealthy) ? TrafficLight.getTrafficLight(pHealthy): null);
90 93  
91 94 final DishType dishType = (pDishType != null && !"".equals(pDishType) ? DishType.getDishTypeByName(pDishType) : null);
... ...
services/MainBot/src/main/java/de/bht/beuthbot/scheduler/SchedulerBean.java
... ... @@ -3,11 +3,13 @@ package de.bht.beuthbot.scheduler;
3 3 import de.bht.beuthbot.attachments.AttachmentStore;
4 4 import de.bht.beuthbot.jms.Target;
5 5 import de.bht.beuthbot.model.Attachment;
  6 +import de.bht.beuthbot.model.EntityName;
6 7 import de.bht.beuthbot.model.Intent;
7 8 import de.bht.beuthbot.model.Messenger;
8 9 import de.bht.beuthbot.model.entities.AppUser;
9 10 import de.bht.beuthbot.scheduler.model.Reminder;
10 11 import de.bht.beuthbot.scheduler.model.ReminderCycle;
  12 +import de.bht.beuthbot.utils.DateStringExtractor;
11 13 import org.slf4j.Logger;
12 14 import org.slf4j.LoggerFactory;
13 15  
... ... @@ -18,6 +20,7 @@ import de.bht.beuthbot.jms.TaskMessage;
18 20  
19 21 import java.text.ParseException;
20 22 import java.text.SimpleDateFormat;
  23 +import java.time.LocalDate;
21 24 import java.util.*;
22 25 import javax.annotation.PostConstruct;
23 26 import javax.annotation.Resource;
... ... @@ -75,14 +78,14 @@ public class SchedulerBean {
75 78  
76 79 for (Reminder reminder : _reminders) {
77 80 //Check if date time now equals reminder date time
78   - if(dateNow.equals(formatter.format(reminder.getCycleDate()))) {
  81 + if(dateNow.equals(formatter.format(reminder.getCycleTime()))) {
79 82 Long messageID = 1L;
80 83 AppUser user = reminder.getUser(); //TODO: Fill with correct value from DB
81 84 Long messengerId = Long.valueOf(user.getFacebookUserId() == null ? user.getTelegramUserId() : user.getFacebookUserId());
82 85 Messenger messenger = reminder.getMessenger(); //TODO: Fill with correct value from DB
83 86 String text = "";
84   - String intent = "showFood";
85   - Map entities = Collections.emptyMap();
  87 + String intent = reminder.getIntent();
  88 + Map entities = reminder.getEntities();
86 89 try {
87 90 TaskMessage message = new TaskMessage(1L, Target.MAINBOT, messageID, messengerId , messenger, text, null, intent, entities);
88 91 processQueue.route(new TaskMessage(message));
... ... @@ -93,12 +96,34 @@ public class SchedulerBean {
93 96 }
94 97 }
95 98  
96   - public String createReminder(TaskMessage message, AppUser user) {
  99 + public Reminder createReminder(TaskMessage message, AppUser user) {
  100 + logger.debug("Creating reminder");
  101 +
  102 + Messenger messenger = message.getMessenger();
  103 +
  104 +
  105 + //Figure out when to remind the user
  106 +
  107 + //Set reminder cycle to one shot as a default
  108 + ReminderCycle cycle = ReminderCycle.ONCE;
  109 + //Extract the Date from the message
  110 + String messageDay = message.getEntities().getOrDefault("date", "TODAY");
  111 + //If the date retrieved from the message contains a cycling parameter (e.g. "daily"), set the reminderCycle
  112 + switch(messageDay) {
  113 + case "DAILY":
  114 + cycle = ReminderCycle.DAILY;
  115 + break;
  116 + case "WEEKLY":
  117 + cycle = ReminderCycle.WEEKLY;
  118 + break;
  119 + case "MONTHLY":
  120 + cycle = ReminderCycle.MONTHLY;
  121 + break;
  122 + }
  123 +
  124 + LocalDate cycleDate = DateStringExtractor.getLocalDateFromString(messageDay);
97 125  
98   - ReminderCycle cycle = ReminderCycle.DAILY;
99   - Messenger messener = message.getMessenger();
100   - String intent = message.getEntities().getOrDefault("event","");
101   - Date date;
  126 + Date cycleTime;
102 127 try {
103 128 //Read out time for Reminder
104 129  
... ... @@ -126,21 +151,39 @@ public class SchedulerBean {
126 151 SimpleDateFormat f = new SimpleDateFormat("dd.MM.yyyy hh:mm");
127 152 Date d = f.parse(dateNow);
128 153 long milliseconds = d.getTime();
129   - date = new Date(milliseconds);
  154 + cycleTime = new Date(milliseconds);
130 155  
131 156 } else
132   - date = new Date();
  157 + cycleTime = new Date();
133 158  
134 159 } catch (Exception e) {
135   - return "Fehler beim Anlegen der Erinnerung";
  160 + return null;
  161 + }
  162 + //Figure out what we're reminding the user of
  163 + String intent = message.getEntities().getOrDefault("event","FALLBACK");
  164 +
  165 + Map<String, String> entities = new HashMap<>();
  166 + switch(intent) {
  167 + case "showFood":
  168 + intent = Intent.SHOW_FOOD.getText();
  169 + logger.debug("Intent is show food");
  170 + entities.put(EntityName.DATE.getText(), cycleDate.toString());
  171 + entities.put(EntityName.TIME.getText(), cycleTime.toString());
  172 + logger.debug("Entities added: " + entities);
  173 + break;
  174 + case "showCourses":
  175 + //TODO: add Courses functionality
  176 + break;
  177 + default:
  178 + intent = "FALLBACK";
  179 + break;
136 180 }
137 181  
138   - Reminder reminder = new Reminder(user, cycle, date, messener, intent);
  182 + logger.debug("Creating reminder with date " + cycleDate.toString() + " and time " + cycleTime.toString());
  183 + Reminder reminder = new Reminder(user, cycle, cycleDate, cycleTime, messenger, intent, entities);
139 184 _reminders.add(reminder);
140 185  
141   - return "Erinnerung erstellt:\n" +
142   - "Wann: um " + new SimpleDateFormat("KK:mm").format(reminder.getCycleDate()) + " Uhr\n" +
143   - "Was: " + reminder.getIntent();
  186 + return reminder;
144 187  
145 188 //TODO: Insert the new Reminder into Database
146 189 }
... ...
services/MainBot/src/main/java/de/bht/beuthbot/scheduler/model/Reminder.java
1 1 package de.bht.beuthbot.scheduler.model;
2 2  
  3 +import de.bht.beuthbot.model.Intent;
3 4 import de.bht.beuthbot.model.Messenger;
4 5 import de.bht.beuthbot.model.entities.AppUser;
5 6  
  7 +import java.time.LocalDate;
6 8 import java.util.Date;
  9 +import java.util.HashMap;
  10 +import java.util.Map;
7 11  
8 12 public class Reminder {
9 13 //TODO: Replace with User Object
... ... @@ -11,18 +15,25 @@ public class Reminder {
11 15 //Reminder cycle
12 16 private ReminderCycle _reminderCycle;
13 17 //Date of Cycle
14   - private Date _cycleDate;
  18 + private LocalDate _cycleDate;
  19 + //Time of cycle
  20 + private Date _cycleTime;
  21 + //private
15 22 //Used Messenger
16 23 private Messenger _messenger;
17 24 //Intent after Reminder is triggered
18 25 private String _intent;
  26 + //Entities
  27 + private final Map<String, String> _entities;
19 28  
20   - public Reminder(AppUser user, ReminderCycle reminderCycle, Date cycleDate, Messenger messenger, String intent) {
  29 + public Reminder(AppUser user, ReminderCycle reminderCycle, LocalDate cycleDate, Date cycleTime, Messenger messenger, String intent, Map<String, String> entities) {
21 30 _user = user;
22 31 _reminderCycle = reminderCycle;
23 32 _cycleDate = cycleDate;
  33 + _cycleTime = cycleTime;
24 34 _messenger = messenger;
25 35 _intent = intent;
  36 + _entities = entities;
26 37 }
27 38  
28 39 public Messenger getMessenger() {
... ... @@ -57,11 +68,17 @@ public class Reminder {
57 68 _reminderCycle = reminderCycle;
58 69 }
59 70  
60   - public Date getCycleDate() {
  71 + public LocalDate getCycleDate() {
61 72 return _cycleDate;
62 73 }
63 74  
64   - public void setCycleDate(Date cycleDate) {
  75 + public void setCycleDate(LocalDate cycleDate) {
65 76 _cycleDate = cycleDate;
66 77 }
  78 +
  79 + public Date getCycleTime() { return _cycleTime; }
  80 +
  81 + public void setCycleTime(Date cycleTime) { _cycleTime = cycleTime; }
  82 +
  83 + public Map<String, String> getEntities() { return _entities; }
67 84 }
... ...
services/MainBot/src/main/resources/de/bht/beuthbot/drools/Canteen.drl
... ... @@ -39,9 +39,15 @@ import java.text.ParseException
39 39 import de.bht.beuthbot.model.Messenger
40 40 import de.bht.beuthbot.scheduler.model.Reminder;
41 41  
  42 +import java.time.DayOfWeek;
  43 +import java.time.LocalDate
  44 +import de.bht.beuthbot.utils.DateStringExtractor;
  45 +
42 46 global de.bht.beuthbot.canteen.model.CanteenData canteenData;
43 47 global de.bht.beuthbot.model.entities.AppUser user;
44 48  
  49 +
  50 +
45 51 // ---------------------- GREETING ------------------------------------
46 52 rule "Hello Drools"
47 53 dialect "java"
... ... @@ -88,8 +94,12 @@ rule &quot;Show dishes filtered&quot;
88 94 when
89 95 m : DroolsMessage( getIntent().equals(Intent.SHOW_FOOD.getText()) && !entities.isEmpty() )
90 96 then
  97 + String day = m.getEntities().getOrDefault(EntityName.DATE.getText(), "");
  98 + if(day != "") {
  99 + day = DateStringExtractor.getGermanWeekDayFromLocalDate(DateStringExtractor.getLocalDateFromString(day),true);
  100 + }
91 101 modify( m ) {
92   - setText("Gerichte:\n" + canteenData.getDishesFilteredAsString(
  102 + setText("Diese Gerichte gibt es " + day + ":\n" + canteenData.getDishesFilteredAsString(
93 103 (m.getEntities().containsKey(EntityName.DATE.getText()) ? m.getEntities().get(EntityName.DATE.getText()) : null),
94 104 (m.getEntities().containsKey(EntityName.HEALTHY.getText()) ? m.getEntities().get(EntityName.HEALTHY.getText()) : null),
95 105 (m.getEntities().containsKey(EntityName.DISH_TYPE.getText()) ? m.getEntities().get(EntityName.DISH_TYPE.getText()) : null),
... ... @@ -126,7 +136,13 @@ rule &quot;Create reminder&quot;
126 136 then
127 137 SchedulerBean s = new SchedulerBean();
128 138 TaskMessage message = new TaskMessage(m);
129   - String messageText = s.createReminder(message, user);
  139 + Reminder reminder = s.createReminder(message, user);
  140 + String messageText = "";
  141 + if(reminder.getIntent() == Intent.SHOW_FOOD.getText()) {
  142 + messageText = "Ich werde dir " + DateStringExtractor.getGermanWeekDayFromLocalDate(reminder.getCycleDate(), true) + " um " + new SimpleDateFormat("KK:mm").format(reminder.getCycleTime()) + " Uhr den Mensaplan mitteilen!";
  143 + } else {
  144 + messageText = "Es war mir leider nicht möglich eine Erinnerung für dich anzulegen. Versuche es bitte noch einmal.";
  145 + } //TODO: Add other intents
130 146 m.setText(messageText);
131 147  
132 148 end
... ...
services/Rasa/src/main/java/de/bht/beuthbot/nlp/rasa/RasaConnector.java
... ... @@ -79,7 +79,8 @@ public class RasaConnector implements MessageListener {
79 79 logger.debug("{}: {}", response.getStatus(), responseAsString);
80 80  
81 81 RasaResponse rasaResponse = gson.fromJson(responseAsString, RasaResponse.class);
82   - TaskMessage messageToMainBot = new TaskMessage(new RasaMessage(incomingChatMessage, rasaResponse));
  82 + RasaMessage rasaMessage = new RasaMessage(incomingChatMessage, rasaResponse);
  83 + TaskMessage messageToMainBot = new TaskMessage(rasaMessage);
83 84 processQueue.route(messageToMainBot);
84 85 } catch (JMSException e) {
85 86 logger.error("Error while processing message.", e);
... ...
services/Telegram/src/main/java/de/bht/beuthbot/messenger/telegram/TelegramSendAdapter.java
... ... @@ -103,9 +103,13 @@ public class TelegramSendAdapter implements MessageListener {
103 103 * @param message to send
104 104 */
105 105 private void sendMessage(final Long senderId, final String message) {
  106 + logger.debug("Message is " + message.length() + " characters long");
106 107 SendMessage request = new SendMessage(senderId, message);
107 108 SendResponse sendResponse = bot.execute(request);
108 109 logger.debug("Send message: " + sendResponse.isOk());
  110 + if(!sendResponse.isOk()) {
  111 + logger.debug("Sending message to user " + senderId + " failed: " + sendResponse.message());
  112 + }
109 113 }
110 114  
111 115 /**
... ...