daily-co / daily-python

Daily Client SDK for Python
BSD 2-Clause "Simplified" License
35 stars 7 forks source link

Unable to send messages using send_app_message #5

Closed kylemcdonald closed 9 months ago

kylemcdonald commented 9 months ago

I tried sending and receiving messages using this simple app:

from daily import Daily, CallClient, EventHandler
from time import sleep
from itertools import count

class MyEventHandler(EventHandler):
    def on_participant_joined(self, participant):
        print('join', participant)

    def on_participant_left(self, participant, reason):
        print('leave', participant, reason)

    def on_app_message(self, message, sender):
        print('message', message, sender)

def main():
    Daily.init()
    client = CallClient(MyEventHandler())
    client.join("https://kcimc.daily.co/<room name>")
    sleep(2)
    try:
        for i in count():
            print('sending', i)
            client.send_app_message(str(i))
            sleep(5)
    except:
        pass
    client.leave()
    sleep(2)

if __name__ == '__main__':
    main()

I am able to receive messages, but not send them.

I tried using a meeting token, and I also tried sending messages directly to a participant.

After looking at the Daily JS documentation I thought I might need to do: client.send_app_message({'message': str(i)}) or even client.send_app_message(json.dumps({'message': str(i)})). But neither of those worked either.

I also checked that using '*' instead of None for participant did not fix it.

The excepted behavior is that I should be able to see chat messages from the Python app when I have joined the room from my browser.

Please help clarify what I am missing. Thanks!

aconchillo commented 9 months ago

HI @kylemcdonald .

The excepted behavior is that I should be able to see chat messages from the Python app when I have joined the room from my browser.

Unfortunately, no. Chat in Daily Prebuilt is an out of the box feature that we offer but it’s not meant to be programmatically controlled or interacted with except by end users. For people that require a different experience (for example sending messages from a bot), we recommend one of the following routes:

  1. Use the integrations API, where you can add a third party chat experience. See https://docs.daily.co/reference/daily-js/instance-methods/custom-integrations and https://www.daily.co/blog/integrate-miro-and-cometchat-in-a-daily-video-call-with-our-new-prebuilt-integrations-api/
  2. Build your own chat.

In both cases you still benefit from having Daily's Prebuilt UI instead of having to build your own which is a significant amount of work.

We should definitely clarify this in the docs to avoid this confusion. But basically app messages are internal messages on your application, and actually our chat is built on top of app messages but we currently do not expose a way to send chat messages.

aconchillo commented 9 months ago

@kylemcdonald We have talked internally and we will be adding a function to send chat messages to Daily Prebuilt.

kylemcdonald commented 9 months ago

Thanks! These hints were enough for me to find a workaround. Now I understand that send_app_message is a generic message sending system, and the chat is implemented on top of that. So I looked at the messages I was receiving from the chat, and found that I could send messages to the chat by imitating them.

Here's my solution:

client.send_app_message(
    {"room": "main-room", "event": "chat-msg", "message": str(i)}
)

In my opinion as a developer, I think either providing a lightweight wrapper for this or updating the documentation would have clarified it for me.

Thanks again.

aconchillo commented 9 months ago

Thanks! These hints were enough for me to find a workaround. Now I understand that send_app_message is a generic message sending system, and the chat is implemented on top of that. So I looked at the messages I was receiving from the chat, and found that I could send messages to the chat by imitating them.

Here's my solution:

client.send_app_message(
    {"room": "main-room", "event": "chat-msg", "message": str(i)}
)

Yep, this is it :smiley:. However, since internal things can change it's not something we were announcing/documenting. But we will make it available now so if we change anything internally users should be fine.

aconchillo commented 9 months ago

daily-python 0.3.0 now includes a send_prebuilt_chat_message to be able to send chat messages to Daily Prebuilt.