generated from rochacbruno/python-project-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
166bf39
commit ea8e380
Showing
20 changed files
with
414 additions
and
363 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .roles import Role | ||
from .data_storage import DataStorage | ||
from . import middleware | ||
from .bot import bot | ||
from .custom_message_types import HCallbackQuery,HMessage,HInlineQuery |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import logging | ||
import os | ||
from telebot.async_telebot import AsyncTeleBot,logger | ||
from telebot import TeleBot, ExceptionHandler,types | ||
from telebot import asyncio_filters | ||
from telebot.asyncio_storage import StateMemoryStorage | ||
from .storage_filter import StorageFilter | ||
from .call_action_filter import CallActionFilter | ||
from .role_filter import RoleFilter | ||
from .middleware import Middleware | ||
logger.setLevel(logging.INFO) # Outputs debug messages to console. | ||
|
||
|
||
BOT_TOKEN = os.getenv("BOT_TOKEN") | ||
|
||
state_storage = StateMemoryStorage() # you can init here another storage | ||
|
||
|
||
class MyExceptionHandler(ExceptionHandler): | ||
def handle(self, exception): | ||
logger.error(exception) | ||
raise exception | ||
|
||
bot: AsyncTeleBot = AsyncTeleBot( | ||
BOT_TOKEN, | ||
# exception_handler=MyExceptionHandler(), | ||
|
||
state_storage=state_storage, | ||
) | ||
|
||
def callback(func=None,**kwargs): | ||
if func is None: func=lambda call:True | ||
return bot.callback_query_handler_orig(func,**kwargs) | ||
bot.callback_query_handler_orig=bot.callback_query_handler | ||
bot.callback_query_handler=callback | ||
|
||
|
||
bot.add_custom_filter(asyncio_filters.StateFilter(bot)) | ||
bot.add_custom_filter(asyncio_filters.TextMatchFilter()) | ||
bot.add_custom_filter(StorageFilter()) | ||
bot.add_custom_filter(CallActionFilter()) | ||
bot.add_custom_filter(RoleFilter()) | ||
bot.setup_middleware(Middleware(bot)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from telebot import asyncio_filters,types | ||
class CallActionFilter(asyncio_filters.AdvancedCustomFilter): | ||
|
||
|
||
key = 'call_action' | ||
|
||
async def check(self, obj, val): | ||
if not isinstance(obj,types.CallbackQuery): | ||
return | ||
if not obj.data:return | ||
action= obj.data.split(":")[0] | ||
if isinstance(val ,list) or isinstance(val ,set): | ||
return action in val | ||
return action==val | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from telebot import types | ||
from . import DataStorage,Role | ||
from hiddifypanel_bot.hiddifyapi import HiddifyApi | ||
class BaseHMessage: | ||
hapi: "HiddifyApi" | ||
db: DataStorage | ||
chat_id: int | ||
user_id: int | ||
role:Role | ||
lang: str | ||
|
||
class HMessage(types.Message,BaseHMessage): | ||
pass | ||
|
||
class HCallbackQuery(types.CallbackQuery): | ||
message:HMessage | ||
|
||
class HInlineQuery(types.InlineQuery,BaseHMessage): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
class DataStorage: | ||
def __init__(self, bot, user_id, chat_id): | ||
self.user_id = user_id | ||
self.chat_id = chat_id | ||
self.bot=bot | ||
|
||
|
||
async def get_state(self) -> str: | ||
return await self.bot.get_state(self.user_id, self.chat_id) | ||
|
||
async def set_state(self, state: str): | ||
await self.bot.set_state(self.user_id, state, self.chat_id) | ||
|
||
async def __getitem__(self, key,defv=None): | ||
async with self.bot.retrieve_data(self.user_id, self.chat_id) as storage: | ||
if not storage: | ||
return defv | ||
return storage.get(key,defv) | ||
|
||
async def all(self): | ||
async with self.bot.retrieve_data(self.user_id, self.chat_id) as storage: | ||
return storage | ||
|
||
async def get(self, key,defv=None): | ||
return self.__getitem__(key,defv) | ||
|
||
async def __setitem__(self, key, value): | ||
await self.bot.add_data(self.user_id, self.chat_id, **{key: value}) | ||
|
||
async def set(self, k=None, v=None,**kwargs): | ||
if k is not None: | ||
kwargs[k]=v | ||
await self.bot.add_data(self.user_id, self.chat_id, **kwargs) |
Oops, something went wrong.