How can I sendText from human participant to agent participant

I’m using Whatsapp Connector so that whenever a user sends a message during the call, I want that message the user sent via whatsapp chat to be sent to the agent using sendText ( Sending text | LiveKit Documentation ).

This sending of text is triggered by a webhook. So when I receive a specific webhook, I’m triggering to send the text to the agent. Here is how the backend server is sending the text to the agent.

def create_room_admin_token(room: str) -> str:
  token = api.AccessToken(os.getenv('LIVEKIT_API_KEY'),
                          os.getenv('LIVEKIT_API_SECRET')) \
      .with_identity("Token-Generator") \
      .with_name("Kamal") \
      .with_grants(api.VideoGrants(
          room_join=True,
          room_admin=True,
          can_publish=True,
          can_subscribe=True,
          room=room)).to_jwt()

  return token
async def publish_whatsapp_message_to_room(
  content: PublishWhatsappMessage
):

    room = rtc.Room()

    try:
        # Connect backend participant to the room
        await room.connect(
            os.getenv("LIVEKIT_URL"),
            create_room_admin_token(content.room_name),
        )

        # Publish text message to lk.chat
        info = await room.local_participant.send_text(
            content.message_text,
            topic="lk.chat",
        )
        return f"Sent text with stream ID: {info.stream_id}"

    finally:
        await room.disconnect()

In my testing, I have seen that the text is actually being sent because I see this in my logs:

“Sent text with stream ID: cee1a037-5e51-4552-961e-ed5563a8add8”

But I have noticed that the text is being sent from the agent to the human. I have noticed this when testing this in agent console. In my case i sent a tracking number. If you check the image below, it shows that the image is sent from the agent to the human.

Because of this, the agent doesn’t know that it has received text and can’t use the text for further action.

I would like to know, how can I specify that when sending text, send the text from the human participant to the agent participant.

Hey Kamal,

The issue is room.local_participant. is not your WhatsApp participant, it will be a different participant since it used a different token.

The best solution I believe would be to still send the message with send_text but use a different topic name. Then register in your Agent for that new stream and inject it into the conversation using generate_reply: Text and transcriptions | LiveKit Documentation.

Thanks Darryn for the guidance.

After further testing, I have felt that using SendData (Room service API | LiveKit Documentation) is the reliable way to send external data to the livekit agent to inform the agent about updates. I’m using session.generate_reply to inject the conversation.