diff --git a/plugins/tgbot/init.d/tgbot.tpl b/plugins/tgbot/init.d/tgbot.tpl index a7c2aaea5..55e6097f2 100644 --- a/plugins/tgbot/init.d/tgbot.tpl +++ b/plugins/tgbot/init.d/tgbot.tpl @@ -33,6 +33,7 @@ tg_start(){ echo -e "starting tgbot... \c" cd $mw_path python3 {$APP_PATH}/tgbot.py >> {$APP_PATH}/task.log & + python3 {$APP_PATH}/tgpush.py >> {$APP_PATH}/push.log & isStart="" while [[ "$isStart" == "" ]]; do @@ -64,6 +65,15 @@ tg_stop(){ kill -9 $p > /dev/null 2>&1 done echo -e "\033[32mdone\033[0m" + + + echo -e "stopping tgpush ... \c"; + arr=`ps aux|grep 'tgpush.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 diff --git a/plugins/tgbot/startup/tgbot.py b/plugins/tgbot/startup/tgbot.py index b0eb3d794..75fc6d1ef 100644 --- a/plugins/tgbot/startup/tgbot.py +++ b/plugins/tgbot/startup/tgbot.py @@ -95,15 +95,6 @@ for p in init_list: writeLog(mw.getTracebackInfo()) writeLog('-----init error end -------') -# @bot.message_handler(commands=['start', 'help']) -# def hanle_start_help(message): -# bot.reply_to(message, "hello world") - - -# @bot.message_handler(commands=['mw_echo']) -# def hanle_mw_echo(message): -# bot.reply_to(message, message.text) - @bot.message_handler(commands=['chat_id']) def hanle_get_chat_id(message): @@ -136,42 +127,6 @@ def callback_query_handler(call): writeLog('-----callback_query_handler error end -------') -def runBotPushTask(): - plist = getStartExtCfgByTag('push') - for p in plist: - try: - script = p['name'].split('.')[0] - __import__(script).run(bot) - except Exception as e: - writeLog('-----runBotPushTask error start -------') - writeLog(mw.getTracebackInfo()) - writeLog('-----runBotPushTask error end -------') - - -def botPush(): - while True: - runBotPushTask() - time.sleep(1) - - -def runBotPushOtherTask(): - plist = getStartExtCfgByTag('other') - for p in plist: - try: - script = p['name'].split('.')[0] - __import__(script).run(bot) - except Exception as e: - writeLog('-----runBotPushOtherTask error start -------') - writeLog(mw.getTracebackInfo()) - writeLog('-----runBotPushOtherTask error end -------') - - -def botPushOther(): - while True: - runBotPushOtherTask() - time.sleep(1) - - def runBot(bot): try: bot.polling() @@ -184,14 +139,6 @@ def runBot(bot): if __name__ == "__main__": - # 机器人推送任务 - botPushTask = threading.Thread(target=botPush) - botPushTask.start() - - # 机器人其他推送任务 - botPushOtherTask = threading.Thread(target=botPushOther) - botPushOtherTask.start() - writeLog('启动成功') runBot(bot) diff --git a/plugins/tgbot/startup/tgpush.py b/plugins/tgbot/startup/tgpush.py new file mode 100644 index 000000000..fcf34a811 --- /dev/null +++ b/plugins/tgbot/startup/tgpush.py @@ -0,0 +1,132 @@ +# coding:utf-8 + +import sys +import io +import os +import time +import re +import json +import base64 +import threading + +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() + + +sys.path.append(getServerDir() + "/extend") + + +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 getExtCfg(): + cfg_path = getServerDir() + "/extend.cfg" + if not os.path.exists(cfg_path): + mw.writeFile(cfg_path, '{}') + t = mw.readFile(cfg_path) + return json.loads(t) + + +def getStartExtCfgByTag(tag='push'): + # 获取开启的扩展 + elist = getExtCfg() + rlist = [] + for x in elist: + if x['tag'] == tag and x['status'] == 'start': + rlist.append(x) + return rlist + + +def writeLog(log_str): + if __name__ == "__main__": + print(log_str) + + now = mw.getDateFromNow() + log_file = getServerDir() + '/push.log' + mw.writeFileLog(now + ':' + log_str, log_file, limit_size=5 * 1024) + return True + +# start tgbot +cfg = getConfigData() +while True: + cfg = getConfigData() + if 'bot' in cfg and 'app_token' in cfg['bot']: + if cfg['bot']['app_token'] != '' and cfg['bot']['app_token'] != 'app_token': + break + writeLog('等待输入配置,填写app_token') + time.sleep(3) + + +bot = telebot.TeleBot(cfg['bot']['app_token']) + + +def runBotPushTask(): + plist = getStartExtCfgByTag('push') + for p in plist: + try: + script = p['name'].split('.')[0] + __import__(script).run(bot) + except Exception as e: + writeLog('-----runBotPushTask error start -------') + writeLog(mw.getTracebackInfo()) + writeLog('-----runBotPushTask error end -------') + + +def botPush(): + while True: + runBotPushTask() + time.sleep(1) + + +def runBotPushOtherTask(): + plist = getStartExtCfgByTag('other') + for p in plist: + try: + script = p['name'].split('.')[0] + __import__(script).run(bot) + except Exception as e: + writeLog('-----runBotPushOtherTask error start -------') + writeLog(mw.getTracebackInfo()) + writeLog('-----runBotPushOtherTask error end -------') + + +def botPushOther(): + while True: + runBotPushOtherTask() + time.sleep(1) + + +if __name__ == "__main__": + + # 机器人推送任务 + botPushTask = threading.Thread(target=botPush) + botPushTask.start() + + # 机器人其他推送任务 + botPushOtherTask = threading.Thread(target=botPushOther) + botPushOtherTask.start()