# @bot.message_handler(commands=['magnet']) def search_magnet(message): all_part_magnet = part_magnet.search(message.text) if all_part_magnet is None: raise NoResultError() with SearchDB('main.db') as d: res = d.magnet(all_part_magnet.group().lower()) res_file = query_response(res, exc_type=MagnetNoReuslt, query=all_part_magnet.group()) bot.send_document(message.chat.id, res_file, caption='执行成功,并返回查询结果') @bot.message_handler(commands=['sql'], func=check(100)) def exec_sql(message): try: with db.Handler('main.db') as d: sql = message.text[5:] d.cursor.execute(sql) res = d.cursor.fetchall() except Exception as e: logger.warning(e) traceback.print_exc() bot.send_message(message.chat.id, 'SQL 执行遇到失败,数据库已回滚') else: res_file = query_response(res, exc_type=NoResultError, query=sql) bot.send_document(message.chat.id, res_file, caption='执行成功,并返回查询结果')
def __str__(self): return '种子管理器' + self.magnet def __repr__(self): return "种子管理器" + self.magnet def __eq__(self, other): if not isinstance(other, QbitMessage): return False if (self.cid, self.mid) == (other.cid, other.mid): return True return False @bot.message_handler(content_types=['document'], func=check(50)) def qbit_download(message: types.Message): doc = message.document # type:types.Document if not message.document.file_name: return if not message.document.file_name.lower().endswith('.ti'): return # 如果文件大于50MiB,就拒绝下载 50*1024*1024=52428800 if doc.file_size > 52428800: return bot.send_message(message.chat.id, "文件过大,无法下载") file_info = bot.get_file(doc.file_id) try: response = requests.get( 'https://api.telegram.org/file/bot{0}/{1}'.format( bot.token, file_info.file_path))
def get_pinned_message(message): try: limit = int(message.text.split()[1]) if limit > 100: limit = 100 except (ValueError, IndexError): limit = 10 with MessageDB("messages.db") as d: msgs = d.get_pinned_message(message.chat.id, limit) pinned_msgs = [] for msg in msgs: # type: date = (dt.datetime.fromtimestamp(msg[5]) + dt.timedelta(**const.TINEZONE_DELTA) ).strftime("%m{}%d{} %H:%M").format('月', '日') link = f'https://t.me/c/{str(msg[1])[4:]}/{msg[9]}' content = msg[4] if len(msg[4]) < 10 else f'{msg[4][:3]}...{msg[4][-3:]}' msg_temp = '该消息由{user}置顶于{date}:<a href="{link}">{content}</a>'.format( user=msg[6], date=date, link=link, content=content ) pinned_msgs.append(msg_temp) text = "\n".join(pinned_msgs) bot.send_message(message.chat.id, text, parse_mode="HTML") @bot.message_handler(commands=['minfo'], func=check(100)) def msg_info(message): f = StringIO() f.name = "Response.txt" json.dump(message, f, default=lambda obj: obj.__dict__) f.seek(0) bot.send_document(const.GOD, f)
reply_markup=btns, disable_notification=True) with MessagesDB('main.db', none_stop=True) as msgdb: msgdb.save_message(self.post.url_path, msg.chat.id, msg.message_id) bot.forward_message(const.GOD, msg.chat.id, msg.message_id) page_queue = Queue(1) running = False class RunningCrawlerError(Exception): pass @bot.message_handler(commands=('get', ), func=check(80)) def check_site_update(message): try: if running: raise RunningCrawlerError page = int(message.text[5:]) page_queue.put_nowait((page, message.chat.id, message.from_user.id)) except (Full, RunningCrawlerError): logger.info("被正在进行的任务阻塞") bot.send_message(message.chat.id, '现在还在忙呢,别着急啊') return except Exception as e: logger.warning(e) return bot.send_message(message.chat.id, 'GETTING...')
import traceback from config import const, autobackup, bot from config.admins import check @bot.message_handler(commands=['backupnow'], func=check(100)) def backup_database_now(message): autobackup.event.set() bot.send_message(message.chat.id, "启动立即备份数据库") @bot.message_handler(commands=['handler'], func=check(100)) def check_handler(message): bot.send_message(message.chat.id, str(bot.message_handlers)) @bot.message_handler(commands=['group'], func=lambda msg: msg.chat.type == "private" and check(80)(msg)) def send_group_message(message): bot.send_message(const.GROUP, message.text[7:]) @bot.message_handler(commands=['exec'], func=check(100)) def remote_exec(message): command = message.text[6:] try: exec(command) except Exception as exc: bot.send_message(message.chat.id, traceback.format_exc(), reply_to_message_id=message.message_id) else: bot.send_message(message.chat.id, '执行成功', reply_to_message_id=message.message_id)