def run(self, debug_mode): # TODO Use a variable to set where db file will save. global_vars.create_variable('mdb', MessageDB('message.db')) global_vars.create_variable('fdb', FileDB('files.db')) qq_bot = CQHttp(api_root=API_ROOT, access_token=ACCESS_TOKEN, secret=SECRET, debug_mode=debug_mode) global_vars.create_variable('callback_queue', queue.Queue()) global_vars.qq_bot = qq_bot global_vars.tg_bot_id = int(TOKEN.split(':')[0]) if PROXY_URL: updater = Updater(TOKEN, request_kwargs={'proxy_url': PROXY_URL}) else: updater = Updater(TOKEN) global_vars.create_variable('job_queue', updater.job_queue) global_vars.tg_bot = updater.bot # Get the dispatcher to register handlers dp = updater.dispatcher global_vars.dp = dp dp.add_error_handler(error) updater.start_polling(poll_interval=1.0, timeout=200) threaded_server = threading.Thread(target=qq_bot.run, kwargs=dict(host=HOST, port=PORT), daemon=True) threaded_server.start() should_wait = True while should_wait: try: bot_status = qq_bot.get_status() should_wait = False except Exception as e: logger.warning( 'Could not reach Coolq-http-api, keep waiting...') time.sleep(1) logger.info('Coolq-http-api status: ok') coolq_version = global_vars.qq_bot.get_version_info()['coolq_edition'] global_vars.create_variable("JQ_MODE", coolq_version == 'pro') logger.info(f'Coolq {coolq_version} detected') import plugins # load all plugins # Block until the you presses Ctrl-C or the process receives SIGINT, # SIGTERM or SIGABRT. This should be used most of the time, since # start_polling() is non-blocking and will stop the bot gracefully. updater.idle()
def decorator(handler): logger.debug('Registering new command: ' + command + '(' + handler.__name__ + ')') @wraps(handler) def return_wrapper(*args, **kwargs): logger.debug(command + '(' + handler.__name__ + ')' + ' called') return handler(*args, **kwargs) global_vars.append_command( Command(command, short_command, return_wrapper, require_admin, tg_only, qq_only, description)) # add command to global_vars, for cross-plugin access global_vars.create_variable(handler.__name__, return_wrapper) logger.debug(command + '(' + handler.__name__ + ') added to global_vars') return return_wrapper
def run(self): global_vars.create_variable('mdb', MessageDB('message.db')) qq_bot = CQHttp(api_root=API_ROOT, access_token=ACCESS_TOKEN, secret=SECRET) global_vars.create_variable('callback_queue', queue.Queue()) global_vars.qq_bot = qq_bot global_vars.tg_bot_id = int(TOKEN.split(':')[0]) updater = Updater(TOKEN) global_vars.create_variable('job_queue', updater.job_queue) global_vars.tg_bot = updater.bot # Get the dispatcher to register handlers dp = updater.dispatcher global_vars.dp = dp dp.add_error_handler(error) updater.start_polling(poll_interval=1.0, timeout=200) threaded_server = threading.Thread(target=qq_bot.run, kwargs=dict(host=HOST, port=PORT), daemon=True) threaded_server.start() import plugins # load all plugins while True: utils.from_main_thread_blocking() time.sleep(1) # Block until the you presses Ctrl-C or the process receives SIGINT, # SIGTERM or SIGABRT. This should be used most of the time, since # start_polling() is non-blocking and will stop the bot gracefully. updater.idle()
def run(self): global_vars.create_variable('mdb', MessageDB('message.db')) qq_bot = CQHttp(api_root=API_ROOT, access_token=ACCESS_TOKEN, secret=SECRET) global_vars.create_variable('callback_queue', queue.Queue()) global_vars.qq_bot = qq_bot global_vars.tg_bot_id = int(TOKEN.split(':')[0]) updater = Updater(TOKEN) global_vars.create_variable('job_queue', updater.job_queue) global_vars.tg_bot = updater.bot # Get the dispatcher to register handlers dp = updater.dispatcher global_vars.dp = dp dp.add_error_handler(error) updater.start_polling(poll_interval=1.0, timeout=200) threaded_server = threading.Thread(target=qq_bot.run, kwargs=dict(host=HOST, port=PORT), daemon=True) threaded_server.start() try: bot_status = qq_bot.get_status() except Exception as e: logger.error( 'Could not reach Coolq-http-api, please check Coolq plugins.') exit(-1) logger.debug('Coolq-http-api status: ok') import plugins # load all plugins # Block until the you presses Ctrl-C or the process receives SIGINT, # SIGTERM or SIGABRT. This should be used most of the time, since # start_polling() is non-blocking and will stop the bot gracefully. updater.idle()
from bot_constant import FORWARD_LIST import global_vars from utils import get_forward_index, send_both_side, get_plugin_priority from telegram.ext import MessageHandler, Filters from telegram.ext.dispatcher import DispatcherHandlerStop from command import command_listener import telegram import logging logger = logging.getLogger("CTBPlugin." + __name__) logger.debug(__name__ + " loading") global_vars.create_variable('DRIVE_MODE', []) for forward in FORWARD_LIST: # initialize drive mode list global_vars.DRIVE_MODE.append(forward['DRIVE_MODE']) def tg_drive_mode(bot: telegram.Bot, update: telegram.Update): """ if update.message: message: telegram.Message = update.message else: message: telegram.Message = update.edited_message """ message: telegram.Message = update.effective_message edited = (bool(getattr(update, "edited_message", None)) or bool(getattr(update, "edited_channel_post", None)))
import json import logging from pathlib import Path import global_vars import telegram from telegram.ext import Filters, CommandHandler from main.utils import get_plugin_priority logger = logging.getLogger("CTB." + __name__) logger.debug(__name__ + " loading") global_vars.create_variable('admin_list', {'QQ': [], 'TG': []}) def load_data(): logger.debug("Begin loading admin list") json_file = Path('./plugins/conf/' + __name__ + '.json') if json_file.is_file(): global_vars.admin_list = json.load(open('./plugins/conf/' + __name__ + '.json', 'r')) logger.debug("Admin list loaded") def save_data(): json.dump(global_vars.admin_list, open('./plugins/conf/' + __name__ + '.json', 'w'), ensure_ascii=False, indent=4) load_data()
import json import logging from pathlib import Path import global_vars import telegram from telegram.ext import MessageHandler, Filters, ConversationHandler, CommandHandler from main.utils import get_plugin_priority logger = logging.getLogger("CTB." + __name__) logger.debug(__name__ + " loading") global_vars.create_variable('filter_list', { 'keywords': [], 'channels': [], 'user': [] }) def load_data(): logger.debug("Begin loading water meter config") json_file = Path('./plugins/conf/' + __name__ + '.json') if json_file.is_file(): global_vars.filter_list = json.load( open('./plugins/conf/' + __name__ + '.json', 'r')) logger.debug("Water meter config loaded") def save_data(): json.dump(global_vars.filter_list,
import logging import global_vars import telegram from bot_constant import FORWARD_LIST from main.command import command_listener from main.utils import send_both_side logger = logging.getLogger("CTB." + __name__) logger.debug(__name__ + " loading") global_vars.create_variable('group_members', [[]] * len(FORWARD_LIST)) def reload_qq_namelist(forward_index: int): global_vars.group_members[forward_index] = global_vars.qq_bot.get_group_member_list(group_id=FORWARD_LIST[forward_index]['QQ']) def reload_all_qq_namelist(): for i in range(len(FORWARD_LIST)): reload_qq_namelist(i) # register the two functions to global_vars for accessing from other modules global_vars.create_variable('reload_qq_namelist', reload_qq_namelist) global_vars.create_variable('reload_all_qq_namelist', reload_all_qq_namelist) @command_listener('update namelist', 'name', description='update namelist for current group') def update_namelist(forward_index: int,
import logging import global_vars import telegram from telegram import InlineKeyboardButton, InlineKeyboardMarkup from telegram.ext import CallbackQueryHandler from main.utils import get_plugin_priority, get_full_user_name # rely on _000_admins logger = logging.getLogger("CTBPlugin." + __name__) logger.debug(__name__ + " loading") global_vars.create_variable('group_requests', {}) # pending admissions @global_vars.qq_bot.on_request('group', group=get_plugin_priority(__name__)) def event_group_request(context): """ handle qq group add/invite requests with reply markup :param context: post_type string "request" 上报类型 request_type string "group" 请求类型 sub_type string "add"、"invite" 请求子类型,分别表示加群请求、邀请登录号入群 group_id number - 群号 user_id number - 发送请求的 QQ 号 message string - 验证信息 flag string - 请求 flag,在调用处理请求的 API 时需要传入 :return: """
from bot_constant import FORWARD_LIST import global_vars from utils import send_both_side from command import command_listener import telegram import logging logger = logging.getLogger("CTBPlugin." + __name__) logger.debug(__name__ + " loading") global_vars.create_variable('group_members', [[]] * len(FORWARD_LIST)) def reload_all_qq_namelist(): for i in range(len(FORWARD_LIST)): global_vars.group_members[ i] = global_vars.qq_bot.get_group_member_list( group_id=FORWARD_LIST[i]['QQ']) @command_listener('update namelist', 'name', description='update namelist for current group') def update_namelist(forward_index: int, tg_group_id: int = None, tg_user: telegram.User = None, tg_message_id: int = None, tg_reply_to: telegram.Message = None, qq_group_id: int = None, qq_discuss_id: int = None, qq_user: int = None):
# you can add more error handel below here logger.error("""Can't update namelist, coolq error retcode=102 You may need to check cqhttp's logs in app/io.github.richardchien.coolqhttpapi/log/xxxxx.log For more information: https://github.com/jqqqqqqqqqq/coolq-telegram-bot/issues/48""" ) else: logger.info('[%s]Successful update qq namelist' % gid_qq) def reload_all_qq_namelist(): for i in range(len(FORWARD_LIST)): reload_qq_namelist(i) # register the two functions to global_vars for accessing from other modules global_vars.create_variable('reload_qq_namelist', reload_qq_namelist) global_vars.create_variable('reload_all_qq_namelist', reload_all_qq_namelist) @command_listener('update namelist', 'name', description='update namelist for current group') def update_namelist(forward_index: int, tg_group_id: int = None, tg_user: telegram.User = None, tg_message_id: int = None, tg_reply_to: telegram.Message = None, qq_group_id: int = None, qq_discuss_id: int = None, qq_user: int = None):