hynek-urban / rocketchat-async

asyncio-based Python wrapper for the Rocket.Chat Realtime API.
MIT License
11 stars 9 forks source link

How to use send_message #8

Closed anthonymanzo closed 2 months ago

anthonymanzo commented 3 months ago

Hi, In your example, since handle_message is sync, I can't call the async send_message function in it. I've tried awaiting it, but since there is already an active loop I can't get around this. Can you help me understand how to use send_message from within the handle_message function? Thanks Tony

kato-mahiro commented 2 months ago

@anthonymanzo Hi, I just faced a similar issue and found a solution, so I thought I'd share it with you. my code was like this.

async def my_message_handler(...):

await rc.subscribe_to_channel_message(channel_id, my_message_handler) 

But as you pointed out, this code doesn't work properly So instead, I think it would be nice to write it like this.

import asyncio

async def my_message_handler(...):

def subscribe_callback(*args):
    asyncio.create_task(my_message_handler(*args))

await rc.subscribe_to_channel_message(channel_id, subscribe_callback) 

I hope this post helps you!

hynek-urban commented 2 months ago

Hi,

that's a good question - thanks @kato-mahiro for the answer :) For my use cases, I was doing the same.

I can see that it's not ideal, though. At the very least, the example in the README is lacking and maybe the callbacks should actually be async, if only to enable proper error handling. I'll leave this issue open for now and when I have some spare time, I'll try to wrap my head around it :)

anthonymanzo commented 2 months ago

Thanks to both of you for your responses. I working on a bot for a very large AI conference (30k users) that starts in a week.

TLDR; You've helped me out a ton, I'll be posting more 'issues' over the next week, I'll make some contributions from your answers in mid-May.

For the previous year's conference my bot was written in Typescript and used the Rocket.Chat SDK However, I had to run a separate container to do my LLM integrations for the bot in Python (since most LLM packages are python first and node support lags) This year, my plan was to simplify things by eliminating the Node container and running everything in Python. I am running into some issues when porting code over to Python. Thanks for reading all this, I hope you don't get annoyed with my posts as they are not really issues, at the end of the conference I'll share my code in case it helps this repo.

hynek-urban commented 2 months ago

Hey @anthonymanzo , thanks for the context; I hope that in the end, porting to Python was worth it :) In any case, I'm closing this issue - let's open another one if there's more to discuss (and I'd be happy to!).