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

Legal way to get raw events #483

Closed
zba opened this issue Jan 19, 2024 · 6 comments
Closed

Legal way to get raw events #483

zba opened this issue Jan 19, 2024 · 6 comments

Comments

@zba
Copy link

zba commented Jan 19, 2024

Describe the bug
I don't find any way to make it correct to handle events for plugins

How To Reproduce
In short I wanted to make llm bot sample with mattermost and wanted to make a feature to regen on recycle reaction, so I tried to find how to get driver's events and not found any good way

Expected behavior
A Plugin has a function to call like
self.subscribe('event_name', callback) (which implement for in subscribers call callback somewhere here

event_action = post.get("event")
)

Operating Environment (please complete the following information):

  • mmpy_bot Version: master

Additional context
I solved it using this ugly way, but I think it is very bad,

class MyPlugin(Plugin):
    def __init__(self, spy_container):
        super().__init__()
        self.spy_container = spy_container # HERE
    def on_start(self):
        self.bot_instance = self.spy_container['bot'] # HERE
        self.driver.init_websocket(self._handle_event) # plugin handler router handler
    async def _handle_event(self, data):
        event = json.loads(data)
        await self.websocket_event(event) # userspace handler, 
        return await self.bot_instance.event_handler._handle_event(data) # call EventHandler's handler to process messages, etc



spy_container={} # LOOK HERE
bot = Bot(
    settings=settings,  # Either specify your settings here or as environment variables.
    plugins=[MyPlugin(spy_container)],  # Add your own plugins here.
)
spy_container['bot'] = bot
bot.run()
@unode
Copy link
Collaborator

unode commented Jan 21, 2024

Hi @zba,

For "raw" access you may want to look into python-mattermost-autodriver which is how mmpy_bot interacts with mattermost.
You can enable debug information as described in usage.

If you want to implement conversational behavior in mmpy_bot, see also #140.

@zba
Copy link
Author

zba commented Jan 21, 2024

The problem is how mmpy bot handles events, i access driver correct,. Please read example i provide. The problem, that autodriver init single time, it is not subscription by itself, so mmpy loose connection to driver if i init it in plugin, so i had to make this dirty trick to pass events back to mmpy. But if another plugin do same, 1st one will not receive events in my example. It could be easy fixed by making event proxy in eventhandler class,
May be i highlighted it not enough :) i need reactions events, so this is why i need, raw, i happy already to use message listeners and they work, while i not reinit event listener in my plugin, so i made ugly solution to pass this event back to mmpy bot :) ,

I not real pythonist, but you can use EventEmitter template (just found a lot libraries around so can't suggest any)
which provide basically template
in init
self.events = EventEmmitter()
in event receiver (_handle_event() in your function) :
self.websocket_events.emit(eventName, eventData)
in module main class init
self.websocket_events = self.event_handler.websocket_events
in MyPlugin in init
self.wesocket_events.on('reaction_add', callback)
(or for more security you can export on() and once() methods)
I not real pythonist (more js), so I not sure if it best solution or not :) but access to events any way could make me and anybody like me happy :)

@unode
Copy link
Collaborator

unode commented Jan 21, 2024

mmpy_bot currently only listens to posted events.

See #445 for another request to add support for post_edited and a suggestion on how to achieve it.
See also #377 and #386 for other discussion about other kinds of events.

Reactions would need a new approach given @listen_to doesn't make sense for them.

The point of mmpy_bot is that you don't have to interact with events at low level.
If you need that functionality, this is currently not supported without modifying mmpy_bot's source.
We could add support for different kinds of event handlers if a proposal is made and implemented.

Contributions are welcome.

@zba
Copy link
Author

zba commented Jan 22, 2024

Yes, i like i haven't too, but why not allow me also raw access as i have access to driver too ? The whole point of this issue is ask to add ability to use functions of driver not implemented in decorators.

@unode
Copy link
Collaborator

unode commented Jan 22, 2024

There may be a misunderstanding here. We aren't restricting anything in mmpy_bot.

The driver used by this project and linked above is the core that handles the requests. It supports having one event handler function. This role is taken by the event handler in mmpy_bot.

This handler is not accessible to plugins as this was not part of mmpy_bot's design. If you need access to it, you have two options:

  1. Modify mmpy_bot's event handler code to expose events.
  2. Use the driver directly, without mmpy_bot and build your own framework on top of it.

If you choose 1, you are welcome to open a pull request with your contributions. Maintainers will review and decide upon it.

@zba
Copy link
Author

zba commented Jan 22, 2024

Ah I got, i not need mmpy bot, because all it does is decorators on posts events, and i can just go with event model without decorators, hm, ok. I chose 2

@zba zba closed this as completed Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants