Files
aaPanel/class/msg/tg_msg.py
T
Jack ed55fa708d Update to 7.7.0
Since version 7.7.0, we recommend yours update python to 3.12.

[+] Using nginx technology to load static files improves access speed
[+] Refactor homepage, website, FTP, and database using vue3
[+] Table loading changed to skeleton screen
[+] Add Website statistics-v2 professional plug-in
[+] Add Home page - top 5 resource occupancy
[+] Add protection for Files management (requires Tamper-proof for Enterprise 3.7)
[+] Website, FTP, Databases page add program status
[+] Add FTP log analysis (only supports Centos)
[+] Add password-free login to phpMyAdmin
[+] Add Proxy Project in Website (Supported when web service uses Nginx)
[+] Add WP Toolkit (Pro version only)
[+] Redesigned Docker module
[+] Add WP Toolkit Protection
[+] Add WP Toolkit Backup and Restore
[+] Add WP Toolkit Migrated
[+] Add WP Toolkit Clone site (supports new domain and subdomain)
[+] Add WP Toolkit Create site from backup of other panel
[+] Add WP Toolkit support for Cron automatic backup (only save Local disk)
[+] Add WP Toolkit operation log
[+] Add Integrity check for WP Toolkit
[+] Add WP Toolkit plug-in management and themes management

[*] Optimize phpMyAdmin formula access method
[*] Optimize Home page PHP display problem
[*] Optimize jump to the login interface after the login expires
[*] Optimize automatic renewal of SSL at some times
[*] Optimize Let's Encrypt to increase application success rate

[-] Fix Logs Audit cannot be opened
[-] Fix apache URL rewrite issue
[-] Fix phpmyadmin installation problem
[-] Fix the problem that some servers cannot install software
[-] Fix upload file error
[-] Fix left menu hiding problem
[-] Fix aaPanel Mobile QR code display problem
[-] Fix problem that third-party plug-ins are not displayed in the App Store
[-] Fix issue where the menu bar is blank when opening new tabs
[-] Fixed panel not being accessible in some cases
[-] Fix the issue where Curl warning caused the inability to apply for SSL
[-] Fix Quota issues for Website, FTP, Databases
[-] Fix file interface display problem on mobile terminal
2024-07-19 11:25:10 +08:00

283 lines
8.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# coding: utf-8
# +-------------------------------------------------------------------
# | aaPanel
# +-------------------------------------------------------------------
# | Copyright (c) 2015-2020 aaPanel(www.aapanel.com) All rights reserved.
# +-------------------------------------------------------------------
# | Author: jose <zhw@aapanel.com>
# | 消息通道电报模块
# +-------------------------------------------------------------------
import sys, os, re, asyncio, public, json, requests
try:
import telegram
except:
public.ExecShell("btpip install -I python-telegram-bot")
import telegram
panelPath = "/www/server/panel"
os.chdir(panelPath)
sys.path.insert(0, panelPath + "/class/")
from requests.packages import urllib3
# 关闭警告
urllib3.disable_warnings()
class tg_msg:
conf_path = "{}/data/tg_bot.json".format(panelPath)
__tg_info = None
__module_name = None
__default_pl = "{}/data/default_msg_channel.pl".format(panelPath)
def __init__(self):
try:
red_conf_path = public.readFile(self.conf_path)
self.__tg_info = json.loads(red_conf_path)
if not 'bot_token' in self.__tg_info or not 'my_id' in self.__tg_info:
self.__tg_info = None
except:
self.__tg_info = None
self.__module_name = self.__class__.__name__.replace('_msg', '')
def get_version_info(self, get):
"""
获取版本信息
"""
data = {}
data['ps'] = 'Use telegram bots to send receive panel notifications'
data['version'] = '1.0'
data['date'] = '2022-08-10'
data['author'] = 'aaPanel'
data['title'] = 'Telegram'
data['help'] = 'http://www.aapanel.com'
return data
def get_config(self, get):
"""
获取tg配置
"""
data = {}
if self.__tg_info:
data = self.__tg_info
data['default'] = self.__get_default_channel()
return data
def set_config(self, get):
"""
设置tg bot
@my_id tg id
@bot_token 机器人token
"""
if not hasattr(get, 'my_id') or not hasattr(get, 'bot_token'):
return public.returnMsg(False, 'Please fill in the complete information')
title = 'Default'
if hasattr(get, 'title'):
title = get.title
if len(title) > 7:
return public.returnMsg(False, 'Note name cannot exceed 7 characters')
self.__tg_info = {"my_id": get.my_id.strip(), "bot_token": get.bot_token, "title": title, "status": True}
try:
info = public.get_push_info('Notification Configuration Reminder',
['>Configuration status<font color=#20a53a>successfully</font>\n\n'])
ret = self.send_msg(info['msg'])
except:
ret = self.send_msg('aaPanel alarm test')
if ret:
if 'default' in get and get['default']:
public.writeFile(self.__default_pl, self.__module_name)
public.writeFile(self.conf_path, json.dumps(self.__tg_info))
return public.returnMsg(True, 'successfully set')
else:
return ret
def get_send_msg(self, msg):
"""
@name 处理md格式
"""
try:
title = 'aaPanel notifications'
if msg.find("####") >= 0:
try:
title = re.search(r"####(.+)", msg).groups()[0]
except:
pass
else:
info = public.get_push_info('Notification Configuration Reminder', ['>Send Content: ' + msg])
msg = info['msg']
except:
pass
return msg, title
def process_character(self, msg):
"""
格式化消息
"""
# 去掉无用的转义字符
msg = msg.replace('\\', '')
# 去掉 HTML 标签
msg = re.sub(r'<[^>]+>', '', msg)
# 去掉标题中的 ####
msg = msg.replace('####', '')
# > 增加空格
msg = msg.replace('>', '> ')
# 去掉标题两边的空格
title = msg.split('\n')[0].strip()
# 去掉标题后面的换行符和空行,保留消息
msg = msg.replace(f'{title}\n\n', '')
# 去掉消息前后的空格
msg = msg.strip()
character = ['\\', '_', '`', '*', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '=', '.', '!']
for c in character:
if c in msg:
msg = msg.replace(c, '\\' + c)
# 将处理后的消息发送到Telegram
msg = f'*{title}*\n\n{msg}'
return msg
async def send_msg_async(self, bot_token, chat_id, msg):
"""
tg发送信息
@msg 消息正文
"""
bot = telegram.Bot(token=bot_token)
msg = self.process_character(msg)
public.print_log(msg)
await bot.send_message(chat_id=chat_id, text=msg, parse_mode='MarkdownV2')
def send_msg(self, msg):
"""
tg发送信息
@msg 消息正文
"""
if not self.__tg_info:
return public.returnMsg(False, 'The telegram information is incorrectly configured.')
if isinstance(self.__tg_info['my_id'], int):
return public.returnMsg(False, 'Telegram configuration error, please reconfigure the robot.')
msg, title = self.get_send_msg(msg)
# public.WriteFile("/tmp/title.tg", title)
# public.WriteFile("/tmp/msg.tg", msg)
# send_content = msg
# public.WriteFile("/tmp/send_content.tg", send_content)
# bot = telegram.Bot(self.__tg_info['bot_token'])
bot_token = self.__tg_info['bot_token']
chat_id = self.__tg_info['my_id']
#text = msg
public.print_log(msg)
# public.print_log("bot:{}".format(self.__tg_info['bot_token']))
# public.print_log("my_id:{}".format(self.__tg_info['my_id']))
import asyncio
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
loop.run_until_complete(self.send_msg_async(bot_token, chat_id, msg))
ret = {'success': 1}
public.write_push_log(self.__module_name, title, ret)
public.print_log('message sent successfully')
loop.close()
return public.returnMsg(True, 'send complete, send result: True.')
except:
public.print_log('Error:{}'.format(str(public.get_error_info())))
ret = {'success': 0}
public.write_push_log(self.__module_name, title, ret)
return public.returnMsg(False, 'send complete, send result: False.')
def push_data(self, data):
"""
@name 统一发送接口
@data 消息内容
{"module":"mail","title":"标题","msg":"内容","to_email":"xx@qq.com","sm_type":"","sm_args":{}}
"""
return self.send_msg(data['msg'])
def __get_default_channel(self):
"""
@获取默认消息通道
"""
try:
if public.readFile(self.__default_pl) == self.__module_name:
return True
except:
pass
return False
def uninstall(self):
if os.path.exists(self.conf_path):
os.remove(self.conf_path)
# 获取tg机器人信息
# def get_tg_conf(self, get=None):
# conf = self.__tg_info
#
# if not conf:
# return {"setup": False, "bot_token": "", "my_id": ""}
# try:
# return self.__tg_info
# except:
# return {"setup": False, "bot_token": "", "my_id": ""}
# def process_character(self,content):
# character = ['.',',','!',':','%','[',']','\/','_','-','>']
# for c in character:
# if c in content and '\\{}'.format(c) not in content:
# content = content.replace(c,'\\'+c)
# return content
# 使用tg机器人发送消息
# def send_by_tg_bot(self,content,parse_mode=None):
# "parse_mode 消息格式 html/markdown/markdownv2"
# #content = self.process_character(content)
# conf = self.__tg_info
#
# confa1 = conf['my_id']
# public.print_log("开始检查--confa1", confa1)
# confa2 = conf['bot_token']
# public.print_log("开始检查--confa2", confa2)
#
#
# text = send_content
# public.WriteFile("/tmp/text.tg", text)
#
# bot = telegram.Bot(conf['bot_token'])
# #result = bot.send_message(text=content, chat_id=int(conf['my_id']), parse_mode="MarkdownV2")
# result = bot.send_message( chat_id=int(conf['my_id']), text=text, parse_mode='MarkdownV2')
#
# return result