Verified Commit 90e7d2fc authored by GeloMyrtol's avatar GeloMyrtol

added methods to set and reset configs in leave_chat, small_commands and infofetch

parent cb54fcb5
from __future__ import annotations
from telegram import Bot, ReplyKeyboardRemove, Update
from telegram.ext import CommandHandler, Handler
from gmb.pollingbot import PollBotExt
from gmb.pollingbot.extension import PollBotExt
from typing import List
from gmb.telegram_util import send_custom_reply
from .infofetchbot_config import InfoFetchConfig
......@@ -18,7 +18,7 @@ class InfoFetchConfigExt(PollBotExt):
def set_update_time(self: InfoFetchConfigExt, bot: Bot, update: Update) -> None:
# TODO remove command from message?
new_time = update.message.text
new_time = " ".join(update.message.text.split(" ")[1:])
try:
new_time = int(new_time)
except ValueError:
......@@ -42,7 +42,7 @@ class InfoFetchConfigExt(PollBotExt):
def set_prio_standard(self: InfoFetchConfigExt,
bot: Bot, update: Update) -> None:
# TODO remove command from message?
new_prio = update.message.text
new_prio = " ".join(update.message.text.split(" ")[1:])
entry = InfoFetchConfig.get_entry()
entry.prio_standard = new_prio
entry.save()
......
......@@ -5,12 +5,12 @@ from typing import List
import telegram
from peewee import BooleanField, CharField, TextField
from telegram import (Bot, InlineKeyboardButton, InlineKeyboardMarkup, Message,
Update, CommandHandler)
from telegram.ext import BaseFilter, Handler, MessageHandler
Update)
from telegram.ext import BaseFilter, Handler, MessageHandler, CommandHandler
from gmb.config.db_config import ChatsConfig
from gmb.database.basemodel import BaseModel
from gmb.telegram_util import build_menu, send_custom_reply
from gmb.telegram_util import build_menu, send_custom_reply, reset_config_member, set_config_member
from .extension import PollBotExt
......@@ -47,7 +47,6 @@ class LeaveChatConfigExt(PollBotExt):
]
def toggle_leave_chats(self: LeaveChatConfigExt, bot: Bot, update: Update) -> None:
# TODO remove command from message?
entry = LeaveChatConfig.get_entry()
entry.leave_unknown_chats = not entry.leave_unknown_chats
entry.save()
......@@ -59,10 +58,8 @@ class LeaveChatConfigExt(PollBotExt):
def reset_leave_chats(self: LeaveChatConfigExt, bot: Bot, update: Update) -> None:
# TODO remove command from message?
entry = LeaveChatConfig.get_entry()
entry.leave_unknown_chats = LeaveChatConfig.leave_unknown_chats.default
entry.save()
if entry.leave_unknown_chats:
val = reset_config_member(LeaveChatConfig, "leave_unknown_chats")
if val:
msg = "Leave chats is enabled."
else:
msg = "Leave chats is disabled."
......@@ -70,96 +67,61 @@ class LeaveChatConfigExt(PollBotExt):
def set_leave_message(self: LeaveChatConfigExt,
bot: Bot, update: Update) -> None:
# TODO remove command from message?
new_msg = update.message.text
entry = LeaveChatConfig.get_entry()
entry.leave_message = new_msg
entry.save()
set_config_member(LeaveChatConfig, "leave_message", update)
msg = "Leave message updated."
send_custom_reply(bot, update, msg)
def reset_leave_message(self: LeaveChatConfigExt,
bot: Bot, update: Update) -> None:
new_msg = LeaveChatConfig.leave_message.default
entry = LeaveChatConfig.get_entry()
entry.leave_message = new_msg
entry.save()
reset_config_member(LeaveChatConfig, "leave_message")
msg = "Start message resetted."
send_custom_reply(bot, update, msg)
def set_leave_button_1_text(self: LeaveChatConfigExt,
bot: Bot, update: Update) -> None:
# TODO remove command from message?
new_msg = update.message.text
entry = LeaveChatConfig.get_entry()
entry.leave_button_1_text = new_msg
entry.save()
set_config_member(LeaveChatConfig, "leave_button_1_text", update)
msg = "Leave button 1 text updated."
send_custom_reply(bot, update, msg)
def reset_leave_button_1_text(self: LeaveChatConfigExt,
bot: Bot, update: Update) -> None:
new_msg = LeaveChatConfig.leave_button_1_text.default
entry = LeaveChatConfig.get_entry()
entry.leave_button_1_text = new_msg
entry.save()
reset_config_member(LeaveChatConfig, "leave_button_1_text")
msg = "Leave button 1 text resetted."
send_custom_reply(bot, update, msg)
def set_leave_button_1_link(self: LeaveChatConfigExt,
bot: Bot, update: Update) -> None:
# TODO remove command from message?
new_msg = update.message.text
entry = LeaveChatConfig.get_entry()
entry.leave_button_1_link = new_msg
entry.save()
set_config_member(LeaveChatConfig, "leave_button_1_link", update)
msg = "Leave button 1 link updated."
send_custom_reply(bot, update, msg)
def reset_leave_button_1_link(self: LeaveChatConfigExt,
bot: Bot, update: Update) -> None:
new_msg = LeaveChatConfig.leave_button_1_link.default
entry = LeaveChatConfig.get_entry()
entry.leave_button_1_link = new_msg
entry.save()
reset_config_member(LeaveChatConfig, "leave_button_1_link")
msg = "Leave button 1 link resetted."
send_custom_reply(bot, update, msg)
def set_leave_button_2_text(self: LeaveChatConfigExt,
bot: Bot, update: Update) -> None:
# TODO remove command from message?
new_msg = update.message.text
entry = LeaveChatConfig.get_entry()
entry.leave_button_2_text = new_msg
entry.save()
set_config_member(LeaveChatConfig, "leave_button_2_text", update)
msg = "Leave button 2 text updated."
send_custom_reply(bot, update, msg)
def reset_leave_button_2_text(self: LeaveChatConfigExt,
bot: Bot, update: Update) -> None:
new_msg = LeaveChatConfig.leave_button_2_text.default
entry = LeaveChatConfig.get_entry()
entry.leave_button_2_text = new_msg
entry.save()
reset_config_member(LeaveChatConfig, "leave_button_2_text")
msg = "Leave button 2 text resetted."
send_custom_reply(bot, update, msg)
def set_leave_button_2_link(self: LeaveChatConfigExt,
bot: Bot, update: Update) -> None:
# TODO remove command from message?
new_msg = update.message.text
entry = LeaveChatConfig.get_entry()
entry.leave_button_2_link = new_msg
entry.save()
set_config_member(LeaveChatConfig, "leave_button_2_link", update)
msg = "Leave button 2 link updated."
send_custom_reply(bot, update, msg)
def reset_leave_button_2_link(self: LeaveChatConfigExt,
bot: Bot, update: Update) -> None:
new_msg = LeaveChatConfig.leave_button_2_link.default
entry = LeaveChatConfig.get_entry()
entry.leave_button_2_link = new_msg
entry.save()
reset_config_member(LeaveChatConfig, "leave_button_2_link")
msg = "Leave button 2 link resetted."
send_custom_reply(bot, update, msg)
......
......@@ -7,7 +7,7 @@ from telegram import Bot, ReplyKeyboardRemove, Update
from telegram.ext import CommandHandler, Handler
from gmb.database.basemodel import BaseModel
from gmb.telegram_util import send_custom_reply
from gmb.telegram_util import send_custom_reply, reset_config_member, set_config_member
from .extension import PollBotExt
......@@ -43,74 +43,49 @@ class SmallCommandConfigExt(PollBotExt):
def set_start_message(self: SmallCommandConfigExt,
bot: Bot, update: Update) -> None:
# TODO remove command from message?
new_msg = update.message.text
entry = SmallCommandConfig.get_entry()
entry.start_message = new_msg
entry.save()
set_config_member(SmallCommandConfig, "start_message", update)
msg = "Start message updated."
send_custom_reply(bot, update, msg)
def reset_start_message(self: SmallCommandConfigExt,
bot: Bot, update: Update) -> None:
start_message = SmallCommandConfig.start_message.default
entry = SmallCommandConfig.get_entry()
entry.start_message = start_message
entry.save()
reset_config_member(SmallCommandConfig, "start_message")
msg = "Start message resetted."
send_custom_reply(bot, update, msg)
def set_license_message(self: SmallCommandConfigExt,
bot: Bot, update: Update) -> None:
new_msg = update.message.text
entry = SmallCommandConfig.get_entry()
entry.license_message = new_msg
entry.save()
set_config_member(SmallCommandConfig, "license_message", update)
msg = "License message updated."
send_custom_reply(bot, update, msg)
def reset_license_message(self: SmallCommandConfigExt,
bot: Bot, update: Update) -> None:
license_message = SmallCommandConfig.license_message.default
entry = SmallCommandConfig.get_entry()
entry.license_message = license_message
entry.save()
reset_config_member(SmallCommandConfig, "license_message")
msg = "License message resetted."
send_custom_reply(bot, update, msg)
def set_help_message(self: SmallCommandConfigExt,
bot: Bot, update: Update) -> None:
new_msg = update.message.text
entry = SmallCommandConfig.get_entry()
entry.help_message = new_msg
entry.save()
set_config_member(SmallCommandConfig, "help_message", update)
msg = "Help message updated."
send_custom_reply(bot, update, msg)
def reset_help_message(self: SmallCommandConfigExt,
bot: Bot, update: Update) -> None:
help_message = SmallCommandConfig.help_message.default
entry = SmallCommandConfig.get_entry()
entry.help_message = help_message
entry.save()
reset_config_member(SmallCommandConfig, "help_message")
msg = "Help message resetted."
send_custom_reply(bot, update, msg)
def set_dsgvo_message(self: SmallCommandConfigExt,
bot: Bot, update: Update) -> None:
new_msg = update.message.text
entry = SmallCommandConfig.get_entry()
entry.dsgvo_message = new_msg
entry.save()
set_config_member(SmallCommandConfig, "dsgvo_message", update)
msg = "Dsgvo message updated."
send_custom_reply(bot, update, msg)
def reset_dsgvo_message(self: SmallCommandConfigExt,
bot: Bot, update: Update) -> None:
dsgvo_message = SmallCommandConfig.dsgvo_message.default
entry = SmallCommandConfig.get_entry()
entry.dsgvo_message = dsgvo_message
entry.save()
reset_config_member(SmallCommandConfig, "dsgvo_message")
msg = "Dsgvo message resetted."
send_custom_reply(bot, update, msg)
......
from gmb.pollingbot.extension_groups_list import GroupsListExt
from gmb.pollingbot.extension_leave_chat import LeaveChatExt
from gmb.pollingbot.extension_leave_chat import LeaveChatExt, LeaveChatConfigExt
from gmb.pollingbot.extension_send_invite_link import SendInviteLinkExt
from gmb.pollingbot.extension_small_command import SmallCommandExt
from gmb.pollingbot.extension_small_command import SmallCommandExt, SmallCommandConfigExt
from gmb.pollingbot.pollingbot import PollingBotExtensions
from gmb.infofetchbot.extension_infofetchbot_config import InfoFetchConfigExt
def register_pollingbot_extensions():
......@@ -12,3 +13,4 @@ def register_pollingbot_extensions():
PollingBotExtensions.add_extension(LeaveChatExt)
PollingBotExtensions.add_extension(LeaveChatConfigExt)
PollingBotExtensions.add_extension(GroupsListExt)
PollingBotExtensions.add_extension(InfoFetchConfigExt)
from typing import Any, List
from typing import Any, List, Union, Type
import telegram
from telegram import Bot, Update
from gmb.database.basemodel import BaseModel
def text_without_command(update: Update):
return " ".join(update.message.text.split(" ")[1:])
def reset_config_member(cls: Type[BaseModel], member: str) -> Any:
entry = cls.get_entry()
entry.__data__[member] = cls.__dict__[member].field.default
entry.save()
return entry.__data__[member]
def set_config_member(cls: Type[BaseModel], member: str, update: Update, type: Union[Type, None] = None) -> Any:
"""Returns the new value on success or None on error."""
new_val = text_without_command(update)
if type:
try:
new_val = type(new_val)
except Exception:
return None
entry = cls.get_entry()
entry.__data__[member] = new_val
entry.save()
return new_val
def build_menu(buttons: List[Any],
......@@ -10,7 +36,7 @@ def build_menu(buttons: List[Any],
footer_buttons: List[Any] = None) -> List[List[Any]]:
"""Builds an menu.
A button can be for example a InlineKeyboardButton. It inserts the header_button at
A button can be for example a InlineKeyboardButton. It ins/set_start_message husoerts the header_button at
position 0 and appends the footer_button. It creates a list with lists and the inner
lists have a lenth <= n_cols. That makes it possible to create a telegram menu just
by passing in a list of buttons and a width.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment