Skip to content

Commit

Permalink
Merge pull request #61 from KaratekHD/dev
Browse files Browse the repository at this point in the history
Release 2.1
  • Loading branch information
KaratekHD authored Jan 15, 2021
2 parents f42664d + 357aef1 commit e2840a9
Show file tree
Hide file tree
Showing 19 changed files with 643 additions and 55 deletions.
7 changes: 4 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ python-telegram-bot==13.1
psycopg2-binary
feedparser
toml
googletrans
googletrans==4.0.0-rc1
pillow
alphabet-detector
flask
flask-restplus
Werkzeug==0.16.1
flask-httpauth
Werkzeug==1.0.1
flask-httpauth
schedule
11 changes: 7 additions & 4 deletions tg_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
import telegram.ext as tg
from tg_bot.strings.string_helper import get_string

VERSION = 2.1

# Module name
module = "init"

# enable logging


LOGGER = logging.getLogger(__name__)

ENV = bool(os.environ.get('ENV', False))
Expand All @@ -49,7 +49,11 @@
format=LOGFORMAT,
level=logging.INFO)


LOGGER.info(f"Nemesis v{VERSION}\n"
f"This program is free software: you can redistribute it and/or modify\n"
f"it under the terms of the GNU General Public License as published by\n"
f"the Free Software Foundation, either version 3 of the License, or\n"
f"(at your option) any later version.")
# if version < 3.6, stop bot.
if sys.version_info[0] < 3 or sys.version_info[1] < 6:
LOGGER.error(get_string(module, "ERR_INVALID_PYTHON_VERSION", DEFAULT_LANG)) # ERR_INVALID_PYTHON_VERSION
Expand Down Expand Up @@ -152,7 +156,6 @@
SUDO_USERS.add(OWNER_ID)
SUDO_USERS.add(CO_OWNER_ID)
LOGGER.info("Owner: %s", OWNER_ID )
LOGGER.info("Co-Owner: %s", CO_OWNER_ID )

updater = tg.Updater(TOKEN, workers=WORKERS, use_context=True)

Expand Down
7 changes: 4 additions & 3 deletions tg_bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from telegram.utils.helpers import escape_markdown

from tg_bot import dispatcher, updater, TOKEN, WEBHOOK, OWNER_ID, DONATION_LINK, CERT_PATH, PORT, URL, LOGGER, \
ALLOW_EXCL, DEFAULT_LANG
ALLOW_EXCL, DEFAULT_LANG, VERSION

from tg_bot.strings.string_helper import get_string

Expand Down Expand Up @@ -368,6 +368,7 @@ def get_settings(update: Update, context: CallbackContext):
else:
send_settings(chat.id, user.id, True)


def migrate_chats(update: Update, context: CallbackContext):
msg = update.effective_message # type: Optional[Message]
if msg.migrate_to_chat_id:
Expand Down Expand Up @@ -412,7 +413,7 @@ def about(update: Update, context: CallbackContext):
" - [Severus Snape](https://t.me/GenosseSeverus) - Co-Owner\n" \
" - [Luna Loony](https://t.me/Luna_loony) - Admin\n"

update.effective_message.reply_text("*Nemesis - Powerful open-source group manager*\n"
update.effective_message.reply_text("*Nemesis v{} - Powerful open-source group manager*\n"
"Copyright (C) 2017 - 2019 Paul Larsen\n"
"Copyright (C) 2019 - 2020 KaratekHD\n\n"
"This program is free software: you can redistribute it and/or modify "
Expand All @@ -428,7 +429,7 @@ def about(update: Update, context: CallbackContext):
"*Translation*\n"
"{}"
"*Production*\n"
"{}".format(DEVELOPMENT, TRANSLATION, PRODUCTION), parse_mode=ParseMode.MARKDOWN)
"{}".format(VERSION, DEVELOPMENT, TRANSLATION, PRODUCTION), parse_mode=ParseMode.MARKDOWN)


def load_api():
Expand Down
1 change: 0 additions & 1 deletion tg_bot/modules/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ def toggle_mute(update: Update, context: CallbackContext) -> str:

@user_not_admin
def on_message(update: Update, context: CallbackContext):
LOGGER.debug("Yeet!")
if mute_sql.get_muted(update.effective_chat.id):
update.effective_message.delete()

