Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions app/front/send_tg_notification.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import datetime

from flask import jsonify, make_response
from flask_apispec import doc, use_kwargs
from flask import jsonify, make_response, request
from flask_apispec import doc
from flask_apispec.views import MethodResource
from flask_jwt_extended import jwt_required, get_jwt_identity
from flask_restful import Resource
from marshmallow import fields, Schema
from sqlalchemy.exc import SQLAlchemyError

from app import config
from app.database import db_session
from app.logger import app_logger as logger
from app.models import Notification
from app.request_models.telegram_notification import TelegramNotificationRequest
from app.webhooks.check_request import request_to_context
from app.webhooks.check_webhooks_token import check_webhooks_token

from bot.messages import TelegramNotification



class TelegramNotificationSchema(Schema):
message = fields.String(required=True)
has_mailing = fields.String(required=True)
from core.services.mailing_type import MailingType


class SendTelegramNotification(Resource, MethodResource):
method_decorators = {'post': [check_webhooks_token]}

@doc(description='Sends message to the Telegram chat. Requires "message" parameter.'
' Messages can be sent either to subscribed users or not.To do this,'
Expand Down Expand Up @@ -52,26 +50,24 @@ class SendTelegramNotification(Resource, MethodResource):
'Authorization': config.PARAM_HEADER_AUTH, # Only if request requires authorization
}
)
@use_kwargs(TelegramNotificationSchema)
@jwt_required()
def post(self, **kwargs):
message = kwargs.get('message').replace(' ', '')
has_mailing = kwargs.get('has_mailing')
def post(self):
notifications = request_to_context(TelegramNotificationRequest, request)

if not message or not has_mailing:
if ['message', 'has_mailing'] not in notifications:
Comment thread
JediMode marked this conversation as resolved.
Outdated
logger.info("Messages: The <message> and <has_mailing> parameters have not been passed")
return make_response(jsonify(result="Необходимо указать параметры <message> и <has_mailing>."), 400)

authorized_user = get_jwt_identity()
message = Notification(message=message, sent_by=authorized_user)
message = Notification(message=notifications['message'], sent_by=authorized_user)
Comment thread
JediMode marked this conversation as resolved.
db_session.add(message)
try:
db_session.commit()
job_queue = TelegramNotification(has_mailing)
job_queue = TelegramNotification()
Comment thread
JediMode marked this conversation as resolved.
Outdated

if not job_queue.send_notification(message=message.message):
logger.info(f"Messages: Passed invalid <has_mailing> parameter. Passed: {has_mailing}")
return make_response(jsonify(result=f"Неверно указан параметр <has_mailing>. "
if not job_queue.send_notification(message=message, mailing_type=MailingType.value):
Comment thread
JediMode marked this conversation as resolved.
Outdated
logger.info(f"Messages: Passed invalid <mailing_type> parameter. Passed: {MailingType.value}")
return make_response(jsonify(result=f"Неверно указан параметр <mailing_type>. "
f"Сообщение не отправлено."), 400)
Comment thread
JediMode marked this conversation as resolved.
Outdated

message.was_sent = True
Expand Down
8 changes: 8 additions & 0 deletions app/request_models/telegram_notification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pydantic import Field, StrictStr

from app.request_models.request_base import RequestBase


class TelegramNotificationRequest(RequestBase):
message: StrictStr = Field()
has_mailing: StrictStr = Field()
Comment thread
JediMode marked this conversation as resolved.
Outdated
20 changes: 8 additions & 12 deletions bot/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from app.logger import bot_logger as logger
from app.models import User
from bot.charity_bot import dispatcher
from core.services.mailing_type import MailingType

bot = Bot(config.TELEGRAM_TOKEN)

Expand All @@ -30,32 +31,27 @@ class TelegramNotification:
"""
This class describes the functionality for working with notifications in Telegram.
"""

def __init__(self, has_mailing: str = 'subscribed') -> None:
self.has_mailing = has_mailing

# TODO refactoring https://github.com/python-telegram-bot/python-telegram-bot/wiki/Avoiding-flood-limits
def send_notification(self, message):
def send_notification(self, mailing_type, message):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Другой порядок, сперва сообщение, потом тип. сообщение важнее чем тип.!

"""
Adds queue to send notification to telegram chats.

:param mailing_type: Type of subscription
:param message: Message to add to the sending queue
:param telegram_chats: Users query
:return:
"""
if self.has_mailing not in ('all', 'subscribed', 'unsubscribed'):
values = [member.value for member in MailingType]
if mailing_type not in values:
return False

chats_list = []
query = db_session.query(User.telegram_id).filter(User.banned.is_(False))

if self.has_mailing == 'subscribed':
if mailing_type == MailingType.SUBSCRIBED.value:
chats_list = query.filter(User.has_mailing.is_(True))

if self.has_mailing == 'unsubscribed':
if mailing_type == MailingType.UNSUBSCRIBED.value:
chats_list = query.filter(User.has_mailing.is_(False))

if self.has_mailing == 'all':
if mailing_type == MailingType.ALL.value:
chats_list = query

user_notification_context = SendUserNotificationsContext([])
Expand Down
10 changes: 10 additions & 0 deletions core/services/mailing_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from enum import Enum


class MailingType(Enum):
"""
This class implements enumeration.
"""
SUBSCRIBED = 'subscribed'
UNSUBSCRIBED = 'unsubscribed'
ALL = 'all'
11 changes: 5 additions & 6 deletions core/services/user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,15 @@ def change_subscription(self, telegram_id):
"""
user = User.query.options(load_only('has_mailing')).filter_by(telegram_id=telegram_id).first()

if user.has_mailing:
user.has_mailing = False
else:
user.has_mailing = True
try:
user.has_mailing = not user.has_mailing
db_session.commit()
Comment thread
JediMode marked this conversation as resolved.
logger.info("Subscription: Status successfully changed.")
return user.has_mailing
except SQLAlchemyError as ex:
logger.error(f"User DB - 'change_subscription' method: {str(ex)}")

return user.has_mailing
db_session.rollback()
return user.has_mailing
Comment thread
AntonZelinsky marked this conversation as resolved.

def change_user_category(self, telegram_id, category_id):
user = User.query.filter_by(telegram_id=telegram_id).first()
Expand Down