有没有办法在继续之前获得用户输入?

时间:2019-05-05 08:05:12

标签: python

我正在使用Microsoft Teams Botbuilder:https://github.com/Microsoft/botbuilder-python,尚不支持对话框botbuilder-python dialogs and conversation flow。我正在尝试找到一种解决方法,可以在继续下一个问题之前等待用户输入。

我有以下代码可以在第二个问题之前使用,由于第三个问题是一个开放式问题,因此我无法继续前进。

# Licensed under the MIT License.

import http.server
import json
import asyncio
from botbuilder.schema import (Activity, ActivityTypes, ChannelAccount)
from botframework.connector import ConnectorClient
from botframework.connector.auth import (MicrosoftAppCredentials,
                                         JwtTokenValidation, SimpleCredentialProvider)

APP_ID = 'xxxxx'
APP_PASSWORD = 'xxxx'


class BotRequestHandler(http.server.BaseHTTPRequestHandler):

    answers = []

    @staticmethod
    def __create_reply_activity(request_activity, text):
        return Activity(
            type=ActivityTypes.message,
            channel_id=request_activity.channel_id,
            conversation=request_activity.conversation,
            recipient=request_activity.from_property,
            from_property=request_activity.recipient,
            text=text,
            service_url=request_activity.service_url)

    def __handle_conversation_update_activity(self, activity):
        self.send_response(202)
        self.end_headers()
        if activity.members_added[0].id != activity.recipient.id:
            credentials = MicrosoftAppCredentials(APP_ID, APP_PASSWORD)
            reply = BotRequestHandler.__create_reply_activity(activity, 'Hello and welcome to the MS bot!')
            connector = ConnectorClient(credentials, base_url=reply.service_url)
            connector.conversations.send_to_conversation(reply.conversation.id, reply)

    def __handle_message_activity(self, activity):
        self.send_response(200)
        self.end_headers()
        credentials = MicrosoftAppCredentials(APP_ID, APP_PASSWORD)
        connector = ConnectorClient(credentials, base_url=activity.service_url)
        if activity.text == 'hello':
            self.answers.append(activity.text)
            reply = BotRequestHandler.__create_reply_activity(activity, 'Welcome to MS Bot. Would you like to proceed? ')
            connector.conversations.send_to_conversation(reply.conversation.id, reply)
        if activity.text == 'yes':
            self.answers.append(activity.text)
            reply = BotRequestHandler.__create_reply_activity(activity, 'Please summarize your issue: ')
            connector.conversations.send_to_conversation(reply.conversation.id, reply)
        if self.answers[2] is not None:
            self.answers.append(activity.text)
            reply = BotRequestHandler.__create_reply_activity(activity, 'What do you do? ')
            connector.conversations.send_to_conversation(reply.conversation.id, reply)

    def __handle_authentication(self, activity):
        credential_provider = SimpleCredentialProvider(APP_ID, APP_PASSWORD)
        loop = asyncio.new_event_loop()
        try:
            loop.run_until_complete(JwtTokenValidation.authenticate_request(
                activity, self.headers.get("Authorization"), credential_provider))
            return True
        except Exception as ex:
            self.send_response(401, ex)
            self.end_headers()
            return False
        finally:
            loop.close()

    def __unhandled_activity(self):
        self.send_response(404)
        self.end_headers()

    def do_POST(self):
        body = self.rfile.read(int(self.headers['Content-Length']))
        data = json.loads(str(body, 'utf-8'))
        activity = Activity.deserialize(data)

        if not self.__handle_authentication(activity):
            return

        if activity.type == ActivityTypes.conversation_update.value:
            self.__handle_conversation_update_activity(activity)
        elif activity.type == ActivityTypes.message.value:
            self.__handle_message_activity(activity)
        else:
            self.__unhandled_activity()


try:
    SERVER = http.server.HTTPServer(('localhost', 9000), BotRequestHandler)
    print('Started http server')
    SERVER.serve_forever()
except KeyboardInterrupt:
    print('^C received, shutting down server')
    SERVER.socket.close()

0 个答案:

没有答案