def update_dirty_msg(channel_id, msg_id, update_mode=1): lock.acquire() dirty_list = helper_global.value("dirty_list", []) if not (channel_id, msg_id, update_mode) in dirty_list: dirty_list.append((channel_id, msg_id, update_mode)) helper_global.assign("dirty_list", dirty_list) lock.release()
def init_database(filepath): conn = sqlite3.connect(filepath, check_same_thread=False) helper_global.assign("database", conn) cursor = conn.cursor() cursor.execute(""" CREATE TABLE config ( chat_id text PRIMARY KEY, lang text, mode int, recent int, username text, admin_id text ); """) cursor.execute(""" CREATE TABLE reflect ( chat_id text, msg_id text, comment_id text, PRIMARY KEY (chat_id, msg_id) ); """) cursor.execute(""" CREATE TABLE record ( chat_id text, msg_id text, username text, name text, type text, content text, media_id text, date text ); """) conn.commit()
def register(bot, update): logger = Logger.logger from_id = update.message.from_user.id chat_id = update.message.chat_id logger.msg({"user_id": from_id}, tag="register", log_level=80) helper_global.assign(str(from_id) + "_status", "0,1") helper_global.send_intro_template(bot, chat_id, helper_const.DEFAULT_LANG, "register", "register_cmd_text")
def private_msg(bot, update): message = update.message # print(message) chat_id = message.chat_id args = helper_global.value(str(chat_id) + "_status", "0,0") params = args.split(",") channel_id = int(params[0]) msg_id = int(params[1]) anonymous = False if len(params) >= 3 and params[2] == '3': anonymous = True if channel_id == 0: if msg_id == 1: check_channel_message(bot, message) return add_record(channel_id, msg_id, message, anonymous) # Update Dirty List lock.acquire() dirty_list = helper_global.value("dirty_list", []) if not (channel_id, msg_id) in dirty_list: dirty_list.append((channel_id, msg_id)) helper_global.assign("dirty_list", dirty_list) lock.release() bot.send_message(chat_id=chat_id, text=helper_global.value("comment_success", "Success!"))
def check_channel_message(bot, message): chat_id = message.chat_id if not message.forward_from_chat: bot.send_message(chat_id=chat_id, text=helper_global.value("register_cmd_invalid", "")) return chat_type = message.forward_from_chat.type if not chat_type == "channel": bot.send_message(chat_id=chat_id, text=helper_global.value("register_cmd_invalid", "")) return channel_username = message.forward_from_chat.username channel_id = message.forward_from_chat.id try: helper_database.add_channel_config(channel_id, 'zh-CN', 1, 10, channel_username, chat_id) except: helper_global.assign(str(chat_id) + "_status", "0,0") bot.send_message(chat_id=chat_id, text=helper_global.value("register_cmd_failed", "")) return helper_global.assign(str(chat_id) + "_status", "0,0") bot.send_message(chat_id=chat_id, text=helper_global.value("register_cmd_success", ""))
def register(bot, update): from_id = update.message.from_user.id chat_id = update.message.chat_id helper_global.assign(str(from_id) + "_status", "0,1") bot.send_message(chat_id=chat_id, text=helper_global.value("register_cmd_text", "", lang="all"))
def check_channel_message(bot, message): chat_id = message.chat_id if not message.forward_from_chat: bot.send_message(chat_id=chat_id, text=helper_global.value("register_cmd_invalid", "", "all")) return chat_type = message.forward_from_chat.type if not chat_type == "channel": bot.send_message(chat_id=chat_id, text=helper_global.value("register_cmd_invalid", "", "all")) return channel_username = message.forward_from_chat.username channel_id = message.forward_from_chat.id user_id = message.from_user.id bot_id = int(helper_const.BOT_TOKEN.split(":")[0]) try: chat_members = bot.get_chat_administrators(chat_id=channel_id).result() chat_member_ids = [member.user.id for member in chat_members] if not user_id in chat_member_ids: bot.send_message(chat_id=chat_id, text=helper_global.value("register_cmd_not_admin", "", "all")) return for member in chat_members: if member.user.id == bot_id: post_permission = member.can_post_messages if member.can_post_messages else False edit_permission = member.can_edit_messages if member.can_edit_messages else False delete_permission = member.can_delete_messages if member.can_delete_messages else False if not post_permission or not edit_permission or not delete_permission: bot.send_message(chat_id=chat_id, text=helper_global.value( "register_cmd_no_permission", "", "all")) return break except: bot.send_message(chat_id=chat_id, text=helper_global.value("register_cmd_no_info", "", "all")) return try: helper_database.add_channel_config(channel_id, helper_const.DEFAULT_LANG, 1, 10, channel_username, chat_id, 1) except: helper_global.assign(str(chat_id) + "_status", "0,0") bot.send_message(chat_id=chat_id, text=helper_global.value("register_cmd_failed", "", "all")) return helper_global.assign(str(chat_id) + "_status", "0,0") bot.send_message(chat_id=chat_id, text=helper_global.value("register_cmd_success", "", "all"))
def start(bot, update, args): if args is None or len(args) == 0: text = helper_global.value("start_cmd_text", "") bot.send_message(chat_id=update.message.chat_id, text=text) return params = args[0].split("_") channel_id = int(params[1]) msg_id = int(params[2]) chat_id = update.message.chat_id if chat_id < 0: return if helper_database.check_ban(channel_id, chat_id): bot.send_message(chat_id=update.message.chat_id, text=helper_global.value("banned_prompt", "You are banned.")) return config = helper_database.get_channel_config(channel_id) if config is None: return recent, username = config[3], config[4] if params[0] == "add": helper_global.assign(str(chat_id) + "_status", params[1] + "," + params[2]) if username is not None: bot.send_message(chat_id=update.message.chat_id, text=helper_global.value("start_comment_mode", "") + "\n" + helper_global.value("target_message", "") + "https://t.me/%s/%d" % (username, msg_id)) else: bot.send_message(chat_id=update.message.chat_id, text=helper_global.value("start_comment_mode", "")) elif params[0] == "show": offset = 0 channel_username = config[4] records = helper_database.get_recent_records(channel_id, msg_id, recent, offset) # Prepare Keyboard msg_buttons = helper_global.records_to_buttons(records, channel_id, msg_id) motd_keyboard = msg_buttons + [[ InlineKeyboardButton( helper_global.value("prev_page", "Prev Page"), callback_data="msg,%d,%d,%d,%d,%d" % (channel_id, msg_id, recent, offset + 1, chat_id) ), InlineKeyboardButton( helper_global.value("next_page", "Next Page"), callback_data="msg,%d,%d,%d,%d,%d" % (channel_id, msg_id, recent, offset - 1, chat_id) ) ]] motd_markup = InlineKeyboardMarkup(motd_keyboard) prompt_text = helper_global.value("comment_header", "") if channel_username is not None and len(channel_username) > 0: prompt_text = "https://t.me/%s/%a\n" % (channel_username, msg_id) + prompt_text bot.send_message( chat_id=update.message.chat_id, text=prompt_text, parse_mode=telegram.ParseMode.HTML, reply_markup=motd_markup )
def update_dirty_list(): lock.acquire() dirty_list = helper_global.value("dirty_list", []) bot = helper_global.value("bot", None) for item in dirty_list: channel_id, msg_id, update_mode = item threading.Thread(target=update_comments, args=(bot, channel_id, msg_id, update_mode)).start() helper_global.assign("dirty_list", []) lock.release()
def add_comment(bot, chat_id, config, message_id, media_group_id=None): logger = Logger.logger logger.msg( { "channel_id": chat_id, "msg_id": message_id, "action": "normal comment" }, tag="channel", log_level=80) channel_lang = config[1] recent = config[3] if helper_database.check_reflect(chat_id, message_id): return # Avoid duplicated comment for media group if media_group_id: last_media_group = helper_global.value( str(chat_id) + '_last_media_group', '0') # print(last_media_group) if last_media_group == media_group_id: return helper_global.assign( str(chat_id) + '_last_media_group', media_group_id) # Prepare Keyboard motd_keyboard = [[ InlineKeyboardButton( helper_global.value("add_comment", "Add Comment", lang=channel_lang), url="http://telegram.me/%s?start=add_%d_%d" % (helper_global.value('bot_username', ''), chat_id, message_id)), InlineKeyboardButton( helper_global.value("show_all_comments", "Show All", lang=channel_lang), url="http://telegram.me/%s?start=show_%s_%d" % (helper_global.value('bot_username', ''), chat_id, message_id)) ]] motd_markup = InlineKeyboardMarkup(motd_keyboard) records = helper_database.get_recent_records(chat_id, message_id, recent) comment_message = bot.send_photo( photo=open(sys.path[0] + "/default-comment.jpg", "rb"), caption=helper_global.records_to_str(records, channel_lang), chat_id=chat_id, reply_to_message_id=message_id, reply_markup=motd_markup, parse_mode=telegram.ParseMode.HTML).result() helper_database.add_reflect(chat_id, message_id, comment_message.message_id)
def private_msg(bot, update): message = update.edited_message if update.edited_message else update.message # print(message) chat_id = message.chat_id args = helper_global.value(str(chat_id) + "_status", "0,0") params = args.split(",") channel_id = int(params[0]) msg_id = int(params[1]) if channel_id == 0: if msg_id == 1: check_channel_message(bot, message) return # Check comment message comment_exist = helper_database.check_reflect(channel_id, msg_id) if not comment_exist: config = helper_database.get_channel_config(channel_id) if config is None: return recent = config[3] records = helper_database.get_recent_records(channel_id, msg_id, recent) comment_message = bot.send_message( chat_id=channel_id, text=helper_global.records_to_str(records), reply_to_message_id=msg_id, parse_mode=telegram.ParseMode.HTML).result() helper_database.add_reflect(channel_id, msg_id, comment_message.message_id) bot.edit_message_reply_markup(chat_id=channel_id, message_id=msg_id, reply_markup=None) result = add_record(channel_id, msg_id, message) # Update Dirty List lock.acquire() dirty_list = helper_global.value("dirty_list", []) if not (channel_id, msg_id) in dirty_list: dirty_list.append((channel_id, msg_id)) helper_global.assign("dirty_list", dirty_list) lock.release() if result == 0: bot.send_message(chat_id=chat_id, text=helper_global.value("comment_success", "Success!")) elif result == 1: bot.send_message(chat_id=chat_id, text=helper_global.value("comment_edit_success", "Success!"))
def cancel(bot, update): chat_id = update.message.chat_id channel_id = helper_global.value(str(chat_id) + "_status", "0,0").split(",")[0] config = helper_database.get_channel_config(channel_id) if config is None: lang = "all" else: lang = config[1] helper_global.assign(str(chat_id) + "_status", "0,0") help_text = helper_global.value("stop_comment_mode", "", lang=lang) bot.send_message(chat_id=chat_id, text=help_text)
def cancel(bot, update): logger = Logger.logger chat_id = update.message.chat_id channel_id = helper_global.value(str(chat_id) + "_status", "0,0").split(",")[0] logger.msg({ "channel_id": channel_id, "user_id": chat_id }, tag="cancel", log_level=80) config = helper_database.get_channel_config(channel_id) if config is None: lang = "all" else: lang = config[1] helper_global.assign(str(chat_id) + "_status", "0,0") help_text = helper_global.value("stop_comment_mode", "", lang=lang) bot.send_message(chat_id=chat_id, text=help_text)
def start(bot, update, args): if args is None or len(args) == 0: text = helper_global.value("start_cmd_text", "") bot.send_message(chat_id=update.message.chat_id, text=text) return params = args[0].split("_") channel_id = int(params[1]) msg_id = int(params[2]) chat_id = update.message.chat_id if params[0] == "add": helper_global.assign( str(chat_id) + "_status", params[1] + "," + params[2]) bot.send_message(chat_id=update.message.chat_id, text=helper_global.value("start_comment_mode", "")) elif params[0] == 'addanonymous': helper_global.assign( str(chat_id) + "_status", params[1] + "," + params[2] + "," + "3") bot.send_message(chat_id=update.message.chat_id, text=helper_global.value("start_comment_mode", "")) elif params[0] == "show": offset = 0 config = helper_database.get_channel_config(channel_id) if config is None: return recent = config[3] # Prepare Keyboard motd_keyboard = [[ InlineKeyboardButton( helper_global.value("prev_page", "Prev Page"), callback_data="msg,%d,%d,%d,%d,%d" % (channel_id, msg_id, recent, offset + 1, chat_id)), InlineKeyboardButton( helper_global.value("next_page", "Next Page"), callback_data="msg,%d,%d,%d,%d,%d" % (channel_id, msg_id, recent, offset - 1, chat_id)) ]] motd_markup = InlineKeyboardMarkup(motd_keyboard) records = helper_database.get_recent_records(channel_id, msg_id, recent, offset) bot.send_message(chat_id=update.message.chat_id, text=helper_global.records_to_str(records), parse_mode=telegram.ParseMode.HTML, reply_markup=motd_markup)
def add_comment(bot, chat_id, config, message_id, media_group_id=None): channel_lang = config[1] recent = config[3] # Avoid duplicated comment for media group if media_group_id: last_media_group = helper_global.value( str(chat_id) + '_last_media_group', '0') print(last_media_group) if last_media_group == media_group_id: return helper_global.assign( str(chat_id) + '_last_media_group', media_group_id) # Prepare Keyboard motd_keyboard = [[ InlineKeyboardButton( helper_global.value("add_comment", "Add Comment", lang=channel_lang), url="http://telegram.me/%s?start=add_%d_%d" % (helper_global.value('bot_username', ''), chat_id, message_id)), InlineKeyboardButton( helper_global.value("show_all_comments", "Show All", lang=channel_lang), url="http://telegram.me/%s?start=show_%s_%d" % (helper_global.value('bot_username', ''), chat_id, message_id)) ]] motd_markup = InlineKeyboardMarkup(motd_keyboard) records = helper_database.get_recent_records(chat_id, message_id, recent) comment_message = bot.send_message( chat_id=chat_id, text=helper_global.records_to_str(records, channel_lang), reply_to_message_id=message_id, reply_markup=motd_markup, parse_mode=telegram.ParseMode.HTML).result() helper_database.add_reflect(chat_id, message_id, comment_message.message_id)
"请选择一个项目以进行配置\nmode: bot 的工作模式\nrecent: 在频道中显示的评论数量\nnotify: 新评论提醒", "option_choose_mode_value": "本 bot 有三种工作模式\n模式 0: 手动模式。当频道中新增消息时,bot 不会自动创建评论消息。当频道管理员使用 /comment 回复需要评论的原始消息时,bot 才会创建评论消息。如果 /comment 命令不起作用,请检查是否授予 bot 删除消息的权限。\n模式 1: 自动模式。当频道中新增消息时,bot 自动创建评论消息。该模式能保持原始频道消息不被修改。\n模式 2: 自动模式(beta)。当频道中新增消息时,bot 会尝试编辑原消息,显示添加评论按钮。如果编辑失败则直接创建评论消息。该模式能尽可能减少频道里的评论区数量。\n请选择您所需要的工作模式:", "option_choose_recent_value": "在频道中仅显示最近的若干条消息。请选择频道显示的最近条目数量:", "option_choose_notify_value": "您可以选择当频道收到新评论时是否开启提醒功能。0代表关闭,1代表开启。", "option_update_success": "配置更新成功", "option_update_failed": "配置更新失败", "clean_cmd_start": "正在进行检查,请稍候...", "clean_cmd_deleted": "删除记录成功", "clean_cmd_set": "设置成功", "fwd_source": "消息来源: ", "prev_msg": "上一条消息", "next_msg": "下一条消息", "no_message_detail": "未找到消息", "back_to_msg_list": "返回消息列表", "msg_from": "消息来源:", "delete_msg": "删除消息", "delete_success": "消息删除成功", "ban_user": "******", "unban_user": "******", "user_banned": "已封禁该用户", "user_banned_failed": "封禁失败,该用户可能已被封禁", "user_unbanned": "用户已解封", "banned_prompt": "频道管理员不允许你进行评论操作", "new_comment_message": "您收到了新的评论", "target_message": "目标消息:" } for item, value in helper_string.items(): helper_global.assign(item, value)
def cancel(bot, update): chat_id = update.message.chat_id helper_global.assign(str(chat_id) + "_status", "0,0") help_text = helper_global.value("stop_comment_mode", "") bot.send_message(chat_id=chat_id, text=help_text)
def check_channel_message(bot, message): logger = Logger.logger chat_id = message.chat_id if not message.forward_from_chat: helper_global.send_intro_template(bot, chat_id, helper_const.DEFAULT_LANG, "register_invalid", "register_cmd_invalid") return chat_type = message.forward_from_chat.type if not chat_type == "channel": helper_global.send_intro_template(bot, chat_id, helper_const.DEFAULT_LANG, "register_invalid", "register_cmd_invalid") return channel_username = message.forward_from_chat.username channel_id = message.forward_from_chat.id user_id = message.from_user.id logger.msg( { "user_id": chat_id, "channel_id": channel_id, "action": "check channel" }, tag="private", log_level=90) bot_id = int(helper_const.BOT_TOKEN.split(":")[0]) try: chat_members = bot.get_chat_administrators(chat_id=channel_id).result() chat_member_ids = [member.user.id for member in chat_members] if not user_id in chat_member_ids: helper_global.send_intro_template(bot, chat_id, helper_const.DEFAULT_LANG, "register_not_admin", "register_cmd_not_admin") return for member in chat_members: if member.user.id == bot_id: post_permission = member.can_post_messages if member.can_post_messages else False edit_permission = member.can_edit_messages if member.can_edit_messages else False delete_permission = member.can_delete_messages if member.can_delete_messages else False if not post_permission or not edit_permission or not delete_permission: helper_global.send_intro_template( bot, chat_id, helper_const.DEFAULT_LANG, "register_no_permission", "register_cmd_no_permission") return break except: helper_global.send_intro_template(bot, chat_id, helper_const.DEFAULT_LANG, "register_no_info", "register_cmd_no_info") return try: helper_database.add_channel_config(channel_id, helper_const.DEFAULT_LANG, 1, 10, channel_username, chat_id, 1, 1) except: helper_global.assign(str(chat_id) + "_status", "0,0") helper_global.send_intro_template(bot, chat_id, helper_const.DEFAULT_LANG, "register_failed", "register_cmd_failed") return helper_global.assign(str(chat_id) + "_status", "0,0") helper_global.send_intro_template(bot, chat_id, helper_const.DEFAULT_LANG, "register_success", "register_cmd_success")
import telegram.bot from telegram.ext import messagequeue as mq from telegram.ext import Updater, CommandHandler from telegram.utils.request import Request from ninesix import Logger # config logger logger = Logger("channel_helper", log_level=0, preserve=True) # import constant, strings, database helper_const = importlib.import_module('helper_const') helper_string = importlib.import_module('helper_string') helper_database = importlib.import_module('helper_database') # load admin list helper_global.assign('admin_list', []) def reload_admin_list(): global admin_list admin_list = helper_const.BOT_OWNER helper_global.assign('admin_list', admin_list) reload_admin_list() logger.msg("Admin List: " + str(helper_global.value('admin_list', [])), "main", log_level=100) # config bot class MQBot(telegram.bot.Bot): '''A subclass of Bot which delegates send method handling to MQ''' def __init__(self, *args, is_queued_def=True, mqueue=None, **kwargs):
text=helper_global.value("comment_edit_success", "Success!", lang=channel_lang)) def update_dirty_msg(channel_id, msg_id, update_mode=1): lock.acquire() dirty_list = helper_global.value("dirty_list", []) if not (channel_id, msg_id, update_mode) in dirty_list: dirty_list.append((channel_id, msg_id, update_mode)) helper_global.assign("dirty_list", dirty_list) lock.release() def set_interval(func, sec): def func_wrapper(): set_interval(func, sec) func() t = threading.Timer(sec, func_wrapper) t.start() return t refresh_status = helper_global.value("refresh_status", False) if not refresh_status: set_interval(update_dirty_list, helper_const.MIN_REFRESH_INTERVAL) helper_global.assign("refresh_status", True) lock = threading.Lock() _handler = MessageHandler(Filters.private, private_msg, edited_updates=True)
def reload_admin_list(): global admin_list admin_list = helper_const.BOT_OWNER helper_global.assign('admin_list', admin_list)
def private_msg(bot, update): message = update.edited_message if update.edited_message else update.message # print(message) chat_id = message.chat_id args = helper_global.value(str(chat_id) + "_status", "0,0") params = args.split(",") channel_id = int(params[0]) msg_id = int(params[1]) if channel_id == 0: if msg_id == 1: check_channel_message(bot, message) return # Check comment message comment_exist = helper_database.check_reflect(channel_id, msg_id) config = helper_database.get_channel_config(channel_id) if config is None: return channel_lang = config[1] recent, username, admin_id, notify = config[3], config[4], config[ 5], config[6] if not comment_exist: records = helper_database.get_recent_records(channel_id, msg_id, recent) comment_message = bot.send_message( chat_id=channel_id, text=helper_global.records_to_str(records, channel_lang), reply_to_message_id=msg_id, parse_mode=telegram.ParseMode.HTML).result() helper_database.add_reflect(channel_id, msg_id, comment_message.message_id) bot.edit_message_reply_markup(chat_id=channel_id, message_id=msg_id, reply_markup=None) result = add_record(channel_id, msg_id, message) # Update Dirty List lock.acquire() dirty_list = helper_global.value("dirty_list", []) if not (channel_id, msg_id) in dirty_list: dirty_list.append((channel_id, msg_id)) if notify == 1 and not int(chat_id) == int(admin_id): if username is not None: bot.send_message( chat_id=admin_id, text=helper_global.value("new_comment_message", "You have a new comment message.", lang=channel_lang) + "\n" + helper_global.value( "target_message", "", lang=channel_lang) + "https://t.me/%s/%d" % (username, msg_id)) else: bot.send_message(chat_id=admin_id, text=helper_global.value( "new_comment_message", "You have a new comment message.", lang=channel_lang)) helper_global.assign("dirty_list", dirty_list) lock.release() if result == 0: bot.send_message(chat_id=chat_id, text=helper_global.value("comment_success", "Success!", lang=channel_lang)) elif result == 1: bot.send_message(chat_id=chat_id, text=helper_global.value("comment_edit_success", "Success!", lang=channel_lang))
#!/usr/bin/env python # -*- coding: utf-8 -*- """ Channel Helper Bot """ """ helper_string.py """ """ Copyright 2018, Jogle Lew """ import json import helper_const import helper_global lang_config = helper_const.LANG_LIST for lang_code in lang_config: with open("i18n/%s.json" % lang_code, "r") as f: lang_dict = json.load(f) for item, value in lang_dict.items(): helper_global.assign(item, value, lang=lang_code)
def start(bot, update, args): logger = Logger.logger if args is None or len(args) == 0: chat_id = update.message.chat_id logger.msg({"user_id": chat_id}, tag="start", log_level=80) helper_global.send_intro_template(bot, chat_id, helper_const.DEFAULT_LANG, "start", "start_cmd_text") return params = args[0].split("_") channel_id = int(params[1]) msg_id = int(params[2]) chat_id = update.message.chat_id logger.msg({"user_id": chat_id, "args": args}, tag="start", log_level=90) if chat_id < 0: return config = helper_database.get_channel_config(channel_id) if config is None: return channel_lang = config[1] recent, username = config[3], config[4] if helper_database.check_ban(channel_id, chat_id): bot.send_message(chat_id=update.message.chat_id, text=helper_global.value("banned_prompt", "You are banned.", lang=channel_lang)) return if params[0] == "add": helper_global.assign( str(chat_id) + "_status", params[1] + "," + params[2]) motd_keyboard = [[ InlineKeyboardButton( helper_global.value("reply_to", "Reply", lang=channel_lang) + "...", switch_inline_query_current_chat=" ") ]] motd_markup = InlineKeyboardMarkup(motd_keyboard) comment_id = helper_database.get_comment_id(channel_id, msg_id) suffix = "" if username is not None: if comment_id is not None: suffix = "\n" + helper_global.value( "target_comment", "", lang=channel_lang ) + "https://t.me/%s/%d" % (username, comment_id) bot.send_message( chat_id=update.message.chat_id, text=helper_global.value( "start_comment_mode", "", lang=channel_lang) + "\n" + helper_global.value("target_message", "", lang=channel_lang) + "https://t.me/%s/%d" % (username, msg_id) + suffix, reply_markup=motd_markup) else: link_id = abs(channel_id) % 10000000000 if comment_id is not None: suffix = "\n" + helper_global.value( "target_comment", "", lang=channel_lang ) + "https://t.me/%d/%d" % (link_id, comment_id) bot.send_message( chat_id=update.message.chat_id, text=helper_global.value( "start_comment_mode", "", lang=channel_lang) + "\n" + helper_global.value("target_message", "", lang=channel_lang) + "https://t.me/c/%d/%d" % (link_id, msg_id) + suffix, reply_markup=motd_markup) elif params[0] == "show": offset = 0 channel_username = config[4] records = helper_database.get_recent_records(channel_id, msg_id, recent, offset) # Prepare Keyboard msg_buttons = helper_global.records_to_buttons(records, channel_id, msg_id) motd_keyboard = msg_buttons + [[ InlineKeyboardButton( helper_global.value( "prev_page", "Prev Page", lang=channel_lang), callback_data="msg,%d,%d,%d,%d,%d" % (channel_id, msg_id, recent, offset + 1, chat_id)), InlineKeyboardButton( helper_global.value( "next_page", "Next Page", lang=channel_lang), callback_data="msg,%d,%d,%d,%d,%d" % (channel_id, msg_id, recent, offset - 1, chat_id)) ]] motd_markup = InlineKeyboardMarkup(motd_keyboard) prompt_text = helper_global.value("comment_header", "", lang=channel_lang) if channel_username is not None and len(channel_username) > 0: prompt_text = "https://t.me/%s/%a\n" % (channel_username, msg_id) + prompt_text bot.send_message(chat_id=update.message.chat_id, text=prompt_text, parse_mode=telegram.ParseMode.HTML, reply_markup=motd_markup)
def get_channel_info_by_user(user_id): script = "SELECT chat_id, username FROM config WHERE admin_id = ?" params = [str(user_id)] result = list(execute(script, params)) return result def ban_user(channel_id, user_id, name): script = "INSERT INTO blacklist VALUES (?, ?, ?)" params = [str(channel_id), str(user_id), name] result = list(execute(script, params)) return result def check_ban(channel_id, user_id): script = "SELECT * FROM blacklist WHERE chat_id = ? AND user_id = ?" params = [str(channel_id), str(user_id)] result = list(execute(script, params)) return len(result) > 0 lock = Lock() filepath = os.path.join(helper_const.DATABASE_DIR, "data.db") if not os.path.exists(filepath): init_database(filepath) else: conn = sqlite3.connect(filepath, check_same_thread=False) helper_global.assign("database", conn)
def init_database(filepath): conn = sqlite3.connect(filepath, check_same_thread=False) helper_global.assign("database", conn) cursor = conn.cursor() cursor.execute(""" CREATE TABLE config ( chat_id text PRIMARY KEY, lang text, mode int, recent int, username text, admin_id text, notify int, button int ); """) cursor.execute(""" CREATE TABLE reflect ( chat_id text, msg_id text, comment_id text, PRIMARY KEY (chat_id, msg_id) ); """) cursor.execute(""" CREATE TABLE button ( chat_id text, msg_id text, options text, PRIMARY KEY (chat_id, msg_id) ); """) cursor.execute(""" CREATE TABLE record ( chat_id text, msg_id text, username text, name text, type text, content text, media_id text, date text, user_id text, ori_msg_id text, replyTo text ); """) cursor.execute(""" CREATE TABLE blacklist ( chat_id text, user_id text, name text, PRIMARY KEY (chat_id, user_id) ); """) cursor.execute(""" CREATE TABLE reaction ( chat_id text, msg_id text, user_id text, option int, PRIMARY KEY (chat_id, msg_id, user_id) ); """) cursor.execute( "CREATE INDEX record_chat_id_msg_id on record (chat_id, msg_id);") cursor.execute( "CREATE INDEX record_user_id_ori_msg_id on record (user_id, ori_msg_id);" ) conn.commit()