8000 Merge branch 'southworks/auth-stack-updates-sample' into southworks/a… · openvelora/botbuilder-python@68c30ea · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit 68c30ea

Browse files
committed
Merge branch 'southworks/auth-stack-updates-sample' into southworks/auth-stack-updates
2 parents 467458b + e12aa1d commit 68c30ea

File tree

8 files changed

+627
-45
lines changed

8 files changed

+627
-45
lines changed

libraries/botbuilder-ai/language_map.py

Lines changed: 541 additions & 0 deletions
Large diffs are not rendered by default.
-16 Bytes
Binary file not shown.

libraries/botbuilder-schema/setup.cfg

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
[bdist_wheel]
2-
universal=1
3-
azure-namespace-package=microsoft-botbuilder-schema
2+
universal=1
-91 Bytes
Binary file not shown.
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
[bdist_wheel]
2-
universal=1
3-
azure-namespace-package=microsoft-botframework-connector
2+
universal=1

samples/Echo.Connector.Bot/README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
#### Echo Connector Bot
22

3-
Requires [Flask][Flask] to run.
4-
5-
- To run the sample on Windows, enter the following into your command line:
3+
To run this sample:
64

75
```
8-
set FLASK_APP=main.py
9-
flask run
6+
python main.py
107
```
118

12-
[Flask]: http://flask.pocoo.org/
9+
and you can test with the Bot FRamework Emulator by connecting to http://localhost:9000

samples/Echo.Connector.Bot/main.py

Lines changed: 79 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,87 @@
1-
import json
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
23

3-
from flask import Flask, request, Response
4+
import http.server
5+
import json
6+
import asyncio
7+
from microsoft.botbuilder.schema import (Activity, ActivityTypes, ChannelAccount)
48
from microsoft.botframework.connector import ConnectorClient
5-
from microsoft.botframework.connector.auth import MicrosoftTokenAuthentication
6-
from microsoft.botbuilder.schema import *
9+
from microsoft.botframework.connector.auth import (MicrosoftAppCredentials,
10+
JwtTokenValidation, SimpleCredentialProvider)
711

812
APP_ID = ''
9-
APP_PW = ''
10-
11-
app = Flask(__name__)
12-
app.connector = None
13-
app.credentials = MicrosoftTokenAuthentication(APP_ID, APP_PW)
13+
APP_PASSWORD = ''
1414

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

16-
@app.route("/api/messages", methods=['Post'])
17-
def messages():
18-
activity = json.loads(request.data)
19-
if app.connector is None:
20-
app.connector = ConnectorClient(app.credentials, base_url=activity['serviceUrl'])
18+
def __handle_conversation_update_activity(self, activity):
19+
self.send_response(202)
20+
self.end_headers()
21+
if activity.members_added[0].id != activity.recipient.id:
22+
credentials = MicrosoftAppCredentials(APP_ID, APP_PASSWORD)
23+
connector = ConnectorClient(credentials, base_url=activity.service_url)
24+
reply = Activity(
25+
type=ActivityTypes.message,
26+
channel_id=activity.channel_id,
27+
recipient=ChannelAccount(
28+
id=activity.from_property.id,
29+
name=activity.from_property.name),
30+
from_property=ChannelAccount(
31+
id=activity.recipient.id,
32+
name=activity.recipient.name),
33+
text='Hello and welcome to the echo bot!')
34+
connector.conversations.send_to_conversation(activity.conversation.id, reply)
2135

22-
if activity['type'] == 'message':
23-
response_activity = Activity(
36+
def __handle_message_activity(self, activity):
37+
self.send_response(200)
38+
self.end_headers()
39+
credentials = MicrosoftAppCredentials(APP_ID, APP_PASSWORD)
40+
connector = ConnectorClient(credentials, base_url=activity.service_url)
41+
reply = Activity(
2442
type=ActivityTypes.message,
25-
channel_id=activity['channelId'],
26-
recipient=ChannelAccount(id=activity['from']['id'], name=activity['from']['name']),
27-
from_property=ChannelAccount(id=activity['recipient']['id'], name=activity['recipient']['name']),
28-
text='You said "%s"' % activity['text']
29-
)
30-
app.connector.conversations.send_to_conversation(activity['conversation']['id'], response_activity)
31-
return Response("{}", status=200, mimetype='application/json')
32-
elif activity['type'] == 'conversationUpdate':
33-
response_activity = Activity(
34-
type=ActivityTypes.message,
35-
channel_id=activity['channelId'],
36-
recipient=ChannelAccount(id=activity['from']['id'], name=activity['from']['name']),
37-
from_property=ChannelAccount(id=activity['recipient']['id'], name=activity['recipient']['name']),
38-
text='Conversation Update!'
39-
)
40-
app.connector.conversations.send_to_conversation(activity['conversation']['id'], response_activity)
41-
return Response("{}", status=202, mimetype='application/json')
42-
else:
43-
return Response("{}", status=202, mimetype='application/json')
43+
channel_id=activity.channel_id,
44+
recipient=ChannelAccount(id=activity.from_property.id),
45+
from_property=ChannelAccount(id=activity.recipient.id),
46+
text='You said: %s' % activity.text)
47+
connector.conversations.send_to_conversation(activity.conversation.id, reply)
48+
49+
def __handle_authentication(self, activity):
50+
loop = asyncio.new_event_loop()
51+
try:
52+
loop.run_until_complete(JwtTokenValidation.assert_valid_activity(
53+
activity, self.headers.get("Authorization"), MyHandler.credential_provider))
54+
return True
55+
except Exception as ex:
56+
self.send_response(401, ex)
57+
self.end_headers()
58+
return False
59+
finally:
60+
loop.close()
61+
62+
def __unhandled_activity(self):
63+
self.send_response(404)
64+
self.end_headers()
65+
66+
def do_POST(self):
67+
body = self.rfile.read(int(self.headers['Content-Length']))
68+
data = json.loads(str(body, 'utf-8'))
69+
activity = Activity.deserialize(data)
70+
71+
if not self.__handle_authentication(activity):
72+
return
73+
74+
if activity.type == ActivityTypes.conversation_update.value:
75+
self.__handle_conversation_update_activity(activity)
76+
elif activity.type == ActivityTypes.message.value:
77+
self.__handle_message_activity(activity)
78+
else:
79+
self.__unhandled_activity()
80+
81+
try:
82+
SERVER = http.server.HTTPServer(('localhost', 9000), MyHandler)
83+
print('Started http server')
84+
SERVER.serve_forever()
85+
except KeyboardInterrupt:
86+
print('^C received, shutting down server')
87+
SERVER.socket.close()
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/

0 commit comments

Comments
 (0)
0