-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
""" | ||
Copyright (c) Microsoft Corporation. All rights reserved. | ||
Licensed under the MIT License. | ||
""" | ||
|
||
from .messages import Messages | ||
|
||
__all__ = ["Messages"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
""" | ||
Copyright (c) Microsoft Corporation. All rights reserved. | ||
Licensed under the MIT License. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Awaitable, Callable, Generic, List, TypeVar | ||
|
||
from botbuilder.core import TurnContext | ||
from botbuilder.core.serializer_helper import serializer_helper | ||
from botbuilder.schema import ActivityTypes | ||
|
||
from teams.route import Route | ||
from teams.state import TurnState | ||
|
||
FETCH_INVOKE_NAME = "message/fetchTask" | ||
|
||
StateT = TypeVar("StateT", bound=TurnState) | ||
|
||
|
||
class Messages(Generic[StateT]): | ||
_routes: List[Route[StateT]] = [] | ||
|
||
def __init__(self, routes: List[Route[StateT]]) -> None: | ||
self._routes = routes | ||
|
||
def fetch(self) -> Callable[ | ||
[Callable[[TurnContext, StateT, dict], Awaitable[None]]], | ||
Callable[[TurnContext, StateT, dict], Awaitable[None]], | ||
]: | ||
""" | ||
Adds a route for handling the message fetch task activity. | ||
This method can be used as either a decorator or a method. | ||
```python | ||
# Use this method as a decorator | ||
@app.messages.fetch() | ||
async def fetch(context: TurnContext, state: TurnState, data: dict): | ||
print(f"Execute with data: {data}") | ||
return True | ||
# Pass a function to this method | ||
app.messages.fetch()(fetch) | ||
``` | ||
""" | ||
|
||
# Create route selector for the handler | ||
def __selector__(context: TurnContext) -> bool: | ||
return ( | ||
context.activity.type == ActivityTypes.invoke | ||
and context.activity.name == FETCH_INVOKE_NAME | ||
) | ||
|
||
def __call__( | ||
func: Callable[[TurnContext, StateT, dict], Awaitable[None]], | ||
) -> Callable[[TurnContext, StateT, dict], Awaitable[None]]: | ||
async def __handler__(context: TurnContext, state: StateT): | ||
if not context.activity.value: | ||
return False | ||
await func(context, state, context.activity.value) | ||
return True | ||
|
||
self._routes.append(Route[StateT](__selector__, __handler__)) | ||
return func | ||
|
||
return __call__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
Copyright (c) Microsoft Corporation. All rights reserved. | ||
Licensed under the MIT License. | ||
""" | ||
|
||
from unittest import IsolatedAsyncioTestCase, mock | ||
|
||
import pytest | ||
from botbuilder.core import TurnContext | ||
from botbuilder.schema import Activity, ChannelAccount, ConversationAccount | ||
|
||
from teams import Application | ||
from tests.utils import SimpleAdapter | ||
|
||
|
||
class TestMessages(IsolatedAsyncioTestCase): | ||
application: Application | ||
|
||
@pytest.fixture(autouse=True) | ||
def before_each(self): | ||
self.application = Application() | ||
yield | ||
|
||
@pytest.mark.asyncio | ||
async def test_fetch(self): | ||
handler = mock.AsyncMock() # mock handler | ||
self.application.messages.fetch()(handler) | ||
self.assertEqual(len(self.application._routes), 1) | ||
|
||
# The handler will be tirggered | ||
await self.application.on_turn( | ||
TurnContext( | ||
SimpleAdapter(), | ||
Activity( | ||
id="1234", | ||
type="invoke", | ||
name="message/fetchTask", | ||
value={"hello": "world"}, | ||
from_property=ChannelAccount(id="user", name="Task Modules Test User"), | ||
recipient=ChannelAccount(id="bot", name="Task Modules Test Bot"), | ||
conversation=ConversationAccount(id="convo", name="Task Modules Test Convo"), | ||
channel_id="UnitTest", | ||
locale="en-uS", | ||
service_url="https://example.org", | ||
), | ||
) | ||
) |