Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No inbound messages showing in chat widget for azure bot, but all showing on monitor page in Dynamics #572

Open
tealtony opened this issue Jan 15, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@tealtony
Copy link

Describe the bug
I created a python azure bot (see code) based on the Echo Bot example and integrated the bot in omnichannel. I grabbed the live chat widget and opened it in w3schools or in a local index.html. In both cases, the person in the chat only sees their messages they've sent. They see none of the messages from the bot. However, if I go to Dynamics and monitor the chat, I see the chat of BOTH the customer and bot messages. I also see the messages being received in the app service log the bot is hosted on. I don't see any errors. So I'm at a loss for what could be broken for just the Live Chat Widget.

import json
import logging
from datetime import datetime, UTC

from botbuilder.core import ActivityHandler, MessageFactory, TurnContext
from botbuilder.schema import ChannelAccount

from pydantic_ai.messages import ModelRequest, ModelResponse, SystemPromptPart, TextPart

from store.store import Store

from agents.orchestrator_agent import orchestrator_agent

from utils.chat_memory import ChatMemory

from typing import List

from config import DefaultConfig
CONFIG = DefaultConfig()

logger = logging.getLogger(CONFIG.LOG_SERVICE)
from agents.orchestrator_agent import system_prompt

RAY_GREETING_MESSAGE = "Welcome to Company! How can I help you today?"

class RayBot(ActivityHandler):
    def __init__(self, store: Store):
        self.store = store

    async def on_members_added_activity(
        self,
        members_added: List[ChannelAccount],
        turn_context: TurnContext
    ):
        logger.info(f"Members added to conversation {turn_context.activity.conversation.id}")

        # Initialize chat history and context
        chat_memory = ChatMemory(turn_context, self.store)
        await chat_memory.load()

        for member in members_added:
            # Greet anyone that was not the target (recipient) of this message.
            logger.info(f"Member: {member.id} != {turn_context.activity.recipient.id}")
            if member.id != turn_context.activity.recipient.id:
                initial_messages = [
                  ModelRequest(
                      parts=[
                          SystemPromptPart(
                              content=system_prompt,
                              part_kind='system-prompt',
                          ),
                      ],
                      kind='request',
                  ),
                  ModelResponse(
                      parts=[
                          TextPart(
                              content=RAY_GREETING_MESSAGE,
                              part_kind='text',
                          )
                      ],
                      timestamp=datetime.now(UTC),
                      kind='response',
                  ),
                ]
                
                # Update chat history
                chat_memory.add_messages(initial_messages)
                await chat_memory.save()

                # Check for values
                if turn_context.activity.value is not None:
                    logger.info(f"Value received: {turn_context.activity.value}")

                # Send the greeting message
                logger.info(f"Sending greeting message: {initial_messages[1].parts[0].content}")
                activity = MessageFactory.text(initial_messages[1].parts[0].content)
                activity.delivery_mode = "bridged"
                logger.info(f"Activity: {json.dumps(activity.as_dict())}")
                await turn_context.send_activity(activity)

    async def on_message_activity(self, turn_context: TurnContext):
        logger.info(f"Message received from {turn_context.activity.from_property.id}")

        # Load chat history and context
        chat_memory = ChatMemory(turn_context, self.store)
        await chat_memory.load()

        logger.info(f"Chat history: {chat_memory.get_abridged_messages()}")
        logger.info(f"Context: {chat_memory.context}")
        
        # Process the message
        result = await orchestrator_agent.run(
            turn_context.activity.text,
            message_history=chat_memory.get_abridged_messages(),
        )

        # Update chat history
        chat_memory.add_messages(result.new_messages())
        await chat_memory.save()

        # Generate response
        response = result.data
        logger.info(f"Response: {response}")
        # Add the command context for omnichannel chat with deliveryMode, bridge, 
        activity = MessageFactory.text(response)
        activity.delivery_mode = "bridged"
        logger.info(f"Activity: {json.dumps(activity.as_dict())}")
        return await turn_context.send_activity(activity)
@tealtony tealtony added the bug Something isn't working label Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant