Base core for telegram bots.
Project description
Description
Just base core for simple telegram bots.
How to create a bot
class AwesomeBot(Bot):
def __init__(self, token: str):
super().__init__(token)
def init_controllers(self):
UserController(self)
def handle(self, e):
self.send_message(12345, traceback.format_exc())
return True
class UserController(Controller):
def __init__(self, bot: Bot):
super().__init__(bot)
@message_handler(commands=["start"], chat_types=["private"])
def start_handler(self, message: Message):
self.bot.send_message(message.chat.id, "Hello!")
How to use database
db = DataBase("users.db", "structure.sql")
db.update("CREATE TABLE test (id INTEGER PRIMARY KEY, x TEXT)")
db.update("INSERT INTO test (id, x) VALUES (?, ?)", 0, "some text")
print(db.fetchall("SELECT * FROM test"))
print(db.fetchone("SELECT * FROM test WHERE id = ?", 0))
You can provide structure.sql to handle the case where the database doesn't exist. It should contain some scripts to define tables or any other what you need.
How to use service
database.update("CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, money REAL)")
class UserInfo(ModelInfo):
id: int
username: str
money: float
@classmethod
def table(cls):
return "users"
@classmethod
def fields(cls):
return "id", "username", "money"
class UserDao(DAO):
def __init__(self, database: DataBase):
super().__init__(database, UserInfo)
def find_by_username(self, username: str):
return self.database.fetchone("SELECT * FROM users WHERE username = ?", username)
def users_without_money():
return self.database.fetchall("SELECT * FROM users WHERE money = 0")
class UserService(Service[UserInfo, UserDao]):
def __init__(self, dao: UserDao):
super().__init__(UserInfo, dao)
def find_by_username(self, username: str):
return self.to_object(self.dao.find_by_username(username))
def users_without_money(self, user_id: int):
return self.to_objects(self.dao.users_without_money())
service = UserService(UserDao(database))
user_id = service.create(username="some_username", money=10)
print(service.getall())
service.delete(user_id)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
BotCore-1.1.4.tar.gz
(4.6 kB
view details)
File details
Details for the file BotCore-1.1.4.tar.gz
.
File metadata
- Download URL: BotCore-1.1.4.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d16b536e3eae028ab1ee5e75ed23f914d15027908fd4ad597c4521f8afd75e8 |
|
MD5 | b3ab31225e8311e5babf12603b2b6750 |
|
BLAKE2b-256 | d32e593826ef21517d027c987380424aa5ba171ac3aff3e31ac893a608b4ad68 |