Expand Down
12 changes: 10 additions & 2 deletions tg_bot/modules/reputations.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,18 @@ def __user_settings__(user_id):

INCREASE_MESSAGE_HANDLER = DisableAbleMessageHandler(Filters.regex(r"^\+$"), increase, friendly="increase",
run_async=True)

dispatcher.add_handler(INCREASE_MESSAGE_HANDLER)
INCREASE_MESSAGE_HANDLER = DisableAbleMessageHandler(Filters.regex(r"^\-$"), decrease, friendly="decrease",
DECREASE_MESSAGE_HANDLER = DisableAbleMessageHandler(Filters.regex(r"^\-$"), decrease, friendly="decrease",
run_async=True)
dispatcher.add_handler(INCREASE_MESSAGE_HANDLER)
dispatcher.add_handler(DECREASE_MESSAGE_HANDLER)
INCREASE_MESSAGE_HANDLER2 = DisableAbleMessageHandler(Filters.regex(r"^\👍$"), increase, friendly="increase",
run_async=True)

dispatcher.add_handler(INCREASE_MESSAGE_HANDLER2)
DECREASE_MESSAGE_HANDLER2 = DisableAbleMessageHandler(Filters.regex(r"^\👎$"), decrease, friendly="decrease",
run_async=True)
dispatcher.add_handler(DECREASE_MESSAGE_HANDLER2)
SETTINGS_HANDLER = CommandHandler("reputation", reputation, run_async=True)
dispatcher.add_handler(SETTINGS_HANDLER)

Expand Down
11 changes: 5 additions & 6 deletions tg_bot/modules/sql/api_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import threading

from sqlalchemy import Column, String, UnicodeText, func, distinct, Integer
from werkzeug.security import generate_password_hash, check_password_hash
from tg_bot.modules.sql import SESSION, BASE


Expand All @@ -37,6 +35,7 @@ def __repr__(self):

Api.__table__.create(checkfirst=True)


def set_key(user_id, key):
user = SESSION.query(Api).get(user_id)
if not user:
Expand All @@ -49,10 +48,10 @@ def set_key(user_id, key):


def verify_key(key):
hash = SESSION.query(Api).get(Api.key == str(key))
ret = "null"
if hash:
ret = hash.user_id
result = SESSION.query(Api).filter(Api.key == str(key)).scalar()
ret = None
if result:
ret = result.user_id
SESSION.close()
return ret

Expand Down
21 changes: 21 additions & 0 deletions tg_bot/modules/sql/users_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ def ensure_bot_in_db():
SESSION.commit()


def get_chatname(chat_id):
try:
return SESSION.query(Chats).get(str(chat_id)).chat_name
finally:
SESSION.close()


def update_user(user_id, username, chat_id=None, chat_name=None):
with INSERTION_LOCK:
user = SESSION.query(Users).get(user_id)
Expand Down Expand Up @@ -141,13 +148,27 @@ def get_chat_members(chat_id):
SESSION.close()


def get_chats_by_member(user_id):
try:
return SESSION.query(ChatMembers).filter(ChatMembers.user == str(user_id)).all()
finally:
SESSION.close()


def get_all_chats():
try:
return SESSION.query(Chats).all()
finally:
SESSION.close()


def get_all_users():
try:
return SESSION.query(Users).all()
finally:
SESSION.close()


def get_user_num_chats(user_id):
try:
return SESSION.query(ChatMembers).filter(ChatMembers.user == int(user_id)).count()
Expand Down
7 changes: 6 additions & 1 deletion tg_bot/modules/stickers.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ def kang(update, context):
print(e)

else:
update.effective_message.reply_text("Kanging animated stickers is not supported, see"
" [#57](https://github.com/KaratekHD/Nemesis/issues/57)", parse_mode=ParseMode.MARKDOWN)
return
packname = "animated" + str(user.id) + "_by_" + context.bot.username
packname_found = 0
max_stickers = 50
Expand Down Expand Up @@ -304,7 +307,7 @@ def kang(update, context):
elif e.message == "Invalid sticker emojis":
msg.reply_text("Invalid emoji(s).")
elif e.message == "Stickers_too_much":
msg.reply_text("Max packsize reached. Press F to pay respecc.")
msg.reply_text("Max packsize reached. Press F to pay respect.")
elif e.message == "Internal Server Error: sticker set not found (500)":
msg.reply_text(
"Sticker successfully added to [pack](t.me/addstickers/%s)"
Expand Down Expand Up @@ -466,6 +469,8 @@ def __help__(update: Update) -> str:
"""

__mod_name__ = "Stickers"


KANG_HANDLER = DisableAbleCommandHandler("kang", kang, pass_args=True, admin_ok=True, run_async=True)
STICKERID_HANDLER = DisableAbleCommandHandler("stickerid", stickerid, run_async=True)
GETSTICKER_HANDLER = DisableAbleCommandHandler("getsticker", getsticker, run_async=True)
Expand Down
4 changes: 4 additions & 0 deletions tg_bot/restapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
from flask_restplus import Api
from tg_bot.restapi.resources.basic import basic_api
from tg_bot.restapi.resources.chats import chats_api
import tg_bot.restapi.resources.management as management
import tg_bot.restapi.resources.modules.admin as admin

app = Flask("Nemesis Telegram Bot")
api = Api(app, version="2.0 Development Preview 1", title="Nemesis Telegram Bot")

api.add_namespace(basic_api)
api.add_namespace(chats_api)
api.add_namespace(management.api)
api.add_namespace(admin.api)
27 changes: 2 additions & 25 deletions tg_bot/restapi/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,9 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from functools import wraps

from flask import request, abort

import tg_bot.modules.sql.api_sql as sql


def token_required(view_function):
@wraps(view_function)
# the new, post-decoration function. Note *args and **kwargs here.
def decorated_function(*args, **kwargs):
if request.args.get('key') and request.args.get('key') == "test":
return view_function(*args, **kwargs)
else:
abort(401)
return decorated_function

def authenticate(username, password):
if username and password:
key = sql.get_key(username)
if key != "null":
if password is key:
return True
else:
return False
else:
return False
else:
return False
def verify_auth_token(token):
sql.verify_key(token)
25 changes: 25 additions & 0 deletions tg_bot/restapi/models/chats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Nemesis - Powerful Telegram group managment bot
# Copyright (C) 2017 - 2019 Paul Larsen
# Copyright (C) 2019 - 2020 KaratekHD
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from flask_restplus import fields


def create_chat_model(api):
model = api.model('Chats', {
'id': fields.String(description="unique group identifier", required=True),
'name': fields.String(description="group name", required=True)
})
return model
64 changes: 64 additions & 0 deletions tg_bot/restapi/models/management.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Nemesis - Powerful Telegram group managment bot
# Copyright (C) 2017 - 2019 Paul Larsen
# Copyright (C) 2019 - 2020 KaratekHD
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from flask_restplus import fields

from tg_bot.restapi.models.users import create_user_model


def create_chatnumber_model(api):
model = api.model('Total Chats', {
'number': fields.Integer(description="Number of group the bot has registered.", required=True)
})
return model


def create_countusers_model(api):
model = api.model('Total Users', {
'number': fields.Integer(description="Number of users the bot has registered.", required=True)
})
return model


def create_chat_model(api):
model = api.model('Chat', {
'id': fields.Integer(description="Telegram ID of chat", required=True),
'name': fields.String(description="Name of the group", required=True)
})
return model


def create_chatlist_model(api):
model = api.model('Chats', {
'chats': fields.Nested(create_chat_model(api), description="list of chats", required=True)
})
return model


def create_broadcast_model(api):
model = api.model("Broadcast", {
'message': fields.String(description="The message that was broadcasted.", required=True),
"failed": fields.Integer(description="Number of chats that failed receiving the broadcast.", required=True),
"failed_chats": fields.Nested(create_chat_model(api), description="Chats that failed receiving the broadcast.", required=False)
})
return model


def create_userlist_model(api):
model = api.model('Users', {
'users': fields.Nested(create_user_model(api), description="list of users", required=True)
})
return model
17 changes: 17 additions & 0 deletions tg_bot/restapi/models/modules/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Nemesis - Powerful Telegram group managment bot
# Copyright (C) 2017 - 2019 Paul Larsen
# Copyright (C) 2019 - 2020 KaratekHD
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

Loading

0 comments on commit e2840a9

Please sign in to comment.