Beispiel #1
0
class BotConfig(AppConfig):
    """ Bot initialization """
    name = 'bots'
    appConfig = config.DefaultConfig

    SETTINGS = BotFrameworkAdapterSettings(appConfig.APP_ID, appConfig.APP_PASSWORD)
    ADAPTER = BotFrameworkAdapter(SETTINGS)
    LOOP = asyncio.get_event_loop()

    # Create MemoryStorage, UserState and ConversationState
    memory = MemoryStorage()
    user_state = UserState(memory)
    conversation_state = ConversationState(memory)

    dialog = MainDialog(appConfig)
    bot = DialogAndWelcomeBot(conversation_state, user_state, dialog)

    async def on_error(self, context: TurnContext, error: Exception):
        """
        Catch-all for errors.
        This check writes out errors to console log
        NOTE: In production environment, you should consider logging this to Azure
        application insights.
        """
        print(f'\n [on_turn_error]: { error }', file=sys.stderr)
        # Send a message to the user
        await context.send_activity('Oops. Something went wrong!')
        # Clear out state
        await self.conversation_state.delete(context)

    def ready(self):
        self.ADAPTER.on_turn_error = self.on_error
Beispiel #2
0
def main():
    parse_command_line()

    # Create adapter.
    # See https://aka.ms/about-bot-adapter to learn more about how bots work.
    settings = BotFrameworkAdapterSettings(options.app_id,
                                           options.app_password)

    # Create MemoryStorage, UserState and ConversationState
    memory = MemoryStorage()
    user_state = UserState(memory)
    conversation_state = ConversationState(memory)

    # Create adapter.
    # See https://aka.ms/about-bot-adapter to learn more about how bots work.
    adapter = AdapterWithErrorHandler(settings, conversation_state)

    # Create dialogs and Bot
    recognizer = FlightBookingRecognizer(options)
    booking_dialog = BookingDialog()
    dialog = MainDialog(recognizer, booking_dialog)
    bot = DialogAndWelcomeBot(conversation_state, user_state, dialog)

    app = tornado.web.Application(
        [
            (r"/api/messages", MessageHandler, dict(adapter=adapter, bot=bot)),
        ],
        debug=options.debug,
    )
    app.listen(options.port)
    tornado.ioloop.IOLoop.current().start()
    async def intro_step(
            self, step_context: WaterfallStepContext) -> DialogTurnResult:
        """Initial prompt."""
        result = self.sum_bot.update_state_reply(
            step_context.context.activity.text)
        if (result == ''):
            return await step_context.context.send_activity(
                DialogAndWelcomeBot.create_welcome_response(
                    step_context.context.activity))

        else:
            return await step_context.prompt(
                TextPrompt.__name__,
                PromptOptions(prompt=MessageFactory.text(result)),
            )
Beispiel #4
0
SETTINGS = BotFrameworkAdapterSettings(APP.config["APP_ID"], APP.config["APP_PASSWORD"])

# Create MemoryStorage, UserState and ConversationState
MEMORY = MemoryStorage()
USER_STATE = UserState(MEMORY)
CONVERSATION_STATE = ConversationState(MEMORY)

# Create adapter.
# See https://aka.ms/about-bot-adapter to learn more about how bots work.
ADAPTER = AdapterWithErrorHandler(SETTINGS, CONVERSATION_STATE)

# Create dialogs and Bot
RECOGNIZER = FlightBookingRecognizer(APP.config)
BOOKING_DIALOG = BookingDialog()
DIALOG = MainDialog(RECOGNIZER, BOOKING_DIALOG)
BOT = DialogAndWelcomeBot(CONVERSATION_STATE, USER_STATE, DIALOG)


# Listen for incoming requests on /api/messages.
@APP.route("/api/messages", methods=["POST"])
def messages():
    # Main bot message handler.
    if "application/json" in request.headers["Content-Type"]:
        body = request.json
    else:
        return Response(status=415)

    activity = Activity().deserialize(body)
    auth_header = (
        request.headers["Authorization"] if "Authorization" in request.headers else ""
    )
# Set the error handler on the Adapter.
# In this case, we want an unbound method, so MethodType is not needed.
ADAPTER.on_turn_error = on_error

# Create MemoryStorage, UserState and ConversationState
MEMORY = MemoryStorage()
USER_STATE = UserState(MEMORY)
CONVERSATION_STATE = ConversationState(MEMORY)

# Create telemetry client
INSTRUMENTATION_KEY = app.config["APPINSIGHTS_INSTRUMENTATION_KEY"]
TELEMETRY_CLIENT = ApplicationInsightsTelemetryClient(INSTRUMENTATION_KEY)

# Create dialog and Bot
DIALOG = MainDialog(app.config, telemetry_client=TELEMETRY_CLIENT)
BOT = DialogAndWelcomeBot(CONVERSATION_STATE, USER_STATE, DIALOG,
                          TELEMETRY_CLIENT)


# Listen for incoming requests on /api/messages.
@app.route("/api/messages", methods=["POST"])
def messages():
    # Main bot message handler.
    if "application/json" in request.headers["Content-Type"]:
        body = request.json
    else:
        return Response(status=415)

    activity = Activity().deserialize(body)
    auth_header = (request.headers["Authorization"]
                   if "Authorization" in request.headers else "")
Beispiel #6
0
loop = asyncio.get_event_loop()
app = Flask(__name__, instance_relative_config=True)
app.config.from_object('config.DefaultConfig')

SETTINGS = BotFrameworkAdapterSettings(app.config['APP_ID'],
                                       app.config['APP_PASSWORD'])
ADAPTER = BotFrameworkAdapter(SETTINGS)

# Create MemoryStorage, UserState and ConversationState
memory = MemoryStorage()

user_state = UserState(memory)
conversation_state = ConversationState(memory)

dialog = MainDialog(app.config)
bot = DialogAndWelcomeBot(conversation_state, user_state, dialog)


@app.route('/api/messages', methods=['POST'])
def messages():

    if request.headers['Content-Type'] == 'application/json':
        body = request.json
    else:
        return Response(status=415)

    activity = Activity().deserialize(body)
    auth_header = request.headers[
        'Authorization'] if 'Authorization' in request.headers else ''

    async def aux_func(turn_context):