def schedule_own_private_digest(bot, job, data): start_in = 0 for i in data: start_in += 0.1 user = i[0] groups = i[1] user_id = user[0] lang = user[1] tot_msg = user[2] tot_grps = user[3] tot_pos = user[4] first_name = user[5] text = get_lang.get_string( lang, "hello_name").format(name=first_name) + "!\n" text += get_lang.get_string(lang, "digest_of_the_week_global").format( utils.sep_l(tot_msg, lang), utils.sep_l(tot_grps, lang), utils.sep_l(tot_pos, lang)) reply_markup = keyboards.disable_private_own_weekly_digest_kb(lang) # append the text of any group for the same user for group in groups: title = group[0] username = group[1] msg_g = group[2] pos_g = group[3] text += get_lang.get_string(lang, "digest_of_the_week_detail").format( utils.sep_l(msg_g, lang), username, utils.sep_l(pos_g, lang)) text += "\n#weekly_private_digest" # text completed can be scheduled job.job_queue.run_once(send_one_by_one, start_in, context=[user_id, text, reply_markup])
def group_rank_private(bot, update, args): user_id = update.message.from_user.id lang = utils.get_db_lang(user_id) if len(args) != 1: text = get_lang.get_string(lang, "error_param_group_rank_private") update.message.reply_text(text, parse_mode="HTML") return username = args[0] if username.startswith("@"): username = username.replace("@", "") query = "SELECT group_id FROM supergroups_ref WHERE LOWER(username) = LOWER(%s)" extract = database.query_r(query, username) if len(extract) > 1: print("error too many") return if len(extract) == 0: # the group does not exist otherwise anything is returned and if None is NULL text = get_lang.get_string(lang, "cant_check_this").format( html.escape(username)) update.message.reply_text(text=text) return group_id = extract[0][0] update.message.reply_text(text=group_rank_text(group_id, lang), parse_mode="HTML")
def change_vote_kb(group_id, lang, vote_first_time=False): button_back = InlineKeyboardButton( text=get_lang.get_string(lang, "change_vote") if vote_first_time is False else get_lang.get_string(lang, "vote"), callback_data="change_vote:{}".format(group_id)) buttons_list = [[button_back]] keyboard = InlineKeyboardMarkup(buttons_list) return keyboard
def advanced_commands_kb(lang): advanced_commands = InlineKeyboardButton(text=get_lang.get_string( lang, "advanced_commands"), callback_data="advanced_commands") back = InlineKeyboardButton(text=get_lang.get_string(lang, "back"), callback_data="back_main_private_help") buttons_list = [[advanced_commands], [back]] keyboard = InlineKeyboardMarkup(buttons_list) return keyboard
def round_seconds(seconds, lang, short=False): if seconds < 60: return get_lang.get_string(lang, "seconds_ago").format(seconds) if short is False else get_lang.get_string(lang, "seconds_ago_short").format(seconds) elif 60 <= seconds < 60*60: unit = seconds/60 r_unit = round(unit) return get_lang.get_string(lang, "about_minutes_ago").format(r_unit) if short is False else get_lang.get_string(lang, "about_minutes_ago_short").format(r_unit) else: unit = seconds/(60*60) r_unit = round(unit) return get_lang.get_string(lang, "about_hours_ago").format(r_unit) if short is False else get_lang.get_string(lang, "about_hours_ago").format(r_unit)
def private_digest_kb(lang): button0 = InlineKeyboardButton(text=get_lang.get_string( lang, "private_your_own_digest_button"), callback_data="private_your_own_digest") button1 = InlineKeyboardButton(text=get_lang.get_string( lang, "private_groups_digest_button"), callback_data="private_groups_digest") button2 = InlineKeyboardButton(text=get_lang.get_string(lang, "back"), callback_data="main_private_settings") buttons_list = [[button0], [button1], [button2]] keyboard = InlineKeyboardMarkup(buttons_list) return keyboard
def main_private_settings_kb(lang): button0 = InlineKeyboardButton(text=get_lang.get_string( lang, "private_lang_button"), callback_data="private_lang") button1 = InlineKeyboardButton(text=get_lang.get_string( lang, "private_region_button"), callback_data="private_region") button2 = InlineKeyboardButton(text=get_lang.get_string( lang, "private_digest_button"), callback_data="private_digest_button") buttons_list = [[button0], [button1], [button2]] keyboard = InlineKeyboardMarkup(buttons_list) return keyboard
def vote(bot, update, args): user_id = update.message.from_user.id lang = utils.get_db_lang(user_id) if len(args) != 1: text = get_lang.get_string(lang, "insert_param_vote") update.message.reply_text(text, parse_mode="HTML") return username = args[0] if username.startswith("@"): username = username.replace("@", "") query = """ SELECT s.group_id, s_ref.username, s_ref.title, v.vote, v.vote_date FROM supergroups_ref AS s_ref RIGHT JOIN supergroups AS s ON s_ref.group_id = s.group_id LEFT OUTER JOIN votes AS v ON v.group_id = s.group_id AND v.user_id = %s WHERE LOWER(s_ref.username) = LOWER(%s) AND s.bot_inside = TRUE """ extract = database.query_r(query, user_id, username) if len(extract) == 0: # the group does not exist otherwise anything is returned and if None is NULL text = get_lang.get_string(lang, "cant_vote_this") update.message.reply_text(text=text) return if len(extract) > 1: print("error too many") return extract = extract[0] text = get_lang.get_string(lang, "vote_this_group").format( extract[0], extract[1], extract[2]) if extract[3] and extract[4] is not None: stars = emojis.STAR * extract[3] date = utils.formatted_date_l(extract[4].date(), lang) text += "\n\n" + get_lang.get_string(lang, "already_voted").format( stars, date) if extract[3] and extract[4] is not None: reply_markup = keyboards.change_vote_kb(extract[0], lang) else: text += "\n\n" text += get_lang.get_string(lang, "vote_from_one_to_five") reply_markup = keyboards.vote_group_kb(extract[0], lang) update.message.reply_text(text=text, reply_markup=reply_markup)
def filter_category_button(lang, base, chosen_page): category = base.split(":")[4] if category != "": return [ InlineKeyboardButton( text=get_lang.get_string(lang, 'remove_filter'), callback_data=":".join(base.split(":")[:4]).format(page=1) + ":") ] return [ InlineKeyboardButton( text=get_lang.get_string(lang, 'filter_by_category'), callback_data='fc:' + base.format(page=chosen_page)) ]
def generic_leaderboard_kb(lang, region): members = InlineKeyboardButton( text=get_lang.get_string(lang, "by_members"), callback_data="leaderboard_by:" + leaderboards.Leaderboard.MEMBERS + ":" + region) messages = InlineKeyboardButton( text=get_lang.get_string(lang, "by_messages"), callback_data="leaderboard_by:" + leaderboards.Leaderboard.MESSAGES + ":" + region) votes = InlineKeyboardButton(text=get_lang.get_string(lang, "by_votes"), callback_data="leaderboard_by:" + leaderboards.Leaderboard.VOTES + ":" + region) buttons_list = [[members], [messages], [votes]] keyboard = InlineKeyboardMarkup(buttons_list) return keyboard
def weekly_own_digest_kb(lang, value): yes = get_lang.get_string(lang, "yes") no = get_lang.get_string(lang, "no") back = get_lang.get_string(lang, "back") button_yes = InlineKeyboardButton( text=emojis.CURRENT_CHOICE + yes if value is True else yes, callback_data="set_weekly_own_digest:true") button_no = InlineKeyboardButton( text=emojis.CURRENT_CHOICE + no if value is False else no, callback_data="set_weekly_own_digest:false") button_back = InlineKeyboardButton(text=back, callback_data="back_private_digest") buttons_list = [[button_yes, button_no], [button_back]] keyboard = InlineKeyboardMarkup(buttons_list) return keyboard
def filter_by_category_leaderboard_kb(lang, base, back_callback): buttons_list = [] strings = get_lang.get_string(lang, "categories") for i in sorted(categories.CODES.items(), key=lambda x: x[1]): buttons_list.append( InlineKeyboardButton(text=strings[i[1]], callback_data=base + str(i[0]))) footer = InlineKeyboardButton(text=get_lang.get_string(lang, "back"), callback_data=back_callback) footer_buttons = [footer] buttons_list = build_menu(buttons_list, n_cols=2, footer_buttons=footer_buttons) keyboard = InlineKeyboardMarkup(buttons_list) return keyboard
def adult_content_kb(lang, value): yes = get_lang.get_string(lang, "yes") no = get_lang.get_string(lang, "no") back = get_lang.get_string(lang, "back") button_yes = InlineKeyboardButton(text=emojis.CURRENT_CHOICE + yes if value is True else yes, callback_data="set_adult_true") button_no = InlineKeyboardButton(text=emojis.CURRENT_CHOICE + no if value is False else no, callback_data="set_adult_false") button_back = InlineKeyboardButton( text=back, callback_data="main_group_settings_creator") buttons_list = [[button_yes, button_no], [button_back]] keyboard = InlineKeyboardMarkup(buttons_list) return keyboard
def disable_private_own_weekly_digest_kb(lang): disable = InlineKeyboardButton( text=get_lang.get_string(lang, "disable"), callback_data="private_your_own_digest:new_msg") buttons_list = [[disable]] keyboard = InlineKeyboardMarkup(buttons_list) return keyboard
def start_no_params(bot, update): lang = utils.get_db_lang(update.message.from_user.id) text = get_lang.get_string(lang, "help_message") reply_markup = keyboards.help_kb(lang) update.message.reply_text(text=text, parse_mode="HTML", reply_markup=reply_markup)
def vote_link_kb(lang): button_back = InlineKeyboardButton( text=get_lang.get_string(lang, "back"), callback_data="main_group_settings_admin") buttons_list = [[button_back]] keyboard = InlineKeyboardMarkup(buttons_list) return keyboard
def vote_intro(group_id, lang): query = "SELECT username, title FROM supergroups_ref WHERE group_id = %s" extract = database.query_r(query, group_id, one=True) if extract is None: return None else: return get_lang.get_string(lang, "vote_this_group").format(group_id, extract[0], extract[1])
def current_page_admin(bot, query): group_id = query.message.chat.id query_db = "SELECT lang FROM supergroups WHERE group_id = %s" extract = database.query_r(query_db, group_id, one=True) lang = extract[0] if extract is not None else None query.answer(get_lang.get_string(lang, "already_this_page"), show_alert=True)
def feedback_reply_kb(lang): reply = InlineKeyboardButton(text=get_lang.get_string( lang, "feedback_reply"), callback_data="feedback_reply") buttons_list = [[reply]] keyboard = InlineKeyboardMarkup(buttons_list) return keyboard
def language_private(bot, update): query = "SELECT lang FROM users WHERE user_id = %s" extract = database.query_r(query, update.message.from_user.id, one=True) lang = extract[0] text = get_lang.get_string(lang, "choose_your_lang") reply_markup = keyboards.private_language_kb(lang, back=False) update.message.reply_text(text=text, reply_markup=reply_markup)
def settings_group(bot, update): query_db = "SELECT lang FROM supergroups WHERE group_id = %s" lang = database.query_r(query_db, update.message.chat.id, one=True)[0] text = get_lang.get_string(lang, "choose_group_lang") reply_markup = keyboards.main_group_settings_kb(lang) update.message.reply_text(text=text, reply_markup=reply_markup, quote=False)
def region(bot, update): query = "SELECT lang, region FROM users WHERE user_id = %s" extract = database.query_r(query, update.message.from_user.id, one=True) lang = extract[0] region = extract[1] text = get_lang.get_string(lang, "choose_region") reply_markup = keyboards.private_region_kb(lang, region) update.message.reply_text(text=text, reply_markup=reply_markup)
def feedback_reply(bot, query): try: query.message.edit_reply_markup(reply_markup=None) except TelegramError as e: if str(e) != "Message is not modified": print(e) lang = utils.get_db_lang(query.from_user.id) text = get_lang.get_string(lang, "feedback_message") bot.sendMessage(chat_id=query.from_user.id, text=text)
def group_categories_kb(lang, current_category): buttons_list = [] strings = get_lang.get_string(lang, "categories") for i in sorted(categories.CODES.items(), key=lambda x: x[1]): buttons_list.append( InlineKeyboardButton( text=emojis.CURRENT_CHOICE + strings[i[1]] if i[0] == current_category else strings[i[1]], callback_data="set_group_category:" + str(i[0]))) footer = InlineKeyboardButton(text=get_lang.get_string(lang, "back"), callback_data="main_group_settings_creator") footer_buttons = [footer] buttons_list = build_menu(buttons_list, n_cols=2, footer_buttons=footer_buttons) keyboard = InlineKeyboardMarkup(buttons_list) return keyboard
def leave_banned_group(bot, update): query_db = "SELECT lang, banned_until, ban_reason FROM supergroups WHERE group_id = %s" extract = database.query_r(query_db, update.message.chat.id, one=True) lang = extract[0] banned_until = extract[1] reason = extract[2] shown_reason = html.escape( reason) if reason is not None else get_lang.get_string( lang, "not_specified") shown_reason = "<code>{}</code>".format(shown_reason) text = get_lang.get_string(lang, "banned_until_leave").format( utils.formatted_datetime_l(banned_until.replace(microsecond=0), lang), shown_reason) update.message.reply_text(text=text, quote=False, parse_mode='HTML') bot.leaveChat(update.message.chat.id) query = "UPDATE supergroups SET bot_inside = FALSE WHERE group_id = %s" database.query_w(query, update.message.chat.id)
def private_digest(bot, query): query.answer() lang = utils.get_db_lang(query.from_user.id) text = get_lang.get_string(lang, "private_digest") reply_markup = keyboards.private_digest_kb(lang) try: query.edit_message_text(text=text, reply_markup=reply_markup) except TelegramError as e: if str(e) != "Message is not modified": print(e)
def wrapped(bot, query, *args, **kwargs): if query.message.chat.type == "private": return if not query.message.chat.get_member(query.from_user.id).status in ["administrator", "creator"]: lang = get_db_lang(query.from_user.id) text = get_lang.get_string(lang, "button_for_admins") query.answer(text=text, show_alert=True) return return func(bot, query, *args, **kwargs)
def remember_to_set_lang(bot, update): if not rtsl_is_creator(bot, update): return if rtsl_already_sent(bot, update): return lang = None text = get_lang.get_string(lang, "hey_no_lang_set") reply_markup = keyboards.select_group_lang_kb(lang, back=False) update.message.reply_text(text=text, reply_markup=reply_markup)
def wrapped(bot, update, *args, **kwargs): if update.message.chat.type == "private": lang = get_db_lang(update.effective_user.id) text = get_lang.get_string(lang, "this_command_only_creator").format(update.message.text.split(" ")[0]) update.message.reply_text(text) return status = update.effective_chat.get_member(update.message.from_user.id).status if status not in ["creator"]: lang = get_db_lang(update.effective_user.id) text = get_lang.get_string(lang, "this_command_only_creator").format(update.message.text.split(" ")[0]) try: chat_id = update.message.from_user.id bot.send_message(chat_id=chat_id, text=text) except: if status in ['administrator']: update.message.reply_text(text) return return func(bot, update, *args, **kwargs)
def ban_group(bot, update, args): if len(args) < 3: text = "specify id for days\nexample: -34545322 for 30 (optional: for too much spam)" update.message.reply_text(text) return params = " ".join(args).split(" for ") group_id = params[0] days = int(params[1]) try: reason = params[2] except IndexError: reason = None query = """ UPDATE supergroups SET banned_on = now(), banned_until = now() + interval '%s days', ban_reason = %s WHERE group_id = %s RETURNING lang, banned_until """ extract = database.query_wr(query, days, reason, group_id, one=True) lang = extract[0] banned_until = extract[1] shown_reason = html.escape( reason) if reason is not None else get_lang.get_string( lang, "not_specified") shown_reason = "<code>{}</code>".format(shown_reason) text = get_lang.get_string(lang, "banned_until_leave").format( utils.formatted_datetime_l(banned_until.replace(microsecond=0), lang), shown_reason) text += utils.text_mention_creator(bot, group_id) try: bot.send_message(chat_id=group_id, text=text, parse_mode='HTML') bot.leaveChat(group_id) except Unauthorized as e: update.message.reply_text(e.message) query = "UPDATE supergroups SET bot_inside = FALSE WHERE group_id = %s" database.query_w(query, group_id) update.message.reply_text("Done!")