Skip to content

Commit

Permalink
v1 async
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddify-com committed Jul 12, 2024
1 parent 166bf39 commit ea8e380
Show file tree
Hide file tree
Showing 20 changed files with 414 additions and 363 deletions.
275 changes: 0 additions & 275 deletions hiddifypanel_bot/basebot.py

This file was deleted.

5 changes: 5 additions & 0 deletions hiddifypanel_bot/basebot/__init__.py
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
43 changes: 43 additions & 0 deletions hiddifypanel_bot/basebot/bot.py
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))
15 changes: 15 additions & 0 deletions hiddifypanel_bot/basebot/call_action_filter.py
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

19 changes: 19 additions & 0 deletions hiddifypanel_bot/basebot/custom_message_types.py
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
34 changes: 34 additions & 0 deletions hiddifypanel_bot/basebot/data_storage.py
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)
Loading

0 comments on commit ea8e380

Please sign in to comment.