|
1 |
| -import json |
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
2 | 3 |
|
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) |
4 | 8 | 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) |
7 | 11 |
|
8 | 12 | 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 = '' |
14 | 14 |
|
| 15 | +class MyHandler(http.server.BaseHTTPRequestHandler): |
| 16 | + credential_provider = SimpleCredentialProvider(APP_ID, APP_PASSWORD) |
15 | 17 |
|
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) |
21 | 35 |
|
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( |
24 | 42 | 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() |
0 commit comments