|
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 | +from microsoft.botbuilder.schema import * |
4 | 7 | from microsoft.botframework.connector import ConnectorClient
|
5 | 8 | from microsoft.botframework.connector.auth import MicrosoftTokenAuthentication
|
6 |
| -from microsoft.botbuilder.schema import * |
7 | 9 |
|
8 | 10 | APP_ID = ''
|
9 |
| -APP_PW = '' |
10 |
| - |
11 |
| -app = Flask(__name__) |
12 |
| -app.connector = None |
13 |
| -app.credentials = MicrosoftTokenAuthentication(APP_ID, APP_PW) |
14 |
| - |
15 |
| - |
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']) |
21 |
| - |
22 |
| - if activity['type'] == 'message': |
23 |
| - response_activity = Activity( |
24 |
| - 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 |
| - message = 'Conversation update!' |
34 |
| - if 'membersAdded' in activity: |
35 |
| - message = 'Conversation update!\n\n"%s" (id: %s) joined the conversation.' %\ |
36 |
| - (activity['membersAdded'][0]['name'], |
37 |
| - activity['membersAdded'][0]['id']) |
38 |
| - elif 'membersRemoved' in activity: |
39 |
| - message = 'Conversation update!\n\nid: "%s" left the conversation.' % activity['membersRemoved'][0]['id'] |
40 |
| - response_activity = Activity( |
41 |
| - type=ActivityTypes.message, |
42 |
| - channel_id=activity['channelId'], |
43 |
| - recipient=ChannelAccount(id=activity['from']['id'], name=activity['from']['name']), |
44 |
| - from_property=ChannelAccount(id=activity['recipient']['id'], name=activity['recipient']['name']), |
45 |
| - text=message |
46 |
| - ) |
47 |
| - app.connector.conversations.send_to_conversation(activity['conversation']['id'], response_activity) |
48 |
| - return Response("{}", status=202, mimetype='application/json') |
49 |
| - else: |
50 |
| - return Response("{}", status=202, mimetype='application/json') |
| 11 | +APP_PASSWORD = '' |
| 12 | + |
| 13 | +class MyHandler(http.server.BaseHTTPRequestHandler): |
| 14 | + |
| 15 | + def __handle_conversationUpdate_activity(self, data): |
| 16 | + self.send_response(202) |
| 17 | + self.end_headers() |
| 18 | + if data['membersAdded'][0]['id'] != data['recipient']['id']: |
| 19 | + credentials = MicrosoftTokenAuthentication(APP_ID, APP_PASSWORD) |
| 20 | + connector = ConnectorClient(credentials, base_url=data['serviceUrl']) |
| 21 | + activity = Activity( |
| 22 | + type=ActivityTypes.message, |
| 23 | + channel_id=data['channelId'], |
| 24 | + recipient=ChannelAccount(id=data['from']['id'], name=data['from']['name']), |
| 25 | + from_property=ChannelAccount(id=data['recipient']['id'], name=data['recipient']['name']), |
| 26 | + text='Hello and welcome to the echo bot!') |
| 27 | + connector.conversations.send_to_conversation(data['conversation']['id'], activity) |
| 28 | + |
| 29 | + def __handle_message_activity(self, data): |
| 30 | + self.send_response(200) |
| 31 | + self.end_headers() |
| 32 | + credentials = MicrosoftTokenAuthentication(APP_ID, APP_PASSWORD) |
| 33 | + connector = ConnectorClient(credentials, base_url=data['serviceUrl']) |
| 34 | + activity = Activity( |
| 35 | + type = ActivityTypes.message, |
| 36 | + channel_id = data['channelId'], |
| 37 | + recipient = ChannelAccount(id=data['from']['id']), |
| 38 | + from_property = ChannelAccount(id=data['recipient']['id']), |
| 39 | + text = 'You said: %s' % data['text']) |
| 40 | + connector.conversations.send_to_conversation(data['conversation']['id'], activity) |
| 41 | + |
| 42 | + def __unhandled_activity(self): |
| 43 | + self.send_response(404) |
| 44 | + self.end_headers() |
| 45 | + |
| 46 | + def do_POST(self): |
| 47 | + body = self.rfile.read(int(self.headers['Content-Length&
8A62
#39;])) |
| 48 | + data = json.loads(str(body, 'utf-8')) |
| 49 | + if data['type'] == 'conversationUpdate': |
| 50 | + self.__handle_conversationUpdate_activity(data) |
| 51 | + elif data['type'] == 'message': |
| 52 | + self.__handle_message_activity(data) |
| 53 | + else: |
| 54 | + self.__unhandled_activity() |
| 55 | + |
| 56 | +try: |
| 57 | + server = http.server.HTTPServer(('localhost', 9000), MyHandler) |
| 58 | + print('Started http server') |
| 59 | + server.serve_forever() |
| 60 | +except KeyboardInterrupt: |
| 61 | + print('^C received, shutting down server') |
| 62 | + server.socket.close() |
0 commit comments