8000 working sample code but not package · sathishktk/botbuilder-python@43793aa · GitHub
[go: up one dir, main page]

Skip to content

Commit 43793aa

Browse files
committed
working sample code but not package
1 parent 0b370db commit 43793aa

File tree

8 files changed

+63
-159
lines changed

8 files changed

+63
-159
lines changed

libraries/botbuilder/microsoft/botbuilder/activity.py

Lines changed: 0 additions & 97 deletions
This file was deleted.
Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,12 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33

4+
from abc import ABC, abstractmethod
45

5-
from abc import ABCMeta, abstractmethod
66

7-
8-
class ActivityAdapter(ABCMeta):
7+
class ActivityAdapter(ABC):
98
@abstractmethod
10-
def on_receive(cls, activity):
11-
"""
12-
Handler that returns incoming activities to a single consumer. The `Bot` will set this
13-
when the adapter is passed to its constructor. Just keep in mind that should the bots
14-
adapter be replaced (like when running unit tests) this handler can end up being set
15-
back to undefined.
16-
:param activity:
17-
:return:
18-
"""
19-
pass
9+
def send(self, activities: List[Activity]): pass
2010

2111
@abstractmethod
22-
def post(cls, activities):
23-
"""
24-
Called by a consumer to send outgoing set of activities to a user.
25-
:param activities:
26-
:return:
27-
"""
28-
pass
12+
def receive(self, auth_header: str, activity: Activity): pass
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import asyncio
5+
from typing import List
6+
from microsoft.botbuilder.schema import Activity
7+
from microsoft.botframework.connector import ConnectorClient
8+
from microsoft.botframework.connector.auth import (MicrosoftAppCredentials,
9+
JwtTokenValidation, SimpleCredentialProvider)
10+
11+
12+
class BotFrameworkAdapter(ActivityAdapter):
13+
14+
def __init__(self, app_id: str, app_password: str):
15+
self._credentials = MicrosoftAppCredentials(app_id, app_password)
16+
self._credential_provider = SimpleCredentialProvider(app_id, app_password)
17+
self.on_receive = None
18+
19+
def send(self, activities: List[Activity]):
20+
for activity in activities:
21+
connector = ConnectorClient(self._credentials, base_url=activity.service_url)
22+
connector.conversations.send_to_conversation(activity.conversation.id, activity)
23+
24+
def receive(self, auth_header: str, activity: Activity):
25+
loop = asyncio.new_event_loop()
26+
try:
27+
loop.run_until_complete(JwtTokenValidation.assert_valid_activity(
28+
activity, auth_header, self._credential_provider))
29+
finally:
30+
loop.close()
31+
if self.on_receive is not None:
32+
self.on_receive(activity)

samples/Echo.Connector.Bot.Adapter/bot_framework_adapter.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
from microsoft.botframework.connector import ConnectorClient
88
from microsoft.botframework.connector.auth import (MicrosoftAppCredentials,
99
JwtTokenValidation, SimpleCredentialProvider)
10-
from receive_delegate import ReceiveDelegate
1110

1211

1312
class BotFrameworkAdapter:
1413

1514
def __init__(self, app_id: str, app_password: str):
1615
self._credentials = MicrosoftAppCredentials(app_id, app_password)
1716
self._credential_provider = SimpleCredentialProvider(app_id, app_password)
18-
self.on_receive = ReceiveDelegate(None)
17+
self.on_receive = None
1918

2019
def send(self, activities: List[Activity]):
2120
for activity in activities:
@@ -29,5 +28,5 @@ def receive(self, auth_header: str, activity: Activity):
2928
activity, auth_header, self._credential_provider))
3029
finally:
3130
loop.close()
32-
if hasattr(self, 'on_receive'):
33-
self.on_receive.call(activity)
31+
if self.on_receive is not None:
32+
self.on_receive(activity)

samples/Echo.Connector.Bot.Adapter/main.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,41 @@
55
import json
66
import asyncio
77
from microsoft.botbuilder.schema import (Activity, ActivityTypes, ChannelAccount)
8-
98
from bot_framework_adapter import BotFrameworkAdapter
10-
from receive_delegate import ReceiveDelegate
119

1210
APP_ID = ''
1311
APP_PASSWORD = ''
1412

1513

16-
class MyHandler(http.server.BaseHTTPRequestHandler):
14+
class BotRequestHandler(http.server.BaseHTTPRequestHandler):
1715

1816
@staticmethod
19-
def __create_reply_activity(request_activity: Activity, text: str):
17+
def __create_reply_activity(request_activity, text):
2018
return Activity(
2119
type=ActivityTypes.message,
2220
channel_id=request_activity.channel_id,
2321
conversation=request_activity.conversation,
24-
recipient=ChannelAccount(
25-
id=request_activity.from_property.id,
26-
name=request_activity.from_property.name),
27-
from_property=ChannelAccount(
28-
id=request_activity.recipient.id,
29-
name=request_activity.recipient.name),
22+
recipient=request_activity.from_property,
23+
from_property=request_activity.recipient,
3024
text=text,
3125
service_url=request_activity.service_url)
3226

3327
def __handle_conversation_update_activity(self, activity: Activity):
3428
self.send_response(202)
3529
self.end_headers()
3630
if activity.members_added[0].id != activity.recipient.id:
37-
self._adapter.send([MyHandler.__create_reply_activity(activity, 'Hello and welcome to the echo bot!')])
31+
self._adapter.send([BotRequestHandler.__create_reply_activity(activity, 'Hello and welcome to the echo bot!')])
3832

3933
def __handle_message_activity(self, activity: Activity):
4034
self.send_response(200)
4135
self.end_headers()
42-
self._adapter.send([MyHandler.__create_reply_activity(activity, 'You said: %s' % activity.text)])
36+
self._adapter.send([BotRequestHandler.__create_reply_activity(activity, 'You said: %s' % activity.text)])
4337

4438
def __unhandled_activity(self):
4539
self.send_response(404)
4640
self.end_headers()
4741

48-
def receive(self, activity: Activity):
42+
def on_receive(self, activity: Activity):
4943
if activity.type == ActivityTypes.conversation_update.value:
5044
self.__handle_conversation_update_activity(activity)
5145
elif activity.type == ActivityTypes.message.value:
@@ -58,12 +52,12 @@ def do_POST(self):
5852
data = json.loads(str(body, 'utf-8'))
5953
activity = Activity.deserialize(data)
6054
self._adapter = BotFrameworkAdapter(APP_ID, APP_PASSWORD)
61-
self._adapter.on_receive = ReceiveDelegate(self)
55+
self._adapter.on_receive = getattr(self, 'on_receive')
6256
self._adapter.receive(self.headers.get("Authorization"), activity)
6357

6458

6559
try:
66-
SERVER = http.server.HTTPServer(('localhost', 9000), MyHandler)
60+
SERVER = http.server.HTTPServer(('localhost', 9000), BotRequestHandler)
6761
print('Started http server')
6862
SERVER.serve_forever()
6963
except KeyboardInterrupt:

samples/Echo.Connector.Bot.Adapter/receive_delegate.py

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
../../libraries/botbuilder-schema/
2+
../../libraries/botframework-connector/

samples/Echo.Connector.Bot/main.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,43 @@
1212
APP_ID = ''
1313
APP_PASSWORD = ''
1414

15-
class MyHandler(http.server.BaseHTTPRequestHandler):
16-
credential_provider = SimpleCredentialProvider(APP_ID, APP_PASSWORD)
15+
16+
class BotRequestHandler(http.server.BaseHTTPRequestHandler):
1717

1818
@staticmethod
1919
def __create_reply_activity(request_activity, text):
2020
return Activity(
2121
type=ActivityTypes.message,
2222
channel_id=request_activity.channel_id,
2323
conversation=request_activity.conversation,
24-
recipient=ChannelAccount(
25-
id=request_activity.from_property.id,
26-
name=request_activity.from_property.name),
27-
from_property=ChannelAccount(
28-
id=request_activity.recipient.id,
29-
name=request_activity.recipient.name),
30-
text=text)
24+
recipient=request_activity.from_property,
25+
from_property=request_activity.recipient,
26+
text=text,
27+
service_url=request_activity.service_url)
3128

3229
def __handle_conversation_update_activity(self, activity):
3330
self.send_response(202)
3431
self.end_headers()
3532
if activity.members_added[0].id != activity.recipient.id:
3633
credentials = MicrosoftAppCredentials(APP_ID, APP_PASSWORD)
37-
connector = ConnectorClient(credentials, base_url=activity.service_url)
38-
reply = MyHandler.__create_reply_activity(activity, 'Hello and welcome to the echo bot!')
34+
reply = BotRequestHandler.__create_reply_activity(activity, 'Hello and welcome to the echo bot!')
35+
connector = ConnectorClient(credentials, base_url=reply.service_url)
3936
connector.conversations.send_to_conversation(reply.conversation.id, reply)
4037

4138
def __handle_message_activity(self, activity):
4239
self.send_response(200)
4340
self.end_headers()
4441
credentials = MicrosoftAppCredentials(APP_ID, APP_PASSWORD)
4542
connector = ConnectorClient(credentials, base_url=activity.service_url)
46-
reply = MyHandler.__create_reply_activity(activity, 'You said: %s' % activity.text)
43+
reply = BotRequestHandler.__create_reply_activity(activity, 'You said: %s' % activity.text)
4744
connector.conversations.send_to_conversation(reply.conversation.id, reply)
4845

4946
def __handle_authentication(self, activity):
47+
credential_provider = SimpleCredentialProvider(APP_ID, APP_PASSWORD)
5048
loop = asyncio.new_event_loop()
5149
try:
5250
loop.run_until_complete(JwtTokenValidation.assert_valid_activity(
53-
activity, self.headers.get("Authorization"), MyHandler.credential_provider))
51+
activity, self.headers.get("Authorization"), credential_provider))
5452
return True
5553
except Exception as ex:
5654
self.send_response(401, ex)
@@ -78,8 +76,9 @@ def do_POST(self):
7876
else:
7977
self.__unhandled_activity()
8078

79+
8180
try:
82-
SERVER = http.server.HTTPServer(('localhost', 9000), MyHandler)
81+
SERVER = http.server.HTTPServer(('localhost', 9000), BotRequestHandler)
8382
print('Started http server')
8483
SERVER.serve_forever()
8584
except KeyboardInterrupt:

0 commit comments

Comments
 (0)
0