4
31
Fork
You've already forked simplematrixbotlib
21

How to run Bot commands without using the Listener class? #138

Closed
opened 2026年02月21日 03:26:30 +01:00 by LuckiestVenus · 2 comments

Describe the bug
I am trying to make a bot that uses local events in the code in order to send bot commands. To give an example, if the clock hits a certain date, the bot sends a message to remind people of someone's birthday.

However, I was not able to use code outside of the listeners for doing this properly.
Most of the listener class only pays attention to Matrix events and I was not able to figure out how to use the more arbitrary on_startup listener or the on_custom_event ones.
Another part of the problem is that I don't know if it is possible to get things like a room's id without using one of the listeners.

I thought of a few solutions depending on what's the case:

  1. Solving my problem is possible with existing tools. Updating the documentation with the trick would be massively useful for everyone (or just answering here could do)
  2. Adding a listener that can read from some condition that happens locally that's not startup (like a timer, or a boolean)

Sorry if I missed something obvious lol...

Desktop:

  • OS: CachyOS Linux 6.18.3-2-cachyos-lts
  • Python Version: 3.10
  • Package Version 2.12.3
**Describe the bug** I am trying to make a bot that uses local events in the code in order to send bot commands. To give an example, if the clock hits a certain date, the bot sends a message to remind people of someone's birthday. However, I was not able to use code outside of the listeners for doing this properly. Most of the listener class only pays attention to Matrix events and I was not able to figure out how to use the more arbitrary on_startup listener or the on_custom_event ones. Another part of the problem is that I don't know if it is possible to get things like a room's id without using one of the listeners. I thought of a few solutions depending on what's the case: 1) Solving my problem is possible with existing tools. Updating the documentation with the trick would be massively useful for everyone (or just answering here could do) 2) Adding a listener that can read from some condition that happens locally that's not startup (like a timer, or a boolean) Sorry if I missed something obvious lol... **Desktop:** - OS: CachyOS Linux 6.18.3-2-cachyos-lts - Python Version: 3.10 - Package Version 2.12.3
Owner
Copy link

Listener.on_startup would be the proper listener to use.

To get the room id without hardcoding, you could use bot.async_client.joined_rooms. Alternatively, you could store the room id in a database, file, or other persistent storage when the user uses a different command, perhaps some kind of room "registration" or "setup".

To run code triggered by a non-Matrix event, you could create a "loop" within the startup handler. To avoid blocking other listeners, it may be best to use asyncio.create_task or asyncio.ensure_future to continuously run a check for your condition.

See:
https://simple-matrix-bot-lib.readthedocs.io/en/latest/manual.html#using-the-on-startup-decorator
https://matrix-nio.readthedocs.io/en/latest/nio.html#nio.AsyncClient.joined_rooms
https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task

`Listener.on_startup` would be the proper listener to use. To get the room id without hardcoding, you could use `bot.async_client.joined_rooms`. Alternatively, you could store the room id in a database, file, or other persistent storage when the user uses a different command, perhaps some kind of room "registration" or "setup". To run code triggered by a non-Matrix event, you could create a "loop" within the startup handler. To avoid blocking other listeners, it may be best to use `asyncio.create_task` or `asyncio.ensure_future` to continuously run a check for your condition. See: https://simple-matrix-bot-lib.readthedocs.io/en/latest/manual.html#using-the-on-startup-decorator https://matrix-nio.readthedocs.io/en/latest/nio.html#nio.AsyncClient.joined_rooms https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task

Thank you, that helped a ton!

Having a starter function that uses the startup handler that calls one inside asyncio.create_task did the trick!

Thank you, that helped a ton! Having a starter function that uses the startup handler that calls one inside `asyncio.create_task` did the trick!
Sign in to join this conversation.
No Branch/Tag specified
master
v3
send_reaction
v2.13.1
v2.13.0
v2.12.2
v2.12.1
v2.12.0
v2.11.0
v2.10.3
v2.10.2
v2.10.1
v2.10.0
v2.9.1
v2.9.0
v2.7.4
v2.7.3
v2.7.2
v2.7.0
v2.6.4
v2.6.3
v2.6.2
v2.6.1
v2.6.0
v1.6.0
v2.5.1
v2.5.0
v2.4.1
v2.4.0
v2.3.0
v2.2.0
v2.1.0
v2.0.0
v1.6.1
v1.5.3
v1.5.2
v1.5.1
v1.5.0
v1.4.0
v1.3.3
v1.3.1
v1.3.0
v1.2.0
v1.3.2
v1.0.1
v1.0.0
v1.1.0
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
imbev/simplematrixbotlib#138
Reference in a new issue
imbev/simplematrixbotlib
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?