Skip to content Skip to sidebar Skip to footer

Get The Active Dialog Id In Botframework Python - Dispatch Model With Multiple Dialog And Qna Maker

My bot processes incoming user messages and takes action based on the intent. For simple, one shot answers that do not require multiple passes between users and the bot, it works w

Solution 1:

Finally, I'm able to do exactly what I want. The Python SDK and the community around is not as mature as the .net one which is why it took a lot more time than it actually should. I followed a .Net sample on YouTubeGithub Repo. Checking if a dialog is active or seemed so straightforward after watching this sample. Below is the Python implementation. What the below code does is that, it creates a DialogSet when the bot is initiated and then a DialogContext. It checks if there is any existing dialog that can be run in the on_message_activity method, which if true, continues the old dialog or else, sends the message to LuisHelper Class to detect the intent and initiate the dialog based on intent.

Bot

import os
import json

from botbuilder.core import ActivityHandler, ConversationState, UserState, TurnContext, StatePropertyAccessor, MessageFactory
from botbuilder.dialogs import Dialog, DialogSet, DialogTurnStatus
from botbuilder.schema import Attachment,ChannelAccount
from helpers.luis_helper import LuisDispatchResult
from helpers.qna_helper import QnAHelper,QnAMakerEndpoint
from dialogs import NameDialog, UserProfileDialog
from typing importListfrom botbuilder.ai.qna import QnAMaker,QnAMakerEndpoint, QnAMakerOptions

from config import DefaultConfig

CONFIG = DefaultConfig()

classBot(ActivityHandler):
    def__init__(
        self,
        conversation_state: ConversationState,
        user_state: UserState
    ):
        if conversation_state isNone:
            raise Exception(
                "[DialogBot]: Missing parameter. conversation_state is required"
            )
        if user_state isNone:
            raise Exception("[DialogBot]: Missing parameter. user_state is required")


        self.conversation_state = conversation_state
        self.user_state = user_state

        self.accessor = self.conversation_state.create_property("DialogState")
        self.dialog_set = DialogSet(self.accessor)
        self.dialog_context = None

        self.app_id = CONFIG.LUIS_APP_ID
        self.api_key = CONFIG.LUIS_API_KEY
        self.host = "https://" + CONFIG.LUIS_API_HOST_NAME

    asyncdefon_turn(self, turn_context: TurnContext):
        awaitsuper().on_turn(turn_context)

        # Save any state changes that might have occurred during the turn.await self.conversation_state.save_changes(turn_context, False)
        await self.user_state.save_changes(turn_context, False)


    asyncdefon_members_added_activity(
        self, members_added: List[ChannelAccount], turn_context: TurnContext
    ):
        for member in members_added:
            # Greet anyone that was not the target (recipient) of this message.# To learn more about Adaptive Cards, see https://aka.ms/msbot-adaptivecards for more details.if member.id != turn_context.activity.recipient.id:
                welcome_card = self.create_adaptive_card_attachment()
                response = MessageFactory.attachment(welcome_card)
                await turn_context.send_activity(response)

    # Load attachment from file.defcreate_adaptive_card_attachment(self):
        relative_path = os.path.abspath(os.path.dirname(__file__))
        path = os.path.join(relative_path, "cards/welcomeCard.json")
        withopen(path) as in_file:
            card = json.load(in_file)

        return Attachment(
            content_type="application/vnd.microsoft.card.adaptive", content=card
        )

    asyncdefon_message_activity(self, turn_context: TurnContext):        
        
        self.dialog_context = await self.dialog_set.create_context(turn_context)
        results = await self.dialog_context.continue_dialog()

        if results.status == DialogTurnStatus.Empty:
            topIntent,childIntent,childEntities = LuisDispatchResult().getLuisDispatchResult(self.app_id,self.api_key,self.host,turn_context.activity.text)
            print(topIntent,childIntent,childEntities )
            await self._dispatch_to_top_intent(turn_context, topIntent,childIntent,childEntities)

    asyncdef_dispatch_to_top_intent(
        self, turn_context: TurnContext, topIntent,childIntent,childEntities
    ):
        if topIntent == "l_myluisapp":
            await self._process_luis_queries(
                turn_context, childIntent,childEntities
            )
        elif topIntent == "q_mybotqna":
            await self._process_sample_qna(turn_context)
        else:
            await turn_context.send_activity(f"Dispatch unrecognized intent: {topIntent}.")

    asyncdef_process_luis_queries(
        self, turn_context: TurnContext, childIntent,childEntities
    ):

        if childIntent == "name":
            dialog = NameDialog(self.user_state)
            ifnotawait self.dialog_set.find(dialog.id):
                self.dialog_set.add(dialog)            
            await self.dialog_context.begin_dialog(dialog.id)
        elif childIntent == "place":
            dialog = UserProfileDialog(self.user_state)
            ifnotawait self.dialog_set.find(dialog.id):
                self.dialog_set.add(dialog)            
            await self.dialog_context.begin_dialog(dialog.id)
        elif childIntent == "AGE":
            dialog = NameDialog(self.user_state)
            ifnotawait self.dialog_set.find(dialog.id):
                self.dialog_set.add(dialog)            
            await self.dialog_context.begin_dialog(dialog.id)
        else:
            await turn_context.send_activity(MessageFactory.text("No suitable intent detected, Checking QnA..."))
            await self._process_sample_qna(turn_context)
            

    asyncdef_process_sample_qna(self, turn_context: TurnContext):

        results = await QnAMaker(QnAMakerEndpoint(CONFIG.QNA_KNOWLEDGEBASE_ID,CONFIG.QNA_ENDPOINT_KEY,CONFIG.QNA_ENDPOINT_HOST),QnAMakerOptions(score_threshold=CONFIG.QNA_THRESHOLD)).get_answers(turn_context)

        # results = await self.qna_maker.get_answers(turn_context)if results:
            await turn_context.send_activity(results[0].answer)
        else:
            await turn_context.send_activity(
                "Sorry, could not find an answer in the Q and A system."
            )

app.py

from quart import Quart, request, Response
from botbuilder.core import (
    BotFrameworkAdapterSettings,
    ConversationState,
    MemoryStorage,
    UserState,
)
from botbuilder.schema import Activity

from bot import Bot

from adapter_with_error_handler import AdapterWithErrorHandler

app = Quart(__name__, instance_relative_config=True)
# app.config.from_object("config.DefaultConfig")from config import DefaultConfig

CONFIG = DefaultConfig()

# Create adapter.# See https://aka.ms/about-bot-adapter to learn more about how bots work.
SETTINGS = BotFrameworkAdapterSettings(CONFIG.APP_ID, 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
BOT = Bot(CONVERSATION_STATE, USER_STATE)



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

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

    try:
        await ADAPTER.process_activity(activity, auth_header, BOT.on_turn)
        return Response("", status=201)
    except Exception as exception:
        raise exception


@app.route("/", methods=["GET"])asyncdefhomepage():
    try:
        return"Yes I'm working brother."except Exception as exception:
        raise exception
    
if __name__ == "__main__":
    try:
        app.run()  # nosec debugexcept Exception as exception:
        raise exception

A Sample Dialog - Name Dialog

from botbuilder.dialogs import (
    ComponentDialog,
    WaterfallDialog,
    WaterfallStepContext,
    DialogTurnResult,
)
from botbuilder.dialogs.prompts import (
    TextPrompt,
    NumberPrompt,
    ChoicePrompt,
    ConfirmPrompt,
    AttachmentPrompt,
    PromptOptions,
    PromptValidatorContext,
)
from botbuilder.dialogs.choices import Choice
from botbuilder.core import MessageFactory, UserState

from data_models import Name


class NameDialog(ComponentDialog):

    def __init__(self, user_state: UserState):
        super(NameDialog, self).__init__(NameDialog.__name__)

        self.name_accessor = user_state.create_property("Name")

        self.add_dialog(
            WaterfallDialog(
                WaterfallDialog.__name__,
                [
                    self.name_step,
                    self.summary_step
                ],
            )
        )
        self.add_dialog(
            TextPrompt(TextPrompt.__name__)
        )

        self.initial_dialog_id = WaterfallDialog.__name__


    async def name_step(self, step_context: WaterfallStepContext) -> DialogTurnResult:

        returnawait step_context.prompt(
            TextPrompt.__name__,
            PromptOptions(prompt=MessageFactory.text("My name is Hidimbi. What's yours ?")),
        )
        # User said "no" so we will skip the nextstep. Give -1as the age.
        # returnawait step_context.next(-1)


    async def summary_step(
        self, step_context: WaterfallStepContext
    ) -> DialogTurnResult:
        if step_context.result:
            # Get the current profile objectfrom user state.  Changes to it
            # will saved during Bot.on_turn.
            
            msg = f"Great name {step_context.result}."await step_context.context.send_activity(MessageFactory.text(msg))

        else:
            await step_context.context.send_activity(
                MessageFactory.text("Thanks. Your data is not saved")
            )

        # WaterfallStep always finishes with the endof the Waterfall orwith another
        # dialog, here it is the end.
        returnawait step_context.end_dialog()

    @staticmethod
    async def material_prompt_validator(prompt_context: PromptValidatorContext) -> bool:
        # This condition is our validation rule. You can also change the value at this point.
        return (
            prompt_context.recognized.succeeded
            and0 < prompt_context.recognized.value < 150
        )

Post a Comment for "Get The Active Dialog Id In Botframework Python - Dispatch Model With Multiple Dialog And Qna Maker"