2022-11-17 07:34:58 +00:00
|
|
|
import json
|
|
|
|
|
|
|
|
import pika
|
2024-06-17 13:09:34 +00:00
|
|
|
import pdpyras
|
2024-04-17 08:06:29 +00:00
|
|
|
import requests
|
2024-06-17 13:09:34 +00:00
|
|
|
import telebot
|
|
|
|
from telebot import apihelper
|
2024-08-19 08:49:19 +00:00
|
|
|
from flask import render_template, abort, g
|
2022-11-17 07:34:58 +00:00
|
|
|
|
2024-03-03 07:11:48 +00:00
|
|
|
import app.modules.db.sql as sql
|
|
|
|
import app.modules.db.user as user_sql
|
|
|
|
import app.modules.db.group as group_sql
|
|
|
|
import app.modules.db.server as server_sql
|
|
|
|
import app.modules.db.channel as channel_sql
|
|
|
|
import app.modules.db.checker as checker_sql
|
|
|
|
import app.modules.common.common as common
|
|
|
|
import app.modules.roxywi.common as roxywi_common
|
2022-11-17 07:34:58 +00:00
|
|
|
|
2022-12-07 07:16:07 +00:00
|
|
|
error_mess = common.error_mess
|
|
|
|
|
2022-11-17 07:34:58 +00:00
|
|
|
|
|
|
|
def send_message_to_rabbit(message: str, **kwargs) -> None:
|
|
|
|
rabbit_user = sql.get_setting('rabbitmq_user')
|
|
|
|
rabbit_password = sql.get_setting('rabbitmq_password')
|
|
|
|
rabbit_host = sql.get_setting('rabbitmq_host')
|
|
|
|
rabbit_port = sql.get_setting('rabbitmq_port')
|
|
|
|
rabbit_vhost = sql.get_setting('rabbitmq_vhost')
|
|
|
|
if kwargs.get('rabbit_queue'):
|
|
|
|
rabbit_queue = kwargs.get('rabbit_queue')
|
|
|
|
else:
|
|
|
|
rabbit_queue = sql.get_setting('rabbitmq_queue')
|
|
|
|
|
|
|
|
credentials = pika.PlainCredentials(rabbit_user, rabbit_password)
|
2024-06-19 17:52:24 +00:00
|
|
|
try:
|
|
|
|
parameters = pika.ConnectionParameters(
|
|
|
|
rabbit_host,
|
|
|
|
rabbit_port,
|
|
|
|
rabbit_vhost,
|
|
|
|
credentials
|
|
|
|
)
|
|
|
|
connection = pika.BlockingConnection(parameters)
|
|
|
|
channel = connection.channel()
|
|
|
|
except Exception as e:
|
|
|
|
raise Exception(f'RabbitMQ connection error {e}')
|
|
|
|
|
2022-11-17 07:34:58 +00:00
|
|
|
channel.queue_declare(queue=rabbit_queue)
|
|
|
|
channel.basic_publish(exchange='', routing_key=rabbit_queue, body=message)
|
|
|
|
|
|
|
|
connection.close()
|
|
|
|
|
|
|
|
|
|
|
|
def alert_routing(
|
|
|
|
server_ip: str, service_id: int, group_id: int, level: str, mes: str, alert_type: str
|
|
|
|
) -> None:
|
2024-08-30 14:25:31 +00:00
|
|
|
try:
|
|
|
|
subject: str = level + ': ' + mes
|
|
|
|
server_id: int = server_sql.select_server_id_by_ip(server_ip)
|
|
|
|
checker_settings = checker_sql.select_checker_settings_for_server(service_id, server_id)
|
|
|
|
except Exception as e:
|
|
|
|
raise Exception(f'Cannot get settings: {e}')
|
2022-11-17 07:34:58 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
json_for_sending = {"user_group": group_id, "message": subject}
|
|
|
|
send_message_to_rabbit(json.dumps(json_for_sending))
|
|
|
|
except Exception as e:
|
2023-03-25 18:12:46 +00:00
|
|
|
roxywi_common.logging('Roxy-WI server', f'error: unable to send message: {e}', roxywi=1)
|
2022-11-17 07:34:58 +00:00
|
|
|
|
|
|
|
for setting in checker_settings:
|
|
|
|
if alert_type == 'service' and setting.service_alert:
|
2023-04-15 21:26:54 +00:00
|
|
|
try:
|
|
|
|
telegram_send_mess(mes, level, channel_id=setting.telegram_id)
|
2023-04-17 15:01:01 +00:00
|
|
|
except Exception as e:
|
|
|
|
roxywi_common.logging('Roxy-WI server', f'error: unable to send message to Telegram: {e}', roxywi=1)
|
|
|
|
try:
|
2023-04-15 21:26:54 +00:00
|
|
|
slack_send_mess(mes, level, channel_id=setting.slack_id)
|
2023-04-17 15:01:01 +00:00
|
|
|
except Exception as e:
|
|
|
|
roxywi_common.logging('Roxy-WI server', f'error: unable to send message to Slack: {e}', roxywi=1)
|
|
|
|
try:
|
2023-04-15 21:26:54 +00:00
|
|
|
pd_send_mess(mes, level, server_ip, service_id, alert_type, channel_id=setting.pd_id)
|
|
|
|
except Exception as e:
|
2023-04-17 15:01:01 +00:00
|
|
|
roxywi_common.logging('Roxy-WI server', f'error: unable to send message to PagerDuty: {e}', roxywi=1)
|
2024-04-17 08:06:29 +00:00
|
|
|
try:
|
|
|
|
mm_send_mess(mes, level, server_ip, service_id, alert_type, channel_id=setting.mm_id)
|
|
|
|
except Exception as e:
|
|
|
|
roxywi_common.logging('Roxy-WI server', f'error: unable to send message to Mattermost: {e}', roxywi=1)
|
2023-04-17 15:01:01 +00:00
|
|
|
|
2022-11-17 07:34:58 +00:00
|
|
|
if setting.email:
|
2023-03-25 18:17:51 +00:00
|
|
|
send_email_to_server_group(subject, mes, level, group_id)
|
2022-11-17 07:34:58 +00:00
|
|
|
|
|
|
|
if alert_type == 'backend' and setting.backend_alert:
|
2023-04-15 21:26:54 +00:00
|
|
|
try:
|
|
|
|
telegram_send_mess(mes, level, channel_id=setting.telegram_id)
|
2023-04-17 15:01:01 +00:00
|
|
|
except Exception as e:
|
|
|
|
roxywi_common.logging('Roxy-WI server', f'error: unable to send message to Telegram: {e}', roxywi=1)
|
|
|
|
try:
|
2023-04-15 21:26:54 +00:00
|
|
|
slack_send_mess(mes, level, channel_id=setting.slack_id)
|
2023-04-17 15:01:01 +00:00
|
|
|
except Exception as e:
|
|
|
|
roxywi_common.logging('Roxy-WI server', f'error: unable to send message to Slack: {e}', roxywi=1)
|
|
|
|
try:
|
2023-04-15 21:26:54 +00:00
|
|
|
pd_send_mess(mes, level, server_ip, service_id, alert_type, channel_id=setting.pd_id)
|
|
|
|
except Exception as e:
|
2023-04-17 15:01:01 +00:00
|
|
|
roxywi_common.logging('Roxy-WI server', f'error: unable to send message to PagerDuty: {e}', roxywi=1)
|
2024-04-17 08:06:29 +00:00
|
|
|
try:
|
|
|
|
mm_send_mess(mes, level, server_ip, service_id, alert_type, channel_id=setting.mm_id)
|
|
|
|
except Exception as e:
|
|
|
|
roxywi_common.logging('Roxy-WI server', f'error: unable to send message to Mattermost: {e}', roxywi=1)
|
2022-11-17 07:34:58 +00:00
|
|
|
|
|
|
|
if setting.email:
|
2023-03-25 18:17:51 +00:00
|
|
|
send_email_to_server_group(subject, mes, level, group_id)
|
2022-11-17 07:34:58 +00:00
|
|
|
|
|
|
|
if alert_type == 'maxconn' and setting.maxconn_alert:
|
2023-04-15 21:26:54 +00:00
|
|
|
try:
|
|
|
|
telegram_send_mess(mes, level, channel_id=setting.telegram_id)
|
2023-04-17 15:01:01 +00:00
|
|
|
except Exception as e:
|
|
|
|
roxywi_common.logging('Roxy-WI server', f'error: unable to send message to Telegram: {e}', roxywi=1)
|
|
|
|
try:
|
2023-04-15 21:26:54 +00:00
|
|
|
slack_send_mess(mes, level, channel_id=setting.slack_id)
|
2023-04-17 15:01:01 +00:00
|
|
|
except Exception as e:
|
|
|
|
roxywi_common.logging('Roxy-WI server', f'error: unable to send message to Slack: {e}', roxywi=1)
|
|
|
|
try:
|
2023-04-15 21:26:54 +00:00
|
|
|
pd_send_mess(mes, level, server_ip, service_id, alert_type, channel_id=setting.pd_id)
|
|
|
|
except Exception as e:
|
2023-04-17 15:01:01 +00:00
|
|
|
roxywi_common.logging('Roxy-WI server', f'error: unable to send message to PagerDuty: {e}', roxywi=1)
|
2024-04-17 08:06:29 +00:00
|
|
|
try:
|
|
|
|
mm_send_mess(mes, level, server_ip, service_id, alert_type, channel_id=setting.mm_id)
|
|
|
|
except Exception as e:
|
|
|
|
roxywi_common.logging('Roxy-WI server', f'error: unable to send message to Mattermost: {e}', roxywi=1)
|
2022-11-17 07:34:58 +00:00
|
|
|
|
|
|
|
if setting.email:
|
2023-03-25 18:17:51 +00:00
|
|
|
send_email_to_server_group(subject, mes, level, group_id)
|
2022-11-17 07:34:58 +00:00
|
|
|
|
|
|
|
|
2023-03-25 18:12:46 +00:00
|
|
|
def send_email_to_server_group(subject: str, mes: str, level: str, group_id: int) -> None:
|
2022-11-17 07:34:58 +00:00
|
|
|
try:
|
2024-03-03 07:11:48 +00:00
|
|
|
users_email = user_sql.select_users_emails_by_group_id(group_id)
|
2022-11-17 07:34:58 +00:00
|
|
|
|
|
|
|
for user_email in users_email:
|
2023-03-25 18:12:46 +00:00
|
|
|
send_email(user_email.email, subject, f'{level}: {mes}')
|
2022-11-17 07:34:58 +00:00
|
|
|
except Exception as e:
|
|
|
|
roxywi_common.logging('Roxy-WI server', f'error: unable to send email: {e}', roxywi=1)
|
|
|
|
|
|
|
|
|
|
|
|
def send_email(email_to: str, subject: str, message: str) -> None:
|
|
|
|
from smtplib import SMTP
|
|
|
|
|
|
|
|
try:
|
|
|
|
from email.MIMEText import MIMEText
|
|
|
|
except Exception:
|
|
|
|
from email.mime.text import MIMEText
|
|
|
|
|
|
|
|
mail_ssl = sql.get_setting('mail_ssl')
|
|
|
|
mail_from = sql.get_setting('mail_from')
|
|
|
|
mail_smtp_host = sql.get_setting('mail_smtp_host')
|
|
|
|
mail_smtp_port = sql.get_setting('mail_smtp_port')
|
|
|
|
mail_smtp_user = sql.get_setting('mail_smtp_user')
|
2022-12-25 09:12:13 +00:00
|
|
|
mail_smtp_password = sql.get_setting('mail_smtp_password').replace("'", "")
|
2022-11-17 07:34:58 +00:00
|
|
|
|
|
|
|
msg = MIMEText(message)
|
2022-12-06 21:04:53 +00:00
|
|
|
msg['Subject'] = f'Roxy-WI: {subject}'
|
|
|
|
msg['From'] = f'Roxy-WI <{mail_from}>'
|
2022-11-17 07:34:58 +00:00
|
|
|
msg['To'] = email_to
|
|
|
|
|
|
|
|
try:
|
|
|
|
smtp_obj = SMTP(mail_smtp_host, mail_smtp_port)
|
|
|
|
if mail_ssl:
|
|
|
|
smtp_obj.starttls()
|
|
|
|
smtp_obj.login(mail_smtp_user, mail_smtp_password)
|
|
|
|
smtp_obj.send_message(msg)
|
|
|
|
roxywi_common.logging('Roxy-WI server', f'An email has been sent to {email_to}', roxywi=1)
|
|
|
|
except Exception as e:
|
|
|
|
roxywi_common.logging('Roxy-WI server', f'error: unable to send email: {e}', roxywi=1)
|
|
|
|
|
|
|
|
|
2023-03-25 18:12:46 +00:00
|
|
|
def telegram_send_mess(mess, level, **kwargs):
|
2022-11-17 07:34:58 +00:00
|
|
|
token_bot = ''
|
|
|
|
channel_name = ''
|
2024-08-12 20:41:31 +00:00
|
|
|
proxy = sql.get_setting('proxy')
|
2022-11-17 07:34:58 +00:00
|
|
|
|
2023-04-15 21:26:54 +00:00
|
|
|
if kwargs.get('channel_id') == 0:
|
2022-11-17 07:34:58 +00:00
|
|
|
return
|
|
|
|
|
2023-04-15 21:26:54 +00:00
|
|
|
if kwargs.get('channel_id'):
|
2024-08-02 09:50:02 +00:00
|
|
|
telegrams = channel_sql.get_receiver_by_id('telegram', kwargs.get('channel_id'))
|
2022-11-17 07:34:58 +00:00
|
|
|
else:
|
2024-08-02 09:50:02 +00:00
|
|
|
telegrams = channel_sql.get_receiver_by_ip('telegram', kwargs.get('ip'))
|
2022-11-17 07:34:58 +00:00
|
|
|
|
|
|
|
for telegram in telegrams:
|
|
|
|
token_bot = telegram.token
|
|
|
|
channel_name = telegram.chanel_name
|
|
|
|
|
|
|
|
if token_bot == '' or channel_name == '':
|
2024-08-21 06:25:56 +00:00
|
|
|
mess = "Can't send message. Add Telegram channel before use alerting at this servers group"
|
2022-11-17 07:34:58 +00:00
|
|
|
roxywi_common.logging('Roxy-WI server', mess, roxywi=1)
|
|
|
|
|
|
|
|
if proxy is not None and proxy != '' and proxy != 'None':
|
|
|
|
apihelper.proxy = {'https': proxy}
|
|
|
|
try:
|
|
|
|
bot = telebot.TeleBot(token=token_bot)
|
2023-03-25 18:12:46 +00:00
|
|
|
bot.send_message(chat_id=channel_name, text=f'{level}: {mess}')
|
2022-11-17 07:34:58 +00:00
|
|
|
except Exception as e:
|
|
|
|
roxywi_common.logging('Roxy-WI server', str(e), roxywi=1)
|
2024-06-17 13:09:34 +00:00
|
|
|
raise Exception(e)
|
2022-11-17 07:34:58 +00:00
|
|
|
|
|
|
|
|
2023-03-25 18:17:51 +00:00
|
|
|
def slack_send_mess(mess, level, **kwargs):
|
2022-11-17 07:34:58 +00:00
|
|
|
from slack_sdk import WebClient
|
|
|
|
from slack_sdk.errors import SlackApiError
|
|
|
|
slack_token = ''
|
|
|
|
channel_name = ''
|
|
|
|
|
2023-04-15 21:26:54 +00:00
|
|
|
if kwargs.get('channel_id') == 0:
|
2022-11-17 07:34:58 +00:00
|
|
|
return
|
|
|
|
|
2023-04-15 21:26:54 +00:00
|
|
|
if kwargs.get('channel_id'):
|
2024-08-02 09:50:02 +00:00
|
|
|
slacks = channel_sql.get_receiver_by_id('slack', kwargs.get('channel_id'))
|
2022-11-17 07:34:58 +00:00
|
|
|
else:
|
2024-08-02 09:50:02 +00:00
|
|
|
slacks = channel_sql.get_receiver_by_ip('slack', kwargs.get('ip'))
|
2022-11-17 07:34:58 +00:00
|
|
|
|
|
|
|
proxy = sql.get_setting('proxy')
|
|
|
|
|
|
|
|
for slack in slacks:
|
|
|
|
slack_token = slack.token
|
|
|
|
channel_name = slack.chanel_name
|
|
|
|
|
|
|
|
if proxy is not None and proxy != '' and proxy != 'None':
|
2024-08-19 08:49:19 +00:00
|
|
|
client = WebClient(token=slack_token, proxy=proxy)
|
2022-11-17 07:34:58 +00:00
|
|
|
else:
|
|
|
|
client = WebClient(token=slack_token)
|
|
|
|
|
|
|
|
try:
|
2023-03-25 18:12:46 +00:00
|
|
|
client.chat_postMessage(channel=f'#{channel_name}', text=f'{level}: {mess}')
|
2022-11-17 07:34:58 +00:00
|
|
|
except SlackApiError as e:
|
|
|
|
roxywi_common.logging('Roxy-WI server', str(e), roxywi=1)
|
2024-06-17 13:09:34 +00:00
|
|
|
raise Exception(e)
|
2023-04-15 21:26:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def pd_send_mess(mess, level, server_ip=None, service_id=None, alert_type=None, **kwargs):
|
|
|
|
token = ''
|
|
|
|
|
|
|
|
if kwargs.get('channel_id') == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
if kwargs.get('channel_id'):
|
|
|
|
try:
|
2024-08-02 09:50:02 +00:00
|
|
|
pds = channel_sql.get_receiver_by_id('pd', kwargs.get('channel_id'))
|
2023-04-15 21:26:54 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
else:
|
|
|
|
try:
|
2024-08-02 09:50:02 +00:00
|
|
|
pds = channel_sql.get_receiver_by_ip('pd', kwargs.get('ip'))
|
2023-04-15 21:26:54 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
|
|
|
|
for pd in pds:
|
|
|
|
token = pd.token
|
|
|
|
|
|
|
|
try:
|
|
|
|
proxy = sql.get_setting('proxy')
|
|
|
|
session = pdpyras.EventsAPISession(token)
|
2024-08-30 14:25:31 +00:00
|
|
|
if server_ip:
|
|
|
|
dedup_key = f'{server_ip} {service_id} {alert_type}'
|
|
|
|
else:
|
|
|
|
dedup_key = f'{level}: {mess}'
|
2023-04-15 21:26:54 +00:00
|
|
|
except Exception as e:
|
|
|
|
roxywi_common.logging('Roxy-WI server', str(e), roxywi=1)
|
2024-06-17 13:09:34 +00:00
|
|
|
raise Exception(e)
|
2023-04-15 21:26:54 +00:00
|
|
|
if proxy is not None and proxy != '' and proxy != 'None':
|
|
|
|
proxies = dict(https=proxy, http=proxy)
|
|
|
|
session.proxies.update(proxies)
|
|
|
|
|
|
|
|
try:
|
|
|
|
if level == 'info':
|
|
|
|
session.resolve(dedup_key)
|
|
|
|
else:
|
|
|
|
session.trigger(mess, 'Roxy-WI', dedup_key=dedup_key, severity=level, custom_details={'server': server_ip, 'alert': mess})
|
|
|
|
except Exception as e:
|
|
|
|
roxywi_common.logging('Roxy-WI server', str(e), roxywi=1)
|
2024-06-17 13:09:34 +00:00
|
|
|
raise Exception(e)
|
2022-12-06 21:04:53 +00:00
|
|
|
|
|
|
|
|
2024-04-17 08:06:29 +00:00
|
|
|
def mm_send_mess(mess, level, server_ip=None, service_id=None, alert_type=None, **kwargs):
|
|
|
|
token = ''
|
|
|
|
|
|
|
|
if kwargs.get('channel_id') == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
if kwargs.get('channel_id'):
|
|
|
|
try:
|
2024-08-02 09:50:02 +00:00
|
|
|
mms = channel_sql.get_receiver_by_id('mm', kwargs.get('channel_id'))
|
2024-04-17 08:06:29 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
else:
|
|
|
|
try:
|
2024-08-02 09:50:02 +00:00
|
|
|
mms = channel_sql.get_receiver_by_ip('mm', kwargs.get('ip'))
|
2024-04-17 08:06:29 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
|
|
|
|
for pd in mms:
|
|
|
|
token = pd.token
|
|
|
|
channel = pd.chanel_name
|
|
|
|
|
|
|
|
headers = {'Content-Type': 'application/json'}
|
|
|
|
if level == "info":
|
|
|
|
color = "51A347"
|
|
|
|
else:
|
|
|
|
color = "c20707"
|
|
|
|
attach = {
|
|
|
|
"fallback": f"{alert_type}",
|
|
|
|
"color": f"#{color}",
|
|
|
|
"text": f"{mess}",
|
|
|
|
"author_name": "Roxy-WI",
|
|
|
|
"title": f"{level} alert",
|
|
|
|
"fields": [
|
|
|
|
{
|
|
|
|
"short": "true",
|
|
|
|
"title": "Level",
|
|
|
|
"value": f"{level}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"short": "true",
|
|
|
|
"title": "Server",
|
|
|
|
"value": f"{server_ip}",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
attach = str(json.dumps(attach))
|
|
|
|
values = f'{{"channel": "{channel}", "username": "Roxy-WI", "attachments": [{attach}]}}'
|
2024-04-19 09:32:13 +00:00
|
|
|
proxy_dict = common.return_proxy_dict()
|
2024-04-17 08:06:29 +00:00
|
|
|
try:
|
2024-04-19 09:32:13 +00:00
|
|
|
requests.post(token, headers=headers, data=str(values), proxies=proxy_dict)
|
2024-04-17 08:06:29 +00:00
|
|
|
except Exception as e:
|
|
|
|
roxywi_common.logging('Roxy-WI server', str(e), roxywi=1)
|
2024-06-17 13:09:34 +00:00
|
|
|
raise Exception(e)
|
2024-04-17 08:06:29 +00:00
|
|
|
|
|
|
|
|
2024-06-17 13:09:34 +00:00
|
|
|
def check_rabbit_alert() -> None:
|
2022-12-06 21:04:53 +00:00
|
|
|
try:
|
2024-08-19 08:49:19 +00:00
|
|
|
json_for_sending = {"user_group": g.user_params['group_id'], "message": 'info: Test message'}
|
2022-12-06 21:04:53 +00:00
|
|
|
send_message_to_rabbit(json.dumps(json_for_sending))
|
|
|
|
except Exception as e:
|
2024-06-19 17:52:24 +00:00
|
|
|
raise Exception(f'Cannot send message {e}')
|
2022-12-06 21:04:53 +00:00
|
|
|
|
|
|
|
|
2024-08-02 09:50:02 +00:00
|
|
|
def check_email_alert() -> str:
|
2022-12-06 21:04:53 +00:00
|
|
|
subject = 'test message'
|
|
|
|
message = 'Test message from Roxy-WI'
|
|
|
|
|
|
|
|
try:
|
2024-08-02 09:50:02 +00:00
|
|
|
user = user_sql.get_user_id(g.user_params['user_id'])
|
2022-12-06 21:04:53 +00:00
|
|
|
except Exception as e:
|
2024-08-02 09:50:02 +00:00
|
|
|
return f'error: Cannot get a user email: {e}'
|
2022-12-06 21:04:53 +00:00
|
|
|
|
|
|
|
try:
|
2024-08-02 09:50:02 +00:00
|
|
|
send_email(user.email, subject, message)
|
2022-12-06 21:04:53 +00:00
|
|
|
except Exception as e:
|
2024-08-02 09:50:02 +00:00
|
|
|
return f'error: Cannot send a message {e}'
|
2022-12-06 21:04:53 +00:00
|
|
|
|
2024-08-02 09:50:02 +00:00
|
|
|
return 'ok'
|
2022-12-06 21:04:53 +00:00
|
|
|
|
2023-04-15 21:26:54 +00:00
|
|
|
|
2024-08-02 09:50:02 +00:00
|
|
|
def add_receiver(receiver: str, token: str, channel: str, group: str, is_api=False) -> str:
|
|
|
|
last_id = channel_sql.insert_new_receiver(receiver, token, channel, group)
|
2023-04-15 21:26:54 +00:00
|
|
|
|
2024-08-02 09:50:02 +00:00
|
|
|
if is_api:
|
|
|
|
return last_id
|
2024-04-17 08:06:29 +00:00
|
|
|
else:
|
2024-08-02 09:50:02 +00:00
|
|
|
lang = roxywi_common.get_user_lang_for_flask()
|
|
|
|
new_channel = channel_sql.select_receiver(receiver, last_id)
|
|
|
|
groups = group_sql.select_groups()
|
|
|
|
roxywi_common.logging('Roxy-WI server', f'A new {receiver.title()} channel {channel} has been created ', roxywi=1, login=1)
|
|
|
|
return render_template('ajax/new_receiver.html', groups=groups, lang=lang, channel=new_channel, receiver=receiver)
|
2024-04-17 08:06:29 +00:00
|
|
|
|
|
|
|
|
2023-04-15 21:26:54 +00:00
|
|
|
def delete_receiver_channel(channel_id: int, receiver_name: str) -> None:
|
2023-09-17 09:42:39 +00:00
|
|
|
try:
|
2024-08-02 09:50:02 +00:00
|
|
|
channel_sql.delete_receiver(receiver_name, channel_id)
|
2023-09-17 09:42:39 +00:00
|
|
|
except Exception as e:
|
2024-08-02 09:50:02 +00:00
|
|
|
raise e
|
2023-04-15 21:26:54 +00:00
|
|
|
|
|
|
|
|
2024-08-02 09:50:02 +00:00
|
|
|
def update_receiver_channel(receiver_name: str, token: str, channel: str, group: id, channel_id: int) -> None:
|
|
|
|
try:
|
|
|
|
channel_sql.update_receiver(receiver_name, token, channel, group, channel_id)
|
|
|
|
except Exception as e:
|
|
|
|
raise e
|
2023-04-15 21:26:54 +00:00
|
|
|
|
|
|
|
|
2024-06-17 13:09:34 +00:00
|
|
|
def check_receiver(channel_id: int, receiver_name: str) -> None:
|
2023-04-15 21:26:54 +00:00
|
|
|
functions = {
|
|
|
|
"telegram": telegram_send_mess,
|
|
|
|
"slack": slack_send_mess,
|
|
|
|
"pd": pd_send_mess,
|
2024-04-17 08:06:29 +00:00
|
|
|
"mm": mm_send_mess,
|
2023-04-15 21:26:54 +00:00
|
|
|
}
|
|
|
|
mess = 'Test message from Roxy-WI'
|
|
|
|
|
|
|
|
if receiver_name == 'pd':
|
|
|
|
level = 'warning'
|
|
|
|
else:
|
|
|
|
level = 'info'
|
|
|
|
|
|
|
|
try:
|
2024-06-17 13:09:34 +00:00
|
|
|
functions[receiver_name](mess, level, channel_id=channel_id)
|
2023-04-15 21:26:54 +00:00
|
|
|
except Exception as e:
|
2024-06-17 13:09:34 +00:00
|
|
|
raise Exception(e)
|
2024-03-17 06:38:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def load_channels():
|
|
|
|
try:
|
|
|
|
user_subscription = roxywi_common.return_user_status()
|
|
|
|
except Exception as e:
|
|
|
|
user_subscription = roxywi_common.return_unsubscribed_user_status()
|
|
|
|
roxywi_common.logging('Roxy-WI server', f'Cannot get a user plan: {e}', roxywi=1)
|
|
|
|
|
|
|
|
try:
|
|
|
|
user_params = roxywi_common.get_users_params()
|
|
|
|
except Exception:
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
kwargs = {
|
|
|
|
'user_subscription': user_subscription,
|
|
|
|
'user_params': user_params,
|
|
|
|
'lang': user_params['lang']
|
|
|
|
}
|
|
|
|
|
|
|
|
if user_subscription['user_status']:
|
|
|
|
user_group = roxywi_common.get_user_group(id=1)
|
2024-08-02 09:50:02 +00:00
|
|
|
kwargs.setdefault('telegrams', channel_sql.get_user_receiver_by_group('telegram', user_group))
|
|
|
|
kwargs.setdefault('pds', channel_sql.get_user_receiver_by_group('pd', user_group))
|
|
|
|
kwargs.setdefault('mms', channel_sql.get_user_receiver_by_group('mm', user_group))
|
2024-03-17 06:38:00 +00:00
|
|
|
kwargs.setdefault('groups', group_sql.select_groups())
|
2024-08-02 09:50:02 +00:00
|
|
|
kwargs.setdefault('slacks', channel_sql.get_user_receiver_by_group('slack', user_group))
|
2024-03-17 06:38:00 +00:00
|
|
|
kwargs.setdefault('user_subscription', user_subscription)
|
|
|
|
kwargs.setdefault('user_params', user_params)
|
|
|
|
kwargs.setdefault('lang', user_params['lang'])
|
|
|
|
|
|
|
|
return render_template('ajax/channels.html', **kwargs)
|