diff --git a/libraries/bot-builder/src/main/java/com/microsoft/bot/builder/SkypeMentionNormalizeMiddleware.java b/libraries/bot-builder/src/main/java/com/microsoft/bot/builder/SkypeMentionNormalizeMiddleware.java index f6ced4bb..8658169f 100644 --- a/libraries/bot-builder/src/main/java/com/microsoft/bot/builder/SkypeMentionNormalizeMiddleware.java +++ b/libraries/bot-builder/src/main/java/com/microsoft/bot/builder/SkypeMentionNormalizeMiddleware.java @@ -3,6 +3,7 @@ package com.microsoft.bot.builder; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; import com.microsoft.bot.connector.Channels; @@ -35,11 +36,10 @@ public class SkypeMentionNormalizeMiddleware implements Middleware { if (closingBracket != -1) { int openingBracket = text.indexOf("<", closingBracket); if (openingBracket != -1) { - String mention = text.substring(closingBracket + 1, openingBracket); + String mention = text.substring(closingBracket + 1, openingBracket).trim(); // create new JsonNode with new mention value - ObjectNode node = JsonNodeFactory.instance.objectNode(); - node.put("text", mention); + JsonNode node = JsonNodeFactory.instance.textNode(mention); entity.setProperties("text", node); } } diff --git a/libraries/bot-builder/src/test/java/com/microsoft/bot/builder/MentionTests.java b/libraries/bot-builder/src/test/java/com/microsoft/bot/builder/MentionTests.java new file mode 100644 index 00000000..1bd5cebe --- /dev/null +++ b/libraries/bot-builder/src/test/java/com/microsoft/bot/builder/MentionTests.java @@ -0,0 +1,131 @@ +package com.microsoft.bot.builder; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.microsoft.bot.schema.Activity; +import com.microsoft.bot.schema.Entity; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; + +public class MentionTests { + static ObjectMapper mapper = new ObjectMapper(); + static { + mapper.findAndRegisterModules(); + } + + @Test + public void Mention_Skype() throws IOException + { + // A Skype mention contains the user mention enclosed in tags. But the activity.getText() (as below) + // does not. + String mentionJson = "{\"mentioned\": {\"id\": \"recipientid\"},\"text\": \"botname\"}"; + Entity mention = mapper.readValue(mentionJson, Entity.class); + mention.setType("mention"); + + Activity activity = MessageFactory.text("botname sometext"); + activity.setChannelId("skype"); + activity.getEntities().add(mention); + + // Normalize the Skype mention so that it is in a format RemoveMentionText can handle. + // If SkypeMentionNormalizeMiddleware is added to the adapters Middleware set, this + // will be called on every Skype message. + SkypeMentionNormalizeMiddleware.normalizeSkypMentionText(activity); + + // This will remove the Mention.Text from the activity.getText(). This should just leave before/after the + // mention. + activity.removeMentionText("recipientid"); + + Assert.assertEquals(activity.getText(), "sometext"); + } + + @Test + public void Mention_Teams() throws IOException + { + String mentionJson = "{\"mentioned\": {\"id\": \"recipientid\"},\"text\": \"botname\"}"; + Entity mention = mapper.readValue(mentionJson, Entity.class); + mention.setType("mention"); + + Activity activity = MessageFactory.text("botname sometext"); + activity.getEntities().add(mention); + + activity.removeMentionText("recipientid"); + + Assert.assertEquals(activity.getText(), "sometext"); + } + + @Test + public void Mention_slack() throws IOException + { + String mentionJson = "{\"mentioned\": {\"id\": \"recipientid\"},\"text\": \"@botname\"}"; + Entity mention = mapper.readValue(mentionJson, Entity.class); + mention.setType("mention"); + + Activity activity = MessageFactory.text("@botname sometext"); + activity.getEntities().add(mention); + + activity.removeMentionText("recipientid"); + + Assert.assertEquals(activity.getText(), "sometext"); + } + + @Test + public void Mention_GroupMe() throws IOException + { + String mentionJson = "{\"mentioned\": {\"id\": \"recipientid\"},\"text\": \"@bot name\"}"; + Entity mention = mapper.readValue(mentionJson, Entity.class); + mention.setType("mention"); + + Activity activity = MessageFactory.text("@bot name sometext"); + activity.getEntities().add(mention); + + activity.removeMentionText("recipientid"); + + Assert.assertEquals(activity.getText(), "sometext"); + } + + @Test + public void Mention_Telegram() throws IOException + { + String mentionJson = "{\"mentioned\": {\"id\": \"recipientid\"},\"text\": \"botname\"}"; + Entity mention = mapper.readValue(mentionJson, Entity.class); + mention.setType("mention"); + + Activity activity = MessageFactory.text("botname sometext"); + activity.getEntities().add(mention); + + activity.removeMentionText("recipientid"); + + Assert.assertEquals(activity.getText(), "sometext"); + } + + @Test + public void Mention_Facebook() + { + // no-op for now: Facebook mentions unknown at this time + } + + @Test + public void Mention_Email() + { + // no-op for now: EMail mentions not included in activity.getText()? + } + + @Test + public void Mention_Cortana() + { + // no-op for now: Cortana mentions unknown at this time + } + + @Test + public void Mention_Kik() + { + // no-op for now: bot mentions in Kik don't get Entity info and not included in activity.getText() + } + + @Test + public void Mention_Twilio() + { + // no-op for now: Twilio mentions unknown at this time. Could not determine if they are supported. + } +} diff --git a/libraries/bot-schema/src/main/java/com/microsoft/bot/schema/Activity.java b/libraries/bot-schema/src/main/java/com/microsoft/bot/schema/Activity.java index 88dbd0ff..fe56824e 100644 --- a/libraries/bot-schema/src/main/java/com/microsoft/bot/schema/Activity.java +++ b/libraries/bot-schema/src/main/java/com/microsoft/bot/schema/Activity.java @@ -1486,10 +1486,19 @@ public class Activity { } String text = activity.getText(); + if (StringUtils.isEmpty(text)) { + return text; + } for (Mention mention : activity.getMentions()) { if (StringUtils.equals(mention.getMentioned().getId(), id)) { - text = text.replaceAll(mention.getText(), ""); + if (StringUtils.isEmpty(mention.getText())) { + text = text.replaceAll("" + mention.getMentioned().getName() + "", ""); + } else { + text = text.replaceAll(mention.getText(), ""); + } + + text = text.trim(); } } diff --git a/libraries/bot-schema/src/main/java/com/microsoft/bot/schema/Entity.java b/libraries/bot-schema/src/main/java/com/microsoft/bot/schema/Entity.java index 3b4d9d27..4a9d9eab 100644 --- a/libraries/bot-schema/src/main/java/com/microsoft/bot/schema/Entity.java +++ b/libraries/bot-schema/src/main/java/com/microsoft/bot/schema/Entity.java @@ -8,6 +8,7 @@ package com.microsoft.bot.schema; import com.fasterxml.jackson.annotation.JsonAnyGetter; import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.JsonProcessingException; @@ -105,6 +106,7 @@ public class Entity { * @param classType Class extended EntitySerialization * @return Entity converted to type T */ + @JsonIgnore public T getAs(Class classType) { // Serialize @@ -139,6 +141,7 @@ public class Entity { * @param obj of type T * @param obj */ + @JsonIgnore public Entity setAs(T obj) { // Serialize String tempJson;