Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mentions support in TurnContext #291

Merged
merged 4 commits into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion libraries/botbuilder-core/botbuilder/core/turn_context.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -290,3 +291,44 @@ def apply_conversation_reference(
activity.reply_to_id = reference.activity_id

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)

@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(.*)>(.*?)<\/at>", mention.text, re.IGNORECASE
)
if mention_name_match:
activity.text = re.sub(
mention_name_match.groups()[1], "", activity.text
)
activity.text = re.sub(r"<at><\/at>", "", activity.text)
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
39 changes: 38 additions & 1 deletion libraries/botbuilder-core/tests/test_turn_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
from botbuilder.schema import (
Activity,
ChannelAccount,
ResourceResponse,
ConversationAccount,
Mention,
ResourceResponse,
)
from botbuilder.core import BotAdapter, TurnContext

Expand Down Expand Up @@ -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="<at>TestOAuth619</at> test activity",
recipient=ChannelAccount(id="TestOAuth619"),
entities=[
Mention(
type="mention",
text="<at>TestOAuth619</at>",
mentioned=ChannelAccount(name="Bot", id="TestOAuth619"),
)
],
)

text = TurnContext.remove_recipient_mention(activity)

assert text, " test activity"
assert activity.text, " test activity"