8000 rename BotContext to TurnContext by stevengum · Pull Request #92 · microsoft/botbuilder-python · GitHub
[go: up one dir, main page]

Skip to content

rename BotContext to TurnContext #92

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

Merged
merged 1 commit into from
Jul 5, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from aiohttp import web
from botbuilder.schema import (Activity, ActivityTypes)
from botbuilder.core import (BotFrameworkAdapter, BotFrameworkAdapterSettings, BotContext)
from botbuilder.core import (BotFrameworkAdapter, BotFrameworkAdapterSettings, TurnContext)

APP_ID = ''
APP_PASSWORD = ''
Expand All @@ -23,13 +23,13 @@ async def create_reply_activity(request_activity, text) -> Activity:
service_url=request_activity.service_url)


async def handle_message(context: BotContext) -> web.Response:
async def handle_message(context: TurnContext) -> web.Response:
response = await create_reply_activity(context.request, 'You said %s.' % context.request.text)
await context.send_activity(response)
return web.Response(status=202)


async def handle_conversation_update(context: BotContext) -> web.Response:
async def handle_conversation_update(context: TurnContext) -> web.Response:
if context.request.members_added[0].id != context.request.recipient.id:
response = await create_reply_activity(context.request, 'Welcome to the Echo Adapter Bot!')
await context.send_activity(response)
Expand All @@ -40,7 +40,7 @@ async def unhandled_activity() -> web.Response:
return web.Response(status=404)


async def request_handler(context: BotContext) -> web.Response:
async def request_handler(context: TurnContext) -> web.Response:
if context.request.type == 'message':
return await handle_message(context)
elif context.request.type == 'conversationUpdate':
Expand All @@ -65,4 +65,4 @@ async def messages(req: web.web_request) -> web.Response:
try:
web.run_app(app, host='localhost', port=PORT)
except Exception as e:
raise e
raise e
4 changes: 2 additions & 2 deletions libraries/botbuilder-core/botbuilder/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from .bot_adapter import BotAdapter
from .bot_framework_adapter import BotFrameworkAdapter, BotFrameworkAdapterSettings
from .bot_context import BotContext
from .turn_context import TurnContext
from .bot_state import BotState
from .card_factory import CardFactory
from .conversation_state import ConversationState
Expand All @@ -22,7 +22,7 @@

__all__ = ['AnonymousReceiveMiddleware',
'BotAdapter',
'BotContext',
'TurnContext',
'BotFrameworkAdapter',
'BotFrameworkAdapterSettings',
'BotState',
Expand Down
10 changes: 5 additions & 5 deletions libraries/botbuilder-core/botbuilder/core/bot_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import List, Callable
from botbuilder.schema import Activity, ConversationReference

from .bot_context import BotContext
from .turn_context import TurnContext
from .middleware_set import MiddlewareSet


Expand All @@ -14,7 +14,7 @@ def __init__(self):
self._middleware = MiddlewareSet()

@abstractmethod
async def send_activities(self, context: BotContext, activities: List[Activity]):
async def send_activities(self, context: TurnContext, activities: List[Activity]):
"""
Sends a set of activities to the user. An array of responses from the server will be returned.
:param activities:
Expand All @@ -23,7 +23,7 @@ async def send_activities(self, context: BotContext, activities: List[Activity])
raise NotImplementedError()

@abstractmethod
async def update_activity(self, context: BotContext, activity: Activity):
async def update_activity(self, context: TurnContext, activity: Activity):
"""
Replaces an existing activity.
:param activity:
Expand All @@ -32,7 +32,7 @@ async def update_activity(self, context: BotContext, activity: Activity):
raise NotImplementedError()

@abstractmethod
async def delete_activity(self, context: BotContext, reference: ConversationReference):
async def delete_activity(self, context: TurnContext, reference: ConversationReference):
"""
Deletes an existing activity.
:param reference:
Expand All @@ -48,7 +48,7 @@ def use(self, middleware):
"""
self._middleware.use(middleware)

async def run_middleware(self, context: BotContext, callback: Callable=None):
async def run_middleware(self, context: TurnContext, callback: Callable=None):
"""
Called by the parent class to run the adapters middleware set and calls the passed in `callback()` handler at
the end of the chain.
Expand Down
20 changes: 10 additions & 10 deletions libraries/botbuilder-core/botbuilder/core/bot_framework_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from . import __version__
from .bot_adapter import BotAdapter
from .bot_context import BotContext
from .turn_context import TurnContext

USER_AGENT = f"Microsoft-BotFramework/3.1 (BotBuilder Python/{__version__})"

Expand Down Expand Up @@ -42,7 +42,7 @@ async def continue_conversation(self, reference: ConversationReference, logic):
:param logic:
:return:
"""
request = BotContext.apply_conversation_reference(Activity(), reference, is_incoming=True)
request = TurnContext.apply_conversation_reference(Activity(), reference, is_incoming=True)
context = self.create_context(request)
return await self.run_middleware(context, logic)

Expand All @@ -63,7 +63,7 @@ async def create_conversation(self, reference: ConversationReference, logic):
client = self.create_connector_client(reference.service_url)

resource_response = await client.conversations.create_conversation_async(parameters)
request = BotContext.apply_conversation_reference(Activity(), reference, is_incoming=True)
request = TurnContext.apply_conversation_reference(Activity(), reference, is_incoming=True)
request.conversation = ConversationAccount(id=resource_response.id)
if resource_response.service_url:
request.service_url = resource_response.service_url
Expand Down Expand Up @@ -107,7 +107,7 @@ def create_context(self, activity):
:param activity:
:return:
"""
return BotContext(self, activity)
return TurnContext(self, activity)

@staticmethod
async def parse_request(req):
Expand Down Expand Up @@ -148,7 +148,7 @@ async def validate_activity(activity: Activity):
if is_valid_activity:
return req

async def update_activity(self, context: BotContext, activity: Activity):
async def update_activity(self, context: TurnContext, activity: Activity):
"""
Replaces an activity that was previously sent to a channel. It should be noted that not all
channels support this feature.
Expand All @@ -165,7 +165,7 @@ async def update_activity(self, context: BotContext, activity: Activity):
except Exception as e:
raise e

async def delete_activity(self, context: BotContext, conversation_reference: ConversationReference):
async def delete_activity(self, context: TurnContext, conversation_reference: ConversationReference):
"""
Deletes an activity that was previously sent to a channel. It should be noted that not all
channels support this feature.
Expand All @@ -180,7 +180,7 @@ async def delete_activity(self, context: BotContext, conversation_reference: Con
except Exception as e:
raise e

async def send_activities(self, context: BotContext, activities: List[Activity]):
async def send_activities(self, context: TurnContext, activities: List[Activity]):
try:
for activity in activities:
if activity.type == 'delay':
Expand All @@ -198,7 +198,7 @@ async def send_activities(self, context: BotContext, activities: List[Activity])
except Exception as e:
raise e

async def delete_conversation_member(self, context: BotContext, member_id: str) -> None:
async def delete_conversation_member(self, context: TurnContext, member_id: str) -> None:
"""
Deletes a member from the current conversation.
:param context:
Expand All @@ -220,7 +220,7 @@ async def delete_conversation_member(self, context: BotContext, member_id: str)
except Exception as e:
raise e

async def get_activity_members(self, context: BotContext, activity_id: str):
async def get_activity_members(self, context: TurnContext, activity_id: str):
"""
Lists the members of a given activity.
:param context:
Expand All @@ -244,7 +244,7 @@ async def get_activity_members(self, context: BotContext, activity_id: str):
except Exception as e:
raise e

async def get_conversation_members(self, context: BotContext):
async def get_conversation_members(self, context: TurnContext):
"""
Lists the members of a current conversation.
:param context:
Expand Down
10 changes: 5 additions & 5 deletions libraries/botbuilder-core/botbuilder/core/bot_state.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from .bot_context import BotContext
from .turn_context import TurnContext
from .middleware_set import Middleware
from .storage import calculate_change_hash, StoreItem, StorageKeyFactory, Storage

Expand All @@ -27,7 +27,7 @@ async def on_process_request(self, context, next_middleware):
await self.write(context)
return logic_results

async def read(self, context: BotContext, force: bool=False):
async def read(self, context: TurnContext, force: bool=False):
"""
Reads in and caches the current state object for a turn.
:param context:
Expand All @@ -47,7 +47,7 @@ async def read(self, context: BotContext, force: bool=False):

return cached['state']

async def write(self, context: BotContext, force: bool=False):
async def write(self, context: TurnContext, force: bool=False):
"""
Saves the cached state object if it's been changed.
:param context:
Expand All @@ -67,7 +67,7 @@ async def write(self, context: BotContext, force: bool=False):
cached['hash'] = calculate_change_hash(cached['state'])
context.services[self.state_key] = cached

async def clear(self, context: BotContext):
async def clear(self, context: TurnContext):
"""
Clears the current state object for a turn.
:param context:
Expand All @@ -78,7 +78,7 @@ async def clear(self, context: BotContext):
cached['state'] = StoreItem()
context.services[self.state_key] = cached

async def get(self, context: BotContext):
async def get(self, context: TurnContext):
"""
Returns a cached state object or undefined if not cached.
:param context:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from .bot_context import BotContext
from .turn_context import TurnContext
from .bot_state import BotState
from .storage import Storage

Expand Down Expand Up @@ -30,7 +30,7 @@ def call_get_storage_key(context):
super(ConversationState, self).__init__(storage, call_get_storage_key)
self.namespace = namespace

def get_storage_key(self, context: BotContext):
def get_storage_key(self, context: TurnContext):
activity = context.activity
channel_id = getattr(activity, 'channel_id', None)
conversation_id = getattr(activity.conversation, 'id', None) if hasattr(activity, 'conversation') else None
Expand Down
10 changes: 5 additions & 5 deletions libraries/botbuilder-core/botbuilder/core/middleware_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
from asyncio import iscoroutinefunction
from abc import ABC, abstractmethod

from .bot_context import BotContext
from .turn_context import TurnContext


class Middleware(ABC):
@abstractmethod
def on_process_request(self, context: BotContext, next): pass
def on_process_request(self, context: TurnContext, next): pass


class AnonymousReceiveMiddleware(Middleware):
Expand All @@ -18,7 +18,7 @@ def __init__(self, anonymous_handler):
raise TypeError('AnonymousReceiveMiddleware must be instantiated with a valid coroutine function.')
self._to_call = anonymous_handler

def on_process_request(self, context: BotContext, next):
def on_process_request(self, context: TurnContext, next):
return self._to_call(context, next)


Expand All @@ -45,14 +45,14 @@ def use(self, *middleware: Middleware):
else:
raise TypeError('MiddlewareSet.use(): invalid middleware at index "%s" being added.' % idx)

async def receive_activity(self, context: BotContext):
async def receive_activity(self, context: TurnContext):
await self.receive_activity_internal(context, None)

async def on_process_request(self, context, logic):
await self.receive_activity_internal(context, None)
await logic()

async def receive_activity_with_status(self, context: BotContext, callback):
async def receive_activity_with_status(self, context: TurnContext, callback):
return await self.receive_activity_internal(context, callback)

async def receive_activity_internal(self, context, callback, next_middleware_index=0):
Expand Down
4 changes: 2 additions & 2 deletions libraries/botbuilder-core/botbuilder/core/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from abc import ABC, abstractmethod
from typing import Callable, List

from .bot_context import BotContext
from .turn_context import TurnContext


class Storage(ABC):
Expand Down Expand Up @@ -54,7 +54,7 @@ def __str__(self):
return output


StorageKeyFactory = Callable[[BotContext], str]
StorageKeyFactory = Callable[[TurnContext], str]


def calculate_change_hash(item: StoreItem) -> str:
Expand Down
4 changes: 2 additions & 2 deletions libraries/botbuilder-core/botbuilder/core/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from datetime import datetime
from typing import Coroutine, List
from copy import copy
from botbuilder.core import BotAdapter, BotContext
from botbuilder.core import BotAdapter, TurnContext
from botbuilder.schema import (ActivityTypes, Activity, ConversationAccount,
ConversationReference, ChannelAccount, ResourceResponse)

Expand Down Expand Up @@ -109,7 +109,7 @@ async def receive_activity(self, activity):
request.id = str(self._next_id)

# Create context object and run middleware
context = BotContext(self, request)
context = TurnContext(self, request)
return await self.run_middleware(context, self.logic)

async def send(self, user_says):
Expand Down
Loading
0