mirror of https://github.com/midoks/mdserver-web
parent
e129051a06
commit
7301edbc5a
After Width: | Height: | Size: 974 B |
@ -0,0 +1,24 @@ |
||||
<div class="bt-form"> |
||||
<div class='plugin_version'></div> |
||||
<div class="bt-w-main"> |
||||
<div class="bt-w-menu"> |
||||
<p class="bgw" onclick="pluginService('tgbot');">服务</p> |
||||
<p onclick="pluginInitD('tgbot');">自启动</p> |
||||
<p onclick="botConf();">Bot配置</p> |
||||
</div> |
||||
<div class="bt-w-con pd15"> |
||||
<div class="soft-man-con"></div> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
<style> |
||||
.conf_p span{ |
||||
width: 70px; |
||||
} |
||||
</style> |
||||
<script type="text/javascript"> |
||||
resetPluginWinHeight(300); |
||||
$.getScript( "/plugins/file?name=tgbot&f=js/tgbot.js", function(){ |
||||
pluginService('tgbot'); |
||||
}); |
||||
</script> |
@ -0,0 +1,239 @@ |
||||
# coding:utf-8 |
||||
|
||||
import sys |
||||
import io |
||||
import os |
||||
import time |
||||
import re |
||||
import json |
||||
import base64 |
||||
|
||||
sys.path.append(os.getcwd() + "/class/core") |
||||
import mw |
||||
|
||||
app_debug = False |
||||
if mw.isAppleSystem(): |
||||
app_debug = True |
||||
|
||||
|
||||
def getPluginName(): |
||||
return 'tgbot' |
||||
|
||||
|
||||
def getPluginDir(): |
||||
return mw.getPluginDir() + '/' + getPluginName() |
||||
|
||||
|
||||
def getServerDir(): |
||||
return mw.getServerDir() + '/' + getPluginName() |
||||
|
||||
|
||||
def getInitDFile(): |
||||
if app_debug: |
||||
return '/tmp/' + getPluginName() |
||||
return '/etc/init.d/' + getPluginName() |
||||
|
||||
|
||||
def getConfigData(): |
||||
cfg_path = getServerDir() + "/data.cfg" |
||||
if not os.path.exists(cfg_path): |
||||
mw.writeFile(cfg_path, '{}') |
||||
t = mw.readFile(cfg_path) |
||||
return json.loads(t) |
||||
|
||||
|
||||
def writeConf(data): |
||||
cfg_path = getServerDir() + "/data.cfg" |
||||
mw.writeFile(cfg_path, json.dumps(data)) |
||||
return True |
||||
|
||||
|
||||
def getInitDTpl(): |
||||
path = getPluginDir() + "/init.d/" + getPluginName() + ".tpl" |
||||
return path |
||||
|
||||
|
||||
def getArgs(): |
||||
args = sys.argv[2:] |
||||
tmp = {} |
||||
args_len = len(args) |
||||
if args_len == 1: |
||||
t = args[0].strip('{').strip('}') |
||||
if t.strip() == '': |
||||
tmp = [] |
||||
else: |
||||
t = t.split(':') |
||||
tmp[t[0]] = t[1] |
||||
tmp[t[0]] = t[1] |
||||
elif args_len > 1: |
||||
for i in range(len(args)): |
||||
t = args[i].split(':') |
||||
tmp[t[0]] = t[1] |
||||
return tmp |
||||
|
||||
|
||||
def checkArgs(data, ck=[]): |
||||
for i in range(len(ck)): |
||||
if not ck[i] in data: |
||||
return (False, mw.returnJson(False, '参数:(' + ck[i] + ')没有!')) |
||||
return (True, mw.returnJson(True, 'ok')) |
||||
|
||||
|
||||
def status(): |
||||
data = mw.execShell( |
||||
"ps -ef|grep tgbot |grep -v grep | grep -v mdserver-web | awk '{print $2}'") |
||||
if data[0] == '': |
||||
return 'stop' |
||||
return 'start' |
||||
|
||||
|
||||
def initDreplace(): |
||||
|
||||
file_tpl = getInitDTpl() |
||||
service_path = mw.getServerDir() |
||||
|
||||
initD_path = getServerDir() + '/init.d' |
||||
if not os.path.exists(initD_path): |
||||
os.mkdir(initD_path) |
||||
file_bin = initD_path + '/' + getPluginName() |
||||
|
||||
# initd replace |
||||
# if not os.path.exists(file_bin): |
||||
content = mw.readFile(file_tpl) |
||||
content = content.replace('{$SERVER_PATH}', service_path + '/mdserver-web') |
||||
content = content.replace( |
||||
'{$APP_PATH}', service_path + '/' + getPluginName()) |
||||
|
||||
mw.writeFile(file_bin, content) |
||||
mw.execShell('chmod +x ' + file_bin) |
||||
|
||||
# systemd |
||||
systemDir = mw.systemdCfgDir() |
||||
systemService = systemDir + '/tgbot.service' |
||||
systemServiceTpl = getPluginDir() + '/init.d/tgbot.service.tpl' |
||||
if os.path.exists(systemDir) and not os.path.exists(systemService): |
||||
service_path = mw.getServerDir() |
||||
se_content = mw.readFile(systemServiceTpl) |
||||
se_content = se_content.replace('{$SERVER_PATH}', service_path) |
||||
mw.writeFile(systemService, se_content) |
||||
mw.execShell('systemctl daemon-reload') |
||||
|
||||
return file_bin |
||||
|
||||
|
||||
def tbOp(method): |
||||
file = initDreplace() |
||||
|
||||
if not mw.isAppleSystem(): |
||||
data = mw.execShell('systemctl ' + method + ' ' + getPluginName()) |
||||
if data[1] == '': |
||||
return 'ok' |
||||
return data[1] |
||||
|
||||
data = mw.execShell(file + ' ' + method) |
||||
print(data) |
||||
# if data[1] == '': |
||||
# return 'ok' |
||||
return 'ok' |
||||
|
||||
|
||||
def start(): |
||||
return tbOp('start') |
||||
|
||||
|
||||
def stop(): |
||||
return tbOp('stop') |
||||
|
||||
|
||||
def restart(): |
||||
status = tbOp('restart') |
||||
return status |
||||
|
||||
|
||||
def reload(): |
||||
|
||||
tgbot_tpl = getPluginDir() + '/startup/tgbot.py' |
||||
tgbot_dst = getServerDir() + '/tgbot.py' |
||||
|
||||
content = mw.readFile(tgbot_tpl) |
||||
mw.writeFile(tgbot_dst, content) |
||||
|
||||
return tbOp('reload') |
||||
|
||||
|
||||
def initdStatus(): |
||||
if mw.isAppleSystem(): |
||||
return "Apple Computer does not support" |
||||
|
||||
shell_cmd = 'systemctl status ' + \ |
||||
getPluginName() + ' | grep loaded | grep "enabled;"' |
||||
data = mw.execShell(shell_cmd) |
||||
if data[0] == '': |
||||
return 'fail' |
||||
return 'ok' |
||||
|
||||
|
||||
def initdInstall(): |
||||
if mw.isAppleSystem(): |
||||
return "Apple Computer does not support" |
||||
|
||||
mw.execShell('systemctl enable ' + getPluginName()) |
||||
return 'ok' |
||||
|
||||
|
||||
def initdUinstall(): |
||||
if mw.isAppleSystem(): |
||||
return "Apple Computer does not support" |
||||
|
||||
mw.execShell('systemctl disable ' + getPluginName()) |
||||
return 'ok' |
||||
|
||||
|
||||
def getBotConf(): |
||||
data = getConfigData() |
||||
if 'bot' in data: |
||||
|
||||
return mw.returnJson(True, 'ok', data['bot']) |
||||
return mw.returnJson(False, 'ok', {}) |
||||
|
||||
|
||||
def setBotConf(): |
||||
args = getArgs() |
||||
data_args = checkArgs(args, ['app_token']) |
||||
if not data_args[0]: |
||||
return data_args[1] |
||||
|
||||
data = getConfigData() |
||||
args['app_token'] = base64.b64decode(args['app_token']).decode('ascii') |
||||
data['bot'] = args |
||||
writeConf(data) |
||||
|
||||
return mw.returnJson(True, '保存成功!', []) |
||||
|
||||
|
||||
if __name__ == "__main__": |
||||
func = sys.argv[1] |
||||
if func == 'status': |
||||
print(status()) |
||||
elif func == 'start': |
||||
print(start()) |
||||
elif func == 'stop': |
||||
print(stop()) |
||||
elif func == 'restart': |
||||
print(restart()) |
||||
elif func == 'reload': |
||||
print(reload()) |
||||
elif func == 'initd_status': |
||||
print(initdStatus()) |
||||
elif func == 'initd_install': |
||||
print(initdInstall()) |
||||
elif func == 'initd_uninstall': |
||||
print(initdUinstall()) |
||||
elif func == 'get_bot_conf': |
||||
print(getBotConf()) |
||||
elif func == 'set_bot_conf': |
||||
print(setBotConf()) |
||||
elif func == 'conf': |
||||
print(getConf()) |
||||
else: |
||||
print('error') |
@ -0,0 +1,17 @@ |
||||
{ |
||||
"sort": 7, |
||||
"ps": "[<b style='color:red;'>DEV</b>]简单Telegram机器人", |
||||
"name": "tgbot", |
||||
"title": "tgbot", |
||||
"shell": "install.sh", |
||||
"versions":["0.1"], |
||||
"tip": "soft", |
||||
"checks": "server/tgbot", |
||||
"path": "server/tgbot", |
||||
"display": 1, |
||||
"author": "Zend", |
||||
"date": "2023-03-06", |
||||
"home": "https://core.telegram.org/bots/api", |
||||
"type": 0, |
||||
"pid": "5" |
||||
} |
@ -0,0 +1,15 @@ |
||||
[Unit] |
||||
Description=Redis In-Memory Data Store |
||||
After=network.target |
||||
|
||||
[Service] |
||||
Type=forking |
||||
ExecStart={$APP_PATH}/init.d/tgbot start |
||||
ExecStop={$APP_PATH}/init.d/tgbot stop |
||||
ExecReload={$APP_PATH}/init.d/tgbot reload |
||||
ExecRestart={$APP_PATH}/init.d/tgbot restart |
||||
KillMode=process |
||||
Restart=on-failure |
||||
|
||||
[Install] |
||||
WantedBy=multi-user.target |
@ -0,0 +1,85 @@ |
||||
#!/bin/sh |
||||
# chkconfig: 2345 55 25 |
||||
# description: Tgbot Service |
||||
|
||||
### BEGIN INIT INFO |
||||
# Provides: Tgbot |
||||
# Required-Start: $all |
||||
# Required-Stop: $all |
||||
# Default-Start: 2 3 4 5 |
||||
# Default-Stop: 0 1 6 |
||||
# Short-Description: starts Tgbot |
||||
# Description: starts the MDW-Web |
||||
### END INIT INFO |
||||
|
||||
# Simple Tgbot init.d script conceived to work on Linux systems |
||||
# as it does use of the /proc filesystem. |
||||
|
||||
PATH=/usr/local/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin |
||||
export LANG=en_US.UTF-8 |
||||
|
||||
|
||||
mw_path={$SERVER_PATH} |
||||
PATH=$PATH:$mw_path/bin |
||||
|
||||
if [ -f $mw_path/bin/activate ];then |
||||
source $mw_path/bin/activate |
||||
fi |
||||
|
||||
tg_start(){ |
||||
|
||||
isStart=`ps -ef|grep 'tgbot.py' |grep -v grep | awk '{print $2}'` |
||||
if [ "$isStart" == '' ];then |
||||
echo -e "starting tgbot... \c" |
||||
cd $mw_path |
||||
python3 {$APP_PATH}/tgbot.py >> {$APP_PATH}/task.log & |
||||
isStart="" |
||||
while [[ "$isStart" == "" ]]; |
||||
do |
||||
echo -e ".\c" |
||||
sleep 0.5 |
||||
isStart=`ps -ef|grep 'tgbot.py' |grep -v grep | awk '{print $2}'` |
||||
let n+=1 |
||||
if [ $n -gt 20 ];then |
||||
break; |
||||
fi |
||||
done |
||||
if [ "$isStart" == '' ];then |
||||
echo -e "\033[31mfailed\033[0m" |
||||
echo -e "\033[31mError: tgbot service startup failed.\033[0m" |
||||
return; |
||||
fi |
||||
echo -e "\033[32mdone\033[0m" |
||||
else |
||||
echo "starting tgbot...(pid $(echo $isStart)) already running" |
||||
fi |
||||
} |
||||
|
||||
|
||||
tg_stop(){ |
||||
echo -e "stopping tgbot ... \c"; |
||||
arr=`ps aux|grep 'tgbot.py'|grep -v grep|awk '{print $2}'` |
||||
for p in ${arr[@]} |
||||
do |
||||
kill -9 $p > /dev/null 2>&1 |
||||
done |
||||
echo -e "\033[32mdone\033[0m" |
||||
} |
||||
|
||||
case "$1" in |
||||
start) |
||||
tg_start |
||||
;; |
||||
stop) |
||||
tg_stop |
||||
;; |
||||
restart|reload) |
||||
tg_stop |
||||
sleep 0.3 |
||||
tg_start |
||||
;; |
||||
*) |
||||
echo "Please use start or stop as first argument" |
||||
;; |
||||
esac |
||||
|
@ -0,0 +1,59 @@ |
||||
#!/bin/bash |
||||
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin |
||||
export PATH |
||||
|
||||
curPath=`pwd` |
||||
rootPath=$(dirname "$curPath") |
||||
rootPath=$(dirname "$rootPath") |
||||
serverPath=$(dirname "$rootPath") |
||||
|
||||
|
||||
install_tmp=${rootPath}/tmp/mw_install.pl |
||||
|
||||
VERSION=$2 |
||||
|
||||
# pip3 install ccxt |
||||
if [ -f ${rootPath}/bin/activate ];then |
||||
source ${rootPath}/bin/activate |
||||
fi |
||||
|
||||
pip3 install pyTelegramBotAPI |
||||
pip3 install telebot |
||||
|
||||
Install_App() |
||||
{ |
||||
echo '正在安装脚本文件...' > $install_tmp |
||||
mkdir -p $serverPath/source |
||||
mkdir -p $serverPath/tgbot |
||||
echo "${VERSION}" > $serverPath/tgbot/version.pl |
||||
|
||||
cp -rf ${rootPath}/plugins/tgbot/startup/* $serverPath/tgbot |
||||
|
||||
cd ${rootPath} && python3 ${rootPath}/plugins/tgbot/index.py start |
||||
cd ${rootPath} && python3 ${rootPath}/plugins/tgbot/index.py initd_install |
||||
echo '安装完成' > $install_tmp |
||||
} |
||||
|
||||
Uninstall_App() |
||||
{ |
||||
if [ -f /usr/lib/systemd/system/tgbot.service ];then |
||||
systemctl stop tgbot |
||||
systemctl disable tgbot |
||||
rm -rf /usr/lib/systemd/system/tgbot.service |
||||
systemctl daemon-reload |
||||
fi |
||||
|
||||
if [ -f $serverPath/tgbot/initd/tgbot ];then |
||||
$serverPath/tgbot/initd/tgbot stop |
||||
fi |
||||
|
||||
rm -rf $serverPath/tgbot |
||||
echo "Uninstall_redis" > $install_tmp |
||||
} |
||||
|
||||
action=$1 |
||||
if [ "${1}" == 'install' ];then |
||||
Install_App |
||||
else |
||||
Uninstall_App |
||||
fi |
@ -0,0 +1,78 @@ |
||||
function appPost(method, args,callback){ |
||||
var loadT = layer.msg('正在获取...', { icon: 16, time: 0, shade: 0.3 }); |
||||
|
||||
var req_data = {}; |
||||
req_data['name'] = 'tgbot'; |
||||
req_data['func'] = method; |
||||
|
||||
if (typeof(args) == 'string'){ |
||||
req_data['args'] = JSON.stringify(toArrayObject(args)); |
||||
} else { |
||||
req_data['args'] = JSON.stringify(args); |
||||
} |
||||
|
||||
$.post('/plugins/run', req_data, function(data) { |
||||
layer.close(loadT); |
||||
if(typeof(callback) == 'function'){ |
||||
callback(data); |
||||
} |
||||
},'json');
|
||||
} |
||||
|
||||
function appPostCallbak(method, args,callback){ |
||||
var loadT = layer.msg('正在获取...', { icon: 16, time: 0, shade: 0.3 }); |
||||
|
||||
var req_data = {}; |
||||
req_data['name'] = 'tgbot'; |
||||
req_data['func'] = method; |
||||
|
||||
if (typeof(args) == 'string'){ |
||||
req_data['args'] = JSON.stringify(toArrayObject(args)); |
||||
} else { |
||||
req_data['args'] = JSON.stringify(args); |
||||
} |
||||
|
||||
$.post('/plugins/callback', req_data, function(data) { |
||||
layer.close(loadT); |
||||
if (!data.status){ |
||||
layer.msg(data.msg,{icon:0,time:2000,shade: [0.3, '#000']}); |
||||
return; |
||||
} |
||||
|
||||
if(typeof(callback) == 'function'){ |
||||
callback(data); |
||||
} |
||||
},'json');
|
||||
} |
||||
|
||||
function botConf(){ |
||||
appPost('get_bot_conf','',function(data){ |
||||
var rdata = $.parseJSON(data.data); |
||||
var app_token = 'app_token'; |
||||
if(rdata['status']){ |
||||
db_data = rdata['data']; |
||||
app_token = db_data['app_token']; |
||||
|
||||
} |
||||
|
||||
var mlist = ''; |
||||
mlist += '<p><span>app_token</span><input style="width: 250px;" class="bt-input-text mr5" name="app_token" value="'+app_token+'" type="text"><font>必填写</font></p>' |
||||
var option = '<style>.conf_p p{margin-bottom: 2px}</style>\ |
||||
<div class="conf_p" style="margin-bottom:0">\ |
||||
' + mlist + '\ |
||||
<div style="margin-top:10px; padding-right:15px" class="text-right">\ |
||||
<button class="btn btn-success btn-sm" onclick="submitBotConf()">保存</button>\ |
||||
</div>\ |
||||
</div>'; |
||||
$(".soft-man-con").html(option); |
||||
}); |
||||
} |
||||
|
||||
function submitBotConf(){ |
||||
var pull_data = {}; |
||||
pull_data['app_token'] = base64_encode($('input[name="app_token"]').val()); |
||||
appPost('set_bot_conf',pull_data,function(data){ |
||||
var rdata = $.parseJSON(data.data); |
||||
layer.msg(rdata['msg'],{icon:rdata['status']?1:2,time:2000,shade: [0.3, '#000']}); |
||||
}); |
||||
} |
@ -0,0 +1,63 @@ |
||||
|
||||
# coding:utf-8 |
||||
|
||||
import sys |
||||
import io |
||||
import os |
||||
import time |
||||
import re |
||||
import json |
||||
import base64 |
||||
|
||||
sys.path.append(os.getcwd() + "/class/core") |
||||
import mw |
||||
|
||||
import telebot |
||||
|
||||
|
||||
def getPluginName(): |
||||
return 'tgbot' |
||||
|
||||
|
||||
def getPluginDir(): |
||||
return mw.getPluginDir() + '/' + getPluginName() |
||||
|
||||
|
||||
def getServerDir(): |
||||
return mw.getServerDir() + '/' + getPluginName() |
||||
|
||||
|
||||
def getConfigData(): |
||||
cfg_path = getServerDir() + "/data.cfg" |
||||
if not os.path.exists(cfg_path): |
||||
mw.writeFile(cfg_path, '{}') |
||||
t = mw.readFile(cfg_path) |
||||
return json.loads(t) |
||||
|
||||
|
||||
def writeConf(data): |
||||
cfg_path = getServerDir() + "/data.cfg" |
||||
mw.writeFile(cfg_path, json.dumps(data)) |
||||
return True |
||||
|
||||
# start tgbot |
||||
cfg = getConfigData() |
||||
bot = telebot.TeleBot(cfg['bot']['app_token']) |
||||
|
||||
|
||||
# from telebot.async_telebot import AsyncTeleBot |
||||
# import asyncio |
||||
# bot = AsyncTeleBot(cfg['bot']['app_token']) |
||||
|
||||
|
||||
@bot.message_handler(commands=['start', 'help']) |
||||
def hanle_start_help(message): |
||||
bot.reply_to(message, "hello world") |
||||
|
||||
|
||||
@bot.message_handler(commands=['chat_id']) |
||||
def hanle_get_chat_id(message): |
||||
bot.reply_to(message, message.chat.id) |
||||
|
||||
bot.polling() |
||||
# asyncio.run(bot.polling()) |
Loading…
Reference in new issue