From 3142643107ede07df83e8c137f59e861523acc1c Mon Sep 17 00:00:00 2001 From: Axel Suarez Date: Fri, 9 Aug 2019 16:08:05 -0700 Subject: [PATCH 1/3] Mentions support in TurnContext --- .../botbuilder/core/turn_context.py | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/libraries/botbuilder-core/botbuilder/core/turn_context.py b/libraries/botbuilder-core/botbuilder/core/turn_context.py index 6900b32a0..d2236aa4f 100644 --- a/libraries/botbuilder-core/botbuilder/core/turn_context.py +++ b/libraries/botbuilder-core/botbuilder/core/turn_context.py @@ -1,9 +1,10 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +import re from copy import copy from typing import List, Callable, Union, Dict -from botbuilder.schema import Activity, ConversationReference, ResourceResponse +from botbuilder.schema import Activity, ConversationReference, Mention, ResourceResponse class TurnContext: @@ -290,3 +291,29 @@ def apply_conversation_reference( activity.reply_to_id = reference.activity_id return activity + + @staticmethod + def remove_recipient_mention(activity: Activity) -> str: + return TurnContext.remove_mention_text(activity, activity.recipient.id) + + @staticmethod + def remove_mention_text(activity: Activity, identifier: str) -> str: + mentions = TurnContext.get_mentions(activity) + for mention in mentions: + if mention.mentioned.id == identifier: + mention_name_match = re.match( + r"/(?<=)(.*?)(?=<\/at>)/i", mention.text + ) + if mention_name_match: + activity.text = activity.text.replace(mention_name_match[0], "") + activity.text = activity.text.replace(r"/<\/at>/g", "") + return activity.text + + @staticmethod + def get_mentions(activity: Activity) -> List[Mention]: + result: List[Mention] = [] + if activity.entities is not None: + for entity in activity.entities: + if entity.type.lower() == "mention": + result.append(entity) + return result From 2777ae915ba22f54f30ea08f1eea328a5339f540 Mon Sep 17 00:00:00 2001 From: Axel Suarez Date: Sun, 11 Aug 2019 12:53:18 -0700 Subject: [PATCH 2/3] Mention support and get_reply_conversation_reference added to TurnContext --- .../botbuilder/core/turn_context.py | 21 +++++++++-- .../tests/test_turn_context.py | 37 +++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/libraries/botbuilder-core/botbuilder/core/turn_context.py b/libraries/botbuilder-core/botbuilder/core/turn_context.py index d2236aa4f..99d53996a 100644 --- a/libraries/botbuilder-core/botbuilder/core/turn_context.py +++ b/libraries/botbuilder-core/botbuilder/core/turn_context.py @@ -292,6 +292,19 @@ def apply_conversation_reference( return activity + @staticmethod + def get_reply_conversation_reference( + activity: Activity, reply: ResourceResponse + ) -> ConversationReference: + reference: ConversationReference = TurnContext.get_conversation_reference( + activity + ) + + # Update the reference with the new outgoing Activity's id. + reference.activity_id = reply.id + + return reference + @staticmethod def remove_recipient_mention(activity: Activity) -> str: return TurnContext.remove_mention_text(activity, activity.recipient.id) @@ -302,11 +315,13 @@ def remove_mention_text(activity: Activity, identifier: str) -> str: for mention in mentions: if mention.mentioned.id == identifier: mention_name_match = re.match( - r"/(?<=)(.*?)(?=<\/at>)/i", mention.text + r"(.*?)<\/at>", mention.text, re.IGNORECASE ) if mention_name_match: - activity.text = activity.text.replace(mention_name_match[0], "") - activity.text = activity.text.replace(r"/<\/at>/g", "") + activity.text = re.sub( + mention_name_match.groups()[1], "", activity.text + ) + activity.text = re.sub(r"<\/at>", "", activity.text) return activity.text @staticmethod diff --git a/libraries/botbuilder-core/tests/test_turn_context.py b/libraries/botbuilder-core/tests/test_turn_context.py index bf1b0c758..9a0cd234d 100644 --- a/libraries/botbuilder-core/tests/test_turn_context.py +++ b/libraries/botbuilder-core/tests/test_turn_context.py @@ -6,6 +6,7 @@ from botbuilder.schema import ( Activity, ChannelAccount, + Mention, ResourceResponse, ConversationAccount, ) @@ -261,3 +262,39 @@ def test_apply_conversation_reference_when_is_incoming_is_true_should_not_prepar assert reply.conversation == ACTIVITY.conversation assert reply.service_url == ACTIVITY.service_url assert reply.channel_id == ACTIVITY.channel_id + + async def test_should_get_conversation_reference_using_get_reply_conversation_reference( + self + ): + context = TurnContext(SimpleAdapter(), ACTIVITY) + reply = await context.send_activity("test") + + assert reply.id, "reply has an id" + + reference = TurnContext.get_reply_conversation_reference( + context.activity, reply + ) + + assert reference.activity_id, "reference has an activity id" + assert ( + reference.activity_id == reply.id + ), "reference id matches outgoing reply id" + + def test_should_remove_at_mention_from_activity(self): + activity = Activity( + type="message", + text="TestOAuth619 test activity", + recipient=ChannelAccount(id="TestOAuth619"), + entities=[ + Mention( + type="mention", + text="TestOAuth619", + mentioned=ChannelAccount(name="Bot", id="TestOAuth619"), + ) + ], + ) + + text = TurnContext.remove_recipient_mention(activity) + + assert text, " test activity" + assert activity.text, " test activity" From 359dce1715693e67954484c9950e800df6c00d12 Mon Sep 17 00:00:00 2001 From: Axel Suarez Date: Mon, 12 Aug 2019 09:22:18 -0700 Subject: [PATCH 3/3] triggering build --- libraries/botbuilder-core/tests/test_turn_context.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/botbuilder-core/tests/test_turn_context.py b/libraries/botbuilder-core/tests/test_turn_context.py index 9a0cd234d..8e7c6f407 100644 --- a/libraries/botbuilder-core/tests/test_turn_context.py +++ b/libraries/botbuilder-core/tests/test_turn_context.py @@ -6,9 +6,9 @@ from botbuilder.schema import ( Activity, ChannelAccount, + ConversationAccount, Mention, ResourceResponse, - ConversationAccount, ) from botbuilder.core import BotAdapter, TurnContext