PaiGram/utils/bot.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

47 lines
1.3 KiB
Python
Raw Normal View History

2022-11-29 04:50:24 +00:00
import json
from typing import List, cast, Union
2022-07-30 12:53:28 +00:00
2022-11-29 04:50:24 +00:00
from telegram import Chat
2022-07-30 12:53:28 +00:00
from telegram.ext import CallbackContext
2022-11-29 04:50:24 +00:00
from core.base.redisdb import RedisDB
from core.bot import bot
redis_db = bot.services.get(RedisDB)
redis_db = cast(RedisDB, redis_db)
async def get_chat(chat_id: Union[str, int], ttl: int = 86400) -> Chat:
if not redis_db:
return await bot.app.bot.get_chat(chat_id)
qname = f"bot:chat:{chat_id}"
data = await redis_db.client.get(qname)
if data:
json_data = json.loads(data)
return Chat.de_json(json_data, bot.app.bot)
chat_info = await bot.app.bot.get_chat(chat_id)
await redis_db.client.set(qname, chat_info.to_json())
await redis_db.client.expire(qname, ttl)
return chat_info
2022-11-29 04:50:24 +00:00
2022-07-30 12:53:28 +00:00
2022-12-01 02:27:27 +00:00
def get_args(context: CallbackContext) -> List[str]:
2022-07-30 12:53:28 +00:00
args = context.args
match = context.match
if args is None:
if match is not None:
groups = match.groups()
2022-12-01 02:27:27 +00:00
command = groups[0]
if command:
temp = []
command_parts = command.split(" ")
for command_part in command_parts:
if command_part:
temp.append(command_part)
return temp
return []
2022-07-30 12:53:28 +00:00
else:
if len(args) >= 1:
return args
return []