Skip to content

Commit

Permalink
feat: Partially major changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Simatwa committed Apr 28, 2024
1 parent b4c721f commit 4f9942f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 37 deletions.
2 changes: 1 addition & 1 deletion pytgpt_bot/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dotenv import load_dotenv
from os import environ
from pytgpt.utils import Audio
from .utils import provider_keys
from pytgpt_bot.utils import provider_keys

load_dotenv()

Expand Down
6 changes: 3 additions & 3 deletions pytgpt_bot/db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .models import Chat
from .models import session
from .config import admin_id
from pytgpt_bot.models import Chat
from pytgpt_bot.models import session
from pytgpt_bot.config import admin_id
from telebot.types import Message, CallbackQuery


Expand Down
39 changes: 35 additions & 4 deletions pytgpt_bot/filters.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from telebot.custom_filters import SimpleCustomFilter
from telebot import types
from .db import User
from .config import admin_ids
from telebot import types, TeleBot
from telebot.util import extract_command, extract_arguments
from pytgpt_bot.db import User
from pytgpt_bot.config import admin_ids


class IsActiveFilter(SimpleCustomFilter):
Expand Down Expand Up @@ -48,10 +49,40 @@ def check(self, message: types.Message | types.CallbackQuery):
return self._bot.get_chat_member(
message.message.chat.id, message.from_user.id
).status in ["creator", "administrator"]

elif message.chat.type == "private":
return True

return self._bot.get_chat_member(
message.chat.id, message.from_user.id
).status in ["creator", "administrator"]


class IsBotTaggedFilter(SimpleCustomFilter):
"""Checks if bot is tagged"""

key: str = "is_bot_tagged"

def __init__(self, bot_info: types.User):
"""Constructor
Args:
bot_info (types.User): Bot info.
"""
self.bot_info = bot_info

def check(self, message: types.Message):
if message.text:
return "@" + self.bot_info.username == message.text.split(" ")[0]

return False


class IsChatCommandFilter(SimpleCustomFilter):
"""Checks if text parsed is for tex-generation"""

key: str = "is_chat_command"

def check(self, message: types.Message):
command = extract_command(message.text)
return command == "chat" if command else True
55 changes: 29 additions & 26 deletions pytgpt_bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from functools import wraps
from sqlalchemy import text

from . import __version__, __repo__
from pytgpt_bot import __version__, __repo__

from .config import (
from pytgpt_bot.config import (
bot_token,
max_tokens,
timeout,
Expand All @@ -20,10 +20,21 @@
admin_ids,
provider,
)
from .db import User
from .utils import provider_keys, get_random_emoji, provider_map, make_delete_markup
from .models import session, Chat, create_all, drop_all
from .filters import IsActiveFilter, IsBotOwnerFilter, IsAdminFilter
from pytgpt_bot.db import User
from pytgpt_bot.utils import (
provider_keys,
get_random_emoji,
provider_map,
make_delete_markup,
)
from pytgpt_bot.models import session, Chat, create_all, drop_all
from pytgpt_bot.filters import (
IsActiveFilter,
IsBotOwnerFilter,
IsAdminFilter,
IsBotTaggedFilter,
IsChatCommandFilter,
)

chosen_provider: str = provider_map.get(provider)

Expand Down Expand Up @@ -107,8 +118,8 @@ def decorator(message: telebot.types.Message):
)
else:
logging.info(f"Serving Group - Function [{func.__name__}]")
if message.text and message.text.startswith("/") and not preserve:
message.text = " ".join(message.text.split(" ")[1:])
if not preserve:
message.text = telebot_util.extract_arguments(message.text)

if text and not message.text:
return bot.reply_to(
Expand Down Expand Up @@ -423,7 +434,7 @@ def text_to_audio(message: telebot.types.Message):
caption=message.text,
reply_markup=make_delete_markup(message),
performer=voice,
title="Text-to-Voice",
title="Text-to-Speech",
)


Expand Down Expand Up @@ -562,23 +573,13 @@ def check_current_settings(message: telebot.types.Message):
return send_long_text(message, contents, add_delete=True, parse_mode=None)


def is_action_for_chat(message: telebot.types.Message) -> bool:
splitted_text = message.text.split(" ")
if splitted_text[0].startswith("/"):
if splitted_text[0] == "/chat":
return True
else:
return False
return True


@bot.message_handler(
content_types=["text"], is_chat_active=True, func=is_action_for_chat
)
@bot.message_handler(content_types=["text"], is_chat_active=True, is_chat_command=True)
@bot.channel_post_handler(
content_types=["text"], is_chat_active=True, func=is_action_for_chat
content_types=["text"],
is_chat_active=True,
commands=["chat"],
)
@handler_formatter(text=True)
@handler_formatter()
def text_chat(message: telebot.types.Message):
"""Text generation"""
user = User(message)
Expand All @@ -599,8 +600,8 @@ def text_chat(message: telebot.types.Message):
send_long_text(message, ai_response)


@bot.message_handler(func=lambda val: True, is_chat_active=True)
@bot.channel_post_handler(func=lambda val: True, is_chat_active=True)
@bot.message_handler(is_chat_active=True)
@bot.channel_post_handler(is_chat_active=True, is_bot_tagged=True)
def any_other_action(message):
return bot.reply_to(
message,
Expand Down Expand Up @@ -629,3 +630,5 @@ def delete_callback_handler(
bot.add_custom_filter(IsBotOwnerFilter())
bot.add_custom_filter(IsAdminFilter(bot))
bot.add_custom_filter(IsActiveFilter())
bot.add_custom_filter(IsBotTaggedFilter(bot.get_me()))
bot.add_custom_filter(IsChatCommandFilter())
6 changes: 3 additions & 3 deletions pytgpt_bot/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from sqlalchemy.orm import declarative_base
from sqlalchemy import Column, Integer, Text, String, Boolean
from pytgpt.utils import Conversation
from .config import database as database_str
from .config import provider as default_provider
from .config import voice
from pytgpt_bot.config import database as database_str
from pytgpt_bot.config import provider as default_provider
from pytgpt_bot.config import voice

if not database_str:
from .utils import path_to_default_db
Expand Down

0 comments on commit 4f9942f

Please sign in to comment.