Add WIP example of database usage in bot
This commit is contained in:
parent
9d38c43576
commit
92c9072257
@ -4,3 +4,4 @@ All examples are published under the [MIT license](LICENSE).
|
|||||||
* [Hello World](helloworld/) - Very basic event handling bot that responds "Hello, World!" to all messages.
|
* [Hello World](helloworld/) - Very basic event handling bot that responds "Hello, World!" to all messages.
|
||||||
* [Echo bot](https://github.com/maubot/echo) - Basic command handling bot with !echo and !ping commands
|
* [Echo bot](https://github.com/maubot/echo) - Basic command handling bot with !echo and !ping commands
|
||||||
* [Config example](config/) - Simple example of using a config file
|
* [Config example](config/) - Simple example of using a config file
|
||||||
|
* [Database example](database/) - Simple example of using a database
|
||||||
|
39
examples/database/databasebot.py
Normal file
39
examples/database/databasebot.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
from typing import Type
|
||||||
|
|
||||||
|
from sqlalchemy import Column, String, Text, Table, MetaData, orm, func
|
||||||
|
|
||||||
|
from mautrix.types import EventType
|
||||||
|
from maubot import Plugin, MessageEvent
|
||||||
|
from maubot.handlers import event, command
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseBot(Plugin):
|
||||||
|
db: orm.Session
|
||||||
|
events: Type[Table]
|
||||||
|
|
||||||
|
async def start(self) -> None:
|
||||||
|
await super().start()
|
||||||
|
|
||||||
|
db_factory = orm.sessionmaker(bind=self.database)
|
||||||
|
self.db = orm.scoped_session(db_factory)
|
||||||
|
table_meta = MetaData(bind=self.db)
|
||||||
|
self.events = Table("event", table_meta,
|
||||||
|
Column("room_id", String(255), primary_key=True),
|
||||||
|
Column("event_id", String(255), primary_key=True),
|
||||||
|
Column("sender", String(255)),
|
||||||
|
Column("body", Text))
|
||||||
|
# In the future, there will be a proper way to include Alembic upgrades in plugins.
|
||||||
|
table_meta.create_all()
|
||||||
|
|
||||||
|
@event.on(EventType.ROOM_MESSAGE)
|
||||||
|
async def handler(self, event: MessageEvent) -> None:
|
||||||
|
self.db.add(self.events(room_id=event.room_id, event_id=event.event_id,
|
||||||
|
sender=event.sender, body=event.content.body))
|
||||||
|
|
||||||
|
@command.new("stats")
|
||||||
|
async def find(self, _: MessageEvent) -> None:
|
||||||
|
res = (self.db
|
||||||
|
.query(func.sum(self.events.event_id))
|
||||||
|
.group_by(self.events.room_id, self.events.sender)
|
||||||
|
.all())
|
||||||
|
print(res)
|
10
examples/database/maubot.yaml
Normal file
10
examples/database/maubot.yaml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
maubot: 0.1.0
|
||||||
|
id: xyz.maubot.databasebot
|
||||||
|
version: 1.0.0
|
||||||
|
license: MIT
|
||||||
|
modules:
|
||||||
|
- databasebot
|
||||||
|
main_class: DatabaseBot
|
||||||
|
|
||||||
|
# This is required for a database to be available.
|
||||||
|
database: true
|
Loading…
Reference in New Issue
Block a user