mrkn / pycall.rb

Calling Python functions from the Ruby language
MIT License
1.06k stars 75 forks source link

set ruby method as callback #89

Open 3rj opened 5 years ago

3rj commented 5 years ago

hi I want to use Telethon (telegram client library) as library in ruby. It works find but when I want to set ruby method to receive updates it does not work

This is the docs link about getting updates in Telethon. https://github.com/LonamiWebs/Telethon/blob/master/readthedocs/extra/basic/working-with-updates.rst

how should i do this?

def handler(update)
    puts update
end

client.add_event_handler(:handler)

I used code above, It does not work

mrkn commented 5 years ago

I'm not altogether familiar with Telethon (I didn't know that as of today), so this answer may be wrong.

You passed a symbol to Python-side. But the code in Python-side couldn't get the method handler from Ruby-side.

As I see in the document you mentioned, the sample code in the document passes a function itself:

from telethon import TelegramClient, events

async def handler(event):
    ...

with TelegramClient(...) as client:
    client.add_event_handler(handler, events.NewMessage)
    client.run_until_disconnected()

So, you need to pass the method itself, don't you think?

However, the handler function above is defined as an async function. I think that this library cannot use from PyCall as is if the handler must be an async function. Because PyCall doesn't have the way to make a Ruby's callback an async function.