def add_comment(bot, chat_id, message_id, msg_content): # Prepare Keyboard motd_keyboard = [[ InlineKeyboardButton( helper_global.value("add_comment", "Add Comment"), url="http://telegram.me/%s?start=add_%d_%d" % (helper_global.value('bot_username', ''), chat_id, message_id) ), InlineKeyboardButton( helper_global.value("add_anonymous_comment", "Add Anonymous Comment"), url="http://telegram.me/%s?start=addanonymous_%d_%d" % (helper_global.value('bot_username', ''), chat_id, message_id) ), InlineKeyboardButton( helper_global.value("show_all_comments", "Show All"), url="http://telegram.me/%s?start=show_%s_%d" % (helper_global.value('bot_username', ''), chat_id, message_id) ) ]] motd_markup = InlineKeyboardMarkup(motd_keyboard) config = helper_database.get_channel_config(chat_id) if config is None: return recent = config[3] records = helper_database.get_recent_records(chat_id, message_id, recent) comment_message = bot.send_message( chat_id=chat_id, text=msg_content+'\n'+helper_global.records_to_str(records), 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 update_comments(bot, channel_id, msg_id): # update comments in channel comment_id = helper_database.get_comment_id(channel_id, msg_id) config = helper_database.get_channel_config(channel_id) if config is None: return channel_lang = config[1] mode = config[2] recent = config[3] records = helper_database.get_recent_records(channel_id, msg_id, recent) # 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', ''), channel_id, msg_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', ''), channel_id, msg_id)) ]] motd_markup = InlineKeyboardMarkup(motd_keyboard) bot.edit_message_text(text=helper_global.records_to_str( records, channel_lang), chat_id=channel_id, message_id=comment_id, parse_mode=telegram.ParseMode.HTML, reply_markup=motd_markup)
def channel_post_msg(bot, update): message = update.channel_post # print("Channel ID: %d, Channel Username: %s" % (message.chat_id, message.chat.username)) chat_id = message.chat_id message_id = message.message_id config = helper_database.get_channel_config(chat_id) if config is None: return lang, mode, recent = config[1], config[2], config[3] # Auto Comment Mode if mode == 1: add_comment(bot, chat_id, config, message_id, media_group_id=message.media_group_id) elif mode == 2: add_compact_comment(bot, chat_id, config, message_id, message) # Manual Mode elif mode == 0 and message.reply_to_message is not None and message.text == "/comment": message_id = message.reply_to_message.message_id bot.delete_message(chat_id=chat_id, message_id=message.message_id) add_compact_comment(bot, chat_id, config, message_id, message.reply_to_message)
def show_msg(bot, update, origin_message_id, chat_id, args): channel_id = int(args[1]) msg_id = int(args[2]) recent = int(args[3]) offset = int(args[4]) ori_chat_id = int(args[5]) config = helper_database.get_channel_config(channel_id) if config is None: return channel_lang = config[1] channel_username = config[4] if offset < 0: bot.answer_callback_query( callback_query_id=update.callback_query.id, text=helper_global.value("no_next_page", "", lang=channel_lang) ) return 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, ori_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, ori_chat_id) ) ]] motd_markup = InlineKeyboardMarkup(motd_keyboard) if offset > 0 and len(records) == 0: bot.answer_callback_query( callback_query_id=update.callback_query.id, text=helper_global.value("no_prev_page", "", lang=channel_lang) ) return 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=ori_chat_id, text=prompt_text, parse_mode=telegram.ParseMode.HTML, reply_markup=motd_markup ) bot.delete_message( chat_id=chat_id, message_id=origin_message_id )
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 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 reaction(bot, update, chat_id, origin_message_id, user_id, args): channel_id = int(args[1]) msg_id = int(args[2]) like_id = int(args[3]) config = helper_database.get_channel_config(channel_id) if config is None: return channel_lang, mode = config[1], config[2] buttons = helper_database.get_button_options(channel_id, msg_id) helper_database.add_reaction(channel_id, msg_id, user_id, like_id) private_msg.update_dirty_msg(channel_id, msg_id, update_mode=(2 if mode == 3 else 0)) bot.answer_callback_query( callback_query_id=update.callback_query.id, text=helper_global.value("like_recorded", "", lang=channel_lang) % buttons[like_id] )
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 msg_delete(bot, update, chat_id, origin_message_id, args): row_id = int(args[1]) channel_id = int(args[2]) msg_id = int(args[3]) msg_args = ["msg"] + args[2:] config = helper_database.get_channel_config(channel_id) if config is None: return channel_lang = config[1] helper_database.delete_record_by_rowid(row_id) bot.answer_callback_query( callback_query_id=update.callback_query.id, text=helper_global.value("delete_success", "", lang=channel_lang) ) private_msg.update_dirty_msg(channel_id, msg_id) show_msg(bot, update, origin_message_id, chat_id, msg_args)
def user_unban(bot, update, chat_id, origin_message_id, args): channel_id = int(args[1]) user_id = int(args[2]) name = args[3] msg_id = int(args[4]) row_id = int(args[5]) config = helper_database.get_channel_config(channel_id) if config is None: return channel_lang = config[1] helper_database.unban_user(channel_id, user_id, name) bot.answer_callback_query( callback_query_id=update.callback_query.id, text=helper_global.value("user_unbanned", "", lang=channel_lang) ) msg_detail(bot, update, chat_id, origin_message_id, ["msg_detail", channel_id, msg_id, row_id])
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 option_key(bot, update, key, values, lang, chat_id, origin_message_id, args): config = helper_database.get_channel_config(args[1]) if config is None or len(config) == 0: return key2idx = { "lang": 1, "mode": 2, "recent": 3, "notify": 6, "button": 7 } # Prepare Keyboard width = helper_const.LANG_WIDTH motd_keyboard = [[ InlineKeyboardButton( values[idx * width + delta] + (" (*)" if str(values[idx * width + delta]) == str(config[key2idx[key]]) else ""), callback_data="option|%s,%s,%s,%s" % (lang, args[1], key, values[idx * width + delta]) ) for delta in range(width) ] for idx in range(len(values) // width)] + [[ InlineKeyboardButton( values[idx] + (" (*)" if str(values[idx]) == str(config[key2idx[key]]) else ""), callback_data="option|%s,%s,%s,%s" % (lang, args[1], key, values[idx]) ) for idx in range(width * (len(values) // width), len(values))]] + [[ InlineKeyboardButton( helper_global.value("option_back", "", lang=lang), callback_data="option|%s,%s" % (lang, args[1]) ) ]] motd_markup = InlineKeyboardMarkup(motd_keyboard) text = helper_global.value("option_choose_%s_value" % key, "", lang=lang) if key == "button": text = text % (", ".join(helper_database.get_default_button_options(args[1]))) bot.answer_callback_query( callback_query_id=update.callback_query.id ) bot.edit_message_text( chat_id=chat_id, message_id=origin_message_id, text=text, reply_markup=motd_markup )
def add_comment(bot, chat_id, message_id, media_group_id=None): # 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"), 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"), url="http://telegram.me/%s?start=show_%s_%d" % (helper_global.value('bot_username', ''), chat_id, message_id)) ]] motd_markup = InlineKeyboardMarkup(motd_keyboard) config = helper_database.get_channel_config(chat_id) if config is None: return recent = config[3] 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), 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 inline_caps(bot, update): user_id = update.inline_query.from_user.id args = helper_global.value(str(user_id) + "_status", "0,0") params = args.split(",") channel_id = int(params[0]) msg_id = int(params[1]) if channel_id == 0: bot.answer_inline_query(update.inline_query.id, []) return config = helper_database.get_channel_config(channel_id) if config is None: bot.answer_inline_query(update.inline_query.id, []) return channel_lang = config[1] recent = config[3] query = update.inline_query.query if len(query.strip()) == 0: bot.answer_inline_query(update.inline_query.id, [], switch_pm_text=helper_global.value( "reply_prompt", "comment here first", lang=channel_lang), switch_pm_parameter="0", cache_time=0, is_personal=True) return records = helper_database.get_recent_records(channel_id, msg_id, recent, 0) results = [] for idx, record in enumerate(records): name = record[3] content = record[5] msg_user_id = record[8] row_id = int(record[11]) results.append( InlineQueryResultArticle( id=idx, title=name, description=re.sub("<.*?>", "", content).replace('<', '<').replace('>', '>'), input_message_content=InputTextMessageContent( message_text=query.replace('<', '<').replace('>', '>'), parse_mode='HTML'), reply_markup=InlineKeyboardMarkup([[ InlineKeyboardButton(helper_global.value( "reply_to", "Reply to: ", lang=channel_lang) + name, callback_data="notify,%d,%d,%d" % (channel_id, msg_id, row_id)) ]]))) bot.answer_inline_query(update.inline_query.id, results, switch_pm_text=helper_global.value( "reply_prompt", "comment here first", lang=channel_lang), switch_pm_parameter="0", cache_time=0, is_personal=True)
def private_msg(bot, update): logger = Logger.logger message = update.edited_message if update.edited_message else update.message chat_id = message.chat_id args = helper_global.value(str(chat_id) + "_status", "0,0") logger.msg({ "user_id": chat_id, "status": args }, tag="private", log_level=90) 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] logger.msg( { "user_id": chat_id, "channel_id": channel_id, "msg_id": msg_id, "action": "add comment" }, tag="private", log_level=90) # For Auto Mode = 2 if not comment_exist: logger.msg( { "user_id": chat_id, "channel_id": channel_id, "msg_id": msg_id, "action": "add comment area" }, tag="private", log_level=80) comment_message = bot.send_message( chat_id=channel_id, text=helper_global.value("comment_refreshing", "Refreshing...", lang=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 #) update_dirty_msg(channel_id, msg_id, update_mode=0) result = add_record(bot, channel_id, msg_id, message) # Update Dirty List update_dirty_msg(channel_id, msg_id) if notify == 1 and not int(chat_id) == int(admin_id): logger.msg( { "user_id": chat_id, "channel_id": channel_id, "msg_id": msg_id, "admin_id": admin_id, "action": "notify channel owner" }, tag="private", log_level=80) reply_markup = [[ 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', ''), channel_id, msg_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---\n" + message + "\n---\n" + helper_global.value("target_message", "", lang=channel_lang) + "https://t.me/%s/%d" % (username, msg_id), reply_markup=reply_markup) else: link_id = abs(channel_id) % 10000000000 bot.send_message( chat_id=admin_id, text=helper_global.value("new_comment_message", "You have a new comment message.", lang=channel_lang) + "\n---\n" + message + "\n---\n" + helper_global.value("target_message", "", lang=channel_lang) + "https://t.me/c/%d/%d" % (link_id, msg_id), reply_markup=reply_markup) 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))
def add_record(bot, channel_id, msg_id, message): logger = Logger.logger ori_msg_id = message.message_id user = message.from_user username = user.username user_id = user.id name = user.first_name if user.last_name: name += " " + user.last_name date = message.date.strftime("%Y-%m-%d %H:%M:%S") msg_type = "text" msg_content = "" media_id = "" if message.text: msg_type = "text" msg_content = message.text elif message.sticker: msg_type = 'sticker' media_id = message.sticker.file_id if message.sticker.emoji: msg_content = message.sticker.emoji elif message.photo: msg_type = 'photo' media_id = message.photo[-1].file_id if message.caption: msg_content = message.caption elif message.video: msg_type = 'video' media_id = message.video.file_id if message.caption: msg_content = message.caption elif message.document: msg_type = 'document' media_id = message.document.file_id if message.document.file_name: msg_content = message.document.file_name elif message.audio: msg_type = 'audio' media_id = message.audio.file_id if message.audio.title: msg_content = message.audio.title elif message.voice: msg_type = 'voice' media_id = message.voice.file_id msg_content = helper_global.parse_entity(msg_content, message.entities) if message.reply_markup and message.reply_markup.inline_keyboard: keyboard = message.reply_markup.inline_keyboard if len(keyboard) > 0 and len(keyboard[0]) > 0: query = keyboard[0][0] if query.callback_data: args = query.callback_data.split(",") if len(args) == 4 and args[0] == "notify": target_channel_id = int(args[1]) target_msg_id = int(args[2]) target_row_id = args[3] record = helper_database.get_record_by_rowid(target_row_id) if len(record) > 0 and target_channel_id == int( record[0][0]) and target_msg_id == int( record[0][1]): target_name = record[0][3] target_user_id = record[0][8] config = helper_database.get_channel_config(channel_id) if config is None: return channel_lang, channel_username = config[1], config[4] msg_content = "<b>(➤%s) </b> " % target_name.replace( '<', '<').replace('>', '>') + msg_content if channel_username is not None: logger.msg( { "channel_id": channel_id, "msg_id": msg_id, "target_user": target_user_id, "action": "notify reply" }, tag="private", log_level=80) bot.send_message( chat_id=target_user_id, text=helper_global.value( "new_reply_message", "You receive a reply message.", lang=channel_lang) + "\n" + helper_global.value( "target_message", "", lang=channel_lang) + "https://t.me/%s/%d" % (channel_username, target_msg_id)) else: link_id = abs(channel_id) % 10000000000 bot.send_message( chat_id=target_user_id, text=helper_global.value( "new_reply_message", "You receive a reply message.", lang=channel_lang) + "\n" + helper_global.value( "target_message", "", lang=channel_lang) + "https://t.me/c/%d/%d" % (link_id, target_msg_id)) return helper_database.add_record(channel_id, msg_id, username, name, msg_type, msg_content, media_id, date, user_id, ori_msg_id)
def update_comments(bot, channel_id, msg_id, update_mode): logger = Logger.logger config = helper_database.get_channel_config(channel_id) if config is None: return channel_lang = config[1] mode = config[2] recent = config[3] logger.msg( { "channel_id": channel_id, "msg_id": msg_id, "update_mode": update_mode }, tag="private", log_level=80) # update comments in channel comment_id = helper_database.get_comment_id(channel_id, msg_id) if comment_id is None: # If no comment message, just update Like buttons buttons = helper_database.get_button_options(channel_id, msg_id) stat = helper_database.get_reaction_stat(channel_id, msg_id) # Prepare Keyboard motd_keyboard = [[ InlineKeyboardButton(value + (" (%d)" % stat[idx] if idx in stat else ""), callback_data="like,%s,%s,%d" % (channel_id, msg_id, idx)) for idx, value in enumerate(buttons) ]] + [[ 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', ''), channel_id, msg_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', ''), channel_id, msg_id)) ]] motd_markup = InlineKeyboardMarkup(motd_keyboard) bot.edit_message_reply_markup(chat_id=channel_id, message_id=msg_id, reply_markup=motd_markup) return # Otherwise # Update Like buttons if update_mode == 0: buttons = helper_database.get_button_options(channel_id, msg_id) stat = helper_database.get_reaction_stat(channel_id, msg_id) # Prepare Keyboard motd_keyboard = [[ InlineKeyboardButton( value + (" (%d)" % stat[idx] if idx in stat else ""), callback_data="like,%s,%s,%d" % (channel_id, msg_id, idx)) for idx, value in enumerate(buttons) ]] motd_markup = InlineKeyboardMarkup(motd_keyboard) bot.edit_message_reply_markup(chat_id=channel_id, message_id=msg_id, reply_markup=motd_markup) return # Update comment message records = helper_database.get_recent_records(channel_id, msg_id, recent) # 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', ''), channel_id, msg_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', ''), channel_id, msg_id)) ]] motd_markup = InlineKeyboardMarkup(motd_keyboard) bot.edit_message_text(text=helper_global.records_to_str( records, channel_lang), chat_id=channel_id, message_id=comment_id, parse_mode=telegram.ParseMode.HTML, reply_markup=motd_markup)
def update_comments(bot, channel_id, msg_id, update_mode): logger = Logger.logger config = helper_database.get_channel_config(channel_id) if config is None: return channel_lang = config[1] mode = config[2] recent = config[3] logger.msg( { "channel_id": channel_id, "msg_id": msg_id, "update_mode": update_mode }, tag="private", log_level=80) # update comments in channel comment_id = helper_database.get_comment_id(channel_id, msg_id) if comment_id is None: # If no comment message, just update Like buttons buttons = helper_database.get_button_options(channel_id, msg_id) stat = helper_database.get_reaction_stat(channel_id, msg_id) # Prepare Keyboard motd_keyboard = [[ InlineKeyboardButton(value + (" (%d)" % stat[idx] if idx in stat else ""), callback_data="like,%s,%s,%d" % (channel_id, msg_id, idx)) for idx, value in enumerate(buttons) ]] + [[ 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', ''), channel_id, msg_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', ''), channel_id, msg_id)) ]] motd_markup = InlineKeyboardMarkup(motd_keyboard) bot.edit_message_reply_markup(chat_id=channel_id, message_id=msg_id, reply_markup=motd_markup) return # Otherwise # Update Like buttons if update_mode == 0: buttons = helper_database.get_button_options(channel_id, msg_id) stat = helper_database.get_reaction_stat(channel_id, msg_id) # Prepare Keyboard motd_keyboard = [[ InlineKeyboardButton( value + (" (%d)" % stat[idx] if idx in stat else ""), callback_data="like,%s,%s,%d" % (channel_id, msg_id, idx)) for idx, value in enumerate(buttons) ]] motd_markup = InlineKeyboardMarkup(motd_keyboard) bot.edit_message_reply_markup(chat_id=channel_id, message_id=msg_id, reply_markup=motd_markup) return # Update comment message records = helper_database.get_recent_records(channel_id, msg_id, recent) # 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', ''), channel_id, msg_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', ''), channel_id, msg_id)) ]] motd_markup = InlineKeyboardMarkup(motd_keyboard) rev_records = records[:] rev_records.reverse() scope = {"BOT_TOKEN": "", "FILES_GROUP": 0} with open(sys.path[0] + "/helper_const.py", "r") as f: lines = f.readlines() for line in lines: if line.strip().startswith("BOT_TOKEN") or line.strip().startswith( "FILES_GROUP"): # trusted file exec(line, scope) infos = [] render_as_text_records = [] for record in rev_records: name = record[3] ftype = "text" if record[4] == "text" else "unsupported" if record[4] == "photo": ftype = "image" elif record[4] == "sticker": ftype = "sticker" content = record[5] fid = record[6] timestr = record[7] uid = record[8] reply_to = record[10] url = "" if fid != "": ctx = f"https://api.telegram.org/bot{scope['BOT_TOKEN']}/getFile?file_id={fid}" with urllib.request.urlopen(ctx) as resp: result = resp.read() url = "https://api.telegram.org/file/" url += "bot" + scope["BOT_TOKEN"] + "/" + json.loads( result)["result"]["file_path"] info = { "name": name, "type": ftype, "content": content, "timeStr": timestr, "uid": uid, "url": url, "replyTo": reply_to } infos.append(info) if ftype == "text": if len(render_as_text_records) < 5: render_as_text_records.append(record) else: render_as_text_records.remove(render_as_text_records[0]) render_as_text_records.append(record) else: render_as_text_records = [] render_as_text_records.reverse() infos = infos[0:len(infos) - len(render_as_text_records)] remote = "http://127.0.0.1:6899/render/" + urllib.parse.quote( json.dumps(infos), safe='') with urllib.request.urlopen(remote) as resp: fpath = json.loads(resp.read())["path"] with open(fpath, "rb") as f: photo_message = bot.send_photo( chat_id=scope["FILES_GROUP"], photo=f, caption=helper_global.records_to_str(render_as_text_records, channel_lang), parse_mode=telegram.ParseMode.HTML).result() photo = photo_message.photo[0] comment_text = helper_global.records_to_str( render_as_text_records, channel_lang) media = telegram.InputMediaPhoto( photo.file_id, caption=comment_text, parse_mode=telegram.ParseMode.HTML) bot.edit_message_media( # text=helper_global.records_to_str(render_as_text_records, channel_lang), media=media, chat_id=channel_id, message_id=comment_id, parse_mode=telegram.ParseMode.HTML, reply_markup=motd_markup)
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))
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 channel_post_msg(bot, update): logger = Logger.logger message = update.channel_post if message is None: return chat_id = message.chat_id message_id = message.message_id config = helper_database.get_channel_config(chat_id) if config is None: return lang, mode, recent, button_mode = config[1], config[2], config[3], config[ 7] # Manual Mode if message.reply_to_message is not None and message.text.startswith( "/comment"): logger.msg( { "channel_id": chat_id, "msg_id": message_id, "mode": mode, "button": button_mode, "action": "/comment" }, tag="channel", log_level=90) message_id = message.reply_to_message.message_id bot.delete_message(chat_id=chat_id, message_id=message.message_id) if not helper_database.check_reflect( chat_id, message_id) and message.reply_to_message.reply_markup is None: add_compact_comment(bot, chat_id, config, message_id, message.reply_to_message) if not button_mode == 0: buttons = message.text.split()[1:] if button_mode == 1 and len(buttons) == 0: buttons = helper_database.get_default_button_options(chat_id) add_like_buttons(bot, lang, config, chat_id, message_id, message, buttons) # Force Comment for Special Cases elif message.reply_to_message is not None and message.text == "/forcecomment": logger.msg( { "channel_id": chat_id, "msg_id": message_id, "mode": mode, "button": button_mode, "action": "/forcecomment" }, tag="channel", log_level=90) message_id = message.reply_to_message.message_id bot.delete_message(chat_id=chat_id, message_id=message.message_id) helper_database.delete_reflect(chat_id, message_id) add_compact_comment(bot, chat_id, config, message_id, message.reply_to_message) # Set default buttons elif message.text is not None and message.text.startswith( "/defaultbuttons"): logger.msg( { "channel_id": chat_id, "msg_id": message_id, "mode": mode, "button": button_mode, "action": "/defaultbuttons" }, tag="channel", log_level=90) buttons = message.text.split()[1:] bot.delete_message(chat_id=chat_id, message_id=message.message_id) helper_database.add_button_options(chat_id, 0, buttons) # Auto Comment Mode elif mode == 1: logger.msg( { "channel_id": chat_id, "msg_id": message_id, "mode": mode, "button": button_mode, "action": "new channel post" }, tag="channel", log_level=90) add_comment(bot, chat_id, config, message_id, media_group_id=message.media_group_id) if button_mode == 1: add_like_buttons( bot, lang, config, chat_id, message_id, message, helper_database.get_default_button_options(chat_id)) elif mode == 2: logger.msg( { "channel_id": chat_id, "msg_id": message_id, "mode": mode, "button": button_mode, "action": "new channel post" }, tag="channel", log_level=90) if button_mode == 1: add_like_buttons( bot, lang, config, chat_id, message_id, message, helper_database.get_default_button_options(chat_id)) else: add_compact_comment(bot, chat_id, config, message_id, message)
def msg_detail(bot, update, chat_id, origin_message_id, args): channel_id = int(args[1]) msg_id = int(args[2]) row_id = int(args[3]) config = helper_database.get_channel_config(channel_id) if config is None: return channel_lang = config[1] recent = config[3] admin_id = config[5] if row_id < 0: bot.answer_callback_query( callback_query_id=update.callback_query.id, text=helper_global.value("no_message_detail", "No Message", lang=channel_lang) ) return records = helper_database.get_record_by_rowid(row_id) if records is None or len(records) == 0: bot.answer_callback_query( callback_query_id=update.callback_query.id, text=helper_global.value("no_message_detail", "No Message", lang=channel_lang) ) return record = records[0] username = record[2] name = record[3] msg_type = record[4] msg_content = record[5] media_id= record[6] user_id = int(record[8]) base_offset = helper_database.get_base_offset_by_rowid(channel_id, msg_id, row_id) offset = base_offset // recent msg_from_button = [ [ InlineKeyboardButton( helper_global.value("msg_from", "Message From: ", lang=channel_lang) + name, callback_data="blank" ) ] ] admin_operation_button = [ [ InlineKeyboardButton( helper_global.value("delete_msg", "Delete Message", lang=channel_lang), callback_data="msg_delete,%d,%d,%d,%d,%d,%d" % (row_id, channel_id, msg_id, recent, offset, chat_id) ), InlineKeyboardButton( helper_global.value("unban_user", "Unban User", lang=channel_lang), callback_data="user_unban,%d,%d,%s,%d,%d" % (channel_id, user_id, "", msg_id, row_id) ) if helper_database.check_ban(channel_id, user_id) else \ InlineKeyboardButton( helper_global.value("ban_user", "Ban User", lang=channel_lang), callback_data="user_ban,%d,%d,%s,%d,%d" % (channel_id, user_id, "", msg_id, row_id) ) ] ] if str(chat_id) == str(admin_id) else [] motd_keyboard = msg_from_button + admin_operation_button + [ [ InlineKeyboardButton( helper_global.value("prev_msg", "Prev Message", lang=channel_lang), callback_data="msg_detail,%d,%d,%d" % (channel_id, msg_id, helper_database.get_next_rowid(channel_id, msg_id, row_id)) ), InlineKeyboardButton( helper_global.value("next_msg", "Next Message", lang=channel_lang), callback_data="msg_detail,%d,%d,%d" % (channel_id, msg_id, helper_database.get_prev_rowid(channel_id, msg_id, row_id)) ) ], [ InlineKeyboardButton( helper_global.value("back_to_msg_list", "Back to message list", lang=channel_lang), callback_data="msg,%d,%d,%d,%d,%d" % (channel_id, msg_id, recent, offset, chat_id) ) ] ] motd_markup = InlineKeyboardMarkup(motd_keyboard) if msg_type == "text": bot.send_message( chat_id=chat_id, message_id=origin_message_id, text=msg_content, parse_mode='HTML', reply_markup=motd_markup ) bot.delete_message( chat_id=chat_id, message_id=origin_message_id ) elif msg_type == "audio" or msg_type == "document" or msg_type == "photo" or msg_type == "video" or msg_type == "sticker" or msg_type == "voice": send_func = { "audio": bot.send_audio, "document": bot.send_document, "photo": bot.send_photo, "video": bot.send_video, "sticker": bot.send_sticker, "voice": bot.send_voice } send_func[msg_type]( chat_id, media_id, caption=msg_content, parse_mode='HTML', reply_markup=motd_markup ) bot.delete_message( chat_id=chat_id, message_id=origin_message_id ) else: bot.send_message( chat_id=chat_id, message_id=origin_message_id, text="[%s] %s" % (msg_type, msg_content), parse_mode='HTML', reply_markup=motd_markup ) bot.delete_message( chat_id=chat_id, message_id=origin_message_id )