コード例 #1
0
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_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)
コード例 #2
0
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!"))
コード例 #3
0
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] if args[4] != '' else '0')
    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)
コード例 #4
0
def add_inplace_comment(bot, chat_id, config, message_id, message, buttons):
    logger = Logger.logger
    logger.msg(
        {
            "channel_id": chat_id,
            "msg_id": message_id,
            "action": "inplace comment"
        },
        tag="channel",
        log_level=80)
    channel_lang = config[1]

    # Fallback media group message
    if message.media_group_id:
        return

    if message.forward_from or message.forward_from_chat:
        new_message = deforward(bot, message, channel_lang)
        message_id = new_message.message_id
        message = new_message

    # Prepare Keyboard
    if buttons and len(buttons) > 0:
        helper_database.add_button_options(chat_id, message_id, buttons)
        helper_database.clear_reaction(chat_id, message_id)

    motd_keyboard = [[
        InlineKeyboardButton(
            value, callback_data="like,%s,%s,%d" % (chat_id, message_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', ''), 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)

    try:
        bot.edit_message_reply_markup(chat_id=chat_id,
                                      message_id=message_id,
                                      reply_markup=motd_markup).result()
    except:
        return
    helper_database.add_reflect(
        chat_id, message_id,
        avoidNone(message.caption if message.caption else message.text))
コード例 #5
0
def option_update(bot, update, lang, chat_id, origin_message_id, args):
    try:
        helper_database.update_config_by_channel(args[1], args[2], args[3])
        bot.answer_callback_query(
            callback_query_id=update.callback_query.id,
            text=helper_global.value("option_update_success", "", lang=lang)
        )
    except:
        bot.answer_callback_query(
            callback_query_id=update.callback_query.id,
            text=helper_global.value("option_update_failed", "", lang=lang)
        )
    option_item(bot, update, lang, chat_id, origin_message_id, args)
コード例 #6
0
ファイル: option_cmd.py プロジェクト: artxia/backup
def option(bot, update, args):
    logger = Logger.logger
    if args is None or len(args) == 0:
        lang = helper_const.DEFAULT_LANG
    else:
        lang = args[0]
    chat_id = update.message.chat_id
    logger.msg({"user_id": chat_id}, tag="option", log_level=80)
    records = helper_database.get_channel_info_by_user(chat_id)
    if records is None or len(records) == 0:
        helper_global.send_intro_template(bot, chat_id,
                                          helper_const.DEFAULT_LANG,
                                          "option_no_channel",
                                          "option_no_channel")
        return

    #Prepare keyboard
    lang_list = helper_const.LANG_LIST
    width = helper_const.LANG_WIDTH
    current_lang = lang
    key = "option"
    motd_keyboard = [[
        InlineKeyboardButton(
            "@" + record[1] if record[1] else "id: " + str(record[0]),
            callback_data="option|%s,%s" % (lang, record[0]))
    ] for record in records] + [[
        InlineKeyboardButton(helper_global.value("option_finish", "", lang),
                             callback_data="option_finish|%s" % lang)
    ]] + [[
        InlineKeyboardButton(
            lang_list[width * idx + delta] +
            (" (*)" if lang_list[width * idx + delta] == current_lang else ""),
            callback_data="%s|%s" % (key, lang_list[width * idx + delta]))
        for delta in range(width)
    ] for idx in range(len(lang_list) // width)] + [[
        InlineKeyboardButton(
            lang_list[idx] +
            (" (*)" if lang_list[idx] == current_lang else ""),
            callback_data="%s|%s" % (key, lang_list[idx]))
        for idx in range(width * (len(lang_list) // width), len(lang_list))
    ]]

    motd_markup = InlineKeyboardMarkup(motd_keyboard)
    bot.send_message(chat_id=chat_id,
                     text=helper_global.value("option_choose_channel",
                                              "",
                                              lang=lang),
                     reply_markup=motd_markup)
コード例 #7
0
def user_unban(bot, update, chat_id, origin_message_id, args):
    channel_id = int(args[1])
    user_id = int(args[2])
    name = args[3]
    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", ""))
コード例 #8
0
ファイル: cancel_cmd.py プロジェクト: artxia/backup
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)
コード例 #9
0
def intro_template(bot, update, lang, chat_id, origin_message_id, key, text_key):
    lang_list = helper_const.LANG_LIST
    width = helper_const.LANG_WIDTH
    current_lang = lang
    motd_keyboard = [[
        InlineKeyboardButton(
            lang_list[width * idx + delta] + (" (*)" if lang_list[width * idx + delta] == current_lang else ""),
            callback_data="%s|%s" % (key, lang_list[width * idx + delta])
        ) for delta in range(width)
    ] for idx in range(len(lang_list) // width)] + [[
        InlineKeyboardButton(
            lang_list[idx] + (" (*)" if lang_list[idx] == current_lang else ""),
            callback_data="%s|%s" % (key, lang_list[idx])
        )
    for idx in range(width * (len(lang_list) // width), len(lang_list))]]

    motd_markup = InlineKeyboardMarkup(motd_keyboard)
    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=helper_global.value(text_key, "", lang=current_lang),
        reply_markup=motd_markup
    )
コード例 #10
0
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()
コード例 #11
0
def option_delete(bot, chat_id, origin_message_id, args):
    channel_id = args[1]
    helper_database.delete_channel_config(channel_id)
    bot.edit_message_text(chat_id=chat_id,
                          message_id=origin_message_id,
                          text=helper_global.value("option_record_deleted",
                                                   ""))
コード例 #12
0
def option_item(bot, update, lang, chat_id, origin_message_id, args):
    # Prepare Keyboard
    motd_keyboard = [[
        InlineKeyboardButton(
            "mode",
            callback_data="option|%s,%s,mode" % (lang, args[1])
        ),
        InlineKeyboardButton(
            "recent",
            callback_data="option|%s,%s,recent" % (lang, args[1])
        ),
        InlineKeyboardButton(
            "notify",
            callback_data="option|%s,%s,notify" % (lang, args[1])
        )
    ]] + [[
        InlineKeyboardButton(
            "lang",
            callback_data="option|%s,%s,lang" % (lang, args[1])
        ),
        InlineKeyboardButton(
            "button",
            callback_data="option|%s,%s,button" % (lang, args[1])
        )
    ]] + [[
        InlineKeyboardButton(
            helper_global.value("option_delete", "", lang=lang),
            callback_data="option_delete|%s,%s" % (lang, args[1])
        )
    ]] + [[
        InlineKeyboardButton(
            helper_global.value("option_finish", "", lang=lang),
            callback_data="option_finish|%s" % lang
        )
    ]]

    motd_markup = InlineKeyboardMarkup(motd_keyboard)

    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=helper_global.value("option_choose_item", "", lang=lang),
        reply_markup=motd_markup
    )
コード例 #13
0
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"))
コード例 #14
0
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)
コード例 #15
0
def option_index(bot, update, lang, chat_id, origin_message_id, args):
    records = helper_database.get_channel_info_by_user(chat_id)
    if records is None or len(records) == 0:
        bot.send_message(
            chat_id=chat_id, 
            text=helper_global.value("option_no_channel", "", lang=lang)
        )
        return

    #Prepare keyboard
    lang_list = helper_const.LANG_LIST
    width = helper_const.LANG_WIDTH
    current_lang = lang
    key = "option"
    motd_keyboard = [[
        InlineKeyboardButton(
            "@" + record[1] if record[1] else "id: " + str(record[0]),
            callback_data="option|%s,%s" % (lang, record[0])
        )
    ] for record in records] + [[
        InlineKeyboardButton(
            helper_global.value("option_finish", "", lang),
            callback_data="option_finish|%s" % lang
        )
    ]] + [[
        InlineKeyboardButton(
            lang_list[width * idx + delta] + (" (*)" if lang_list[width * idx + delta] == current_lang else ""),
            callback_data="%s|%s" % (key, lang_list[width * idx + delta])
        ) for delta in range(width)
    ] for idx in range(len(lang_list) // width)] + [[
        InlineKeyboardButton(
            lang_list[idx] + (" (*)" if lang_list[idx] == current_lang else ""),
            callback_data="%s|%s" % (key, lang_list[idx])
        )
    for idx in range(width * (len(lang_list) // width), len(lang_list))]]

    motd_markup = InlineKeyboardMarkup(motd_keyboard)
    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=helper_global.value("option_choose_channel", "", lang=lang),
        reply_markup=motd_markup
    )
コード例 #16
0
def option_notify(bot, chat_id, origin_message_id, args):
    # Prepare Keyboard
    motd_keyboard = [[
        InlineKeyboardButton("0",
                             callback_data="option,%s,notify,0" % args[1]),
        InlineKeyboardButton("1", callback_data="option,%s,notify,1" % args[1])
    ]] + [[
        InlineKeyboardButton(helper_global.value("option_finish", ""),
                             callback_data="option_finish")
    ]]

    motd_markup = InlineKeyboardMarkup(motd_keyboard)

    bot.edit_message_text(chat_id=chat_id,
                          message_id=origin_message_id,
                          text=helper_global.value(
                              "option_choose_notify_value", ""),
                          reply_markup=motd_markup)
コード例 #17
0
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
    )
コード例 #18
0
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:]
    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", ""))
    private_msg.update_dirty_msg(channel_id, msg_id)
    show_msg(bot, update, origin_message_id, chat_id, msg_args)
コード例 #19
0
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])

    if offset < 0:
        bot.answer_callback_query(
            callback_query_id=update.callback_query.id,
            text=helper_global.value("no_next_page", "")
        )
        return

    # 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, ori_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, ori_chat_id)
        )
    ]]
    motd_markup = InlineKeyboardMarkup(motd_keyboard)
    records = helper_database.get_recent_records(channel_id, msg_id, recent, offset)

    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", "")
        )
        return

    bot.edit_message_text(
        chat_id=ori_chat_id, 
        message_id=origin_message_id,
        text=helper_global.records_to_str(records), 
        parse_mode=telegram.ParseMode.HTML,
        reply_markup=motd_markup
    )
コード例 #20
0
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"))
コード例 #21
0
def option_item(bot, chat_id, origin_message_id, args):
    # Prepare Keyboard
    motd_keyboard = [[
        InlineKeyboardButton("mode", callback_data="option,%s,mode" % args[1]),
        InlineKeyboardButton("recent",
                             callback_data="option,%s,recent" % args[1])
    ]] + [[
        InlineKeyboardButton(helper_global.value("option_delete", ""),
                             callback_data="option_delete,%s" % args[1])
    ]] + [[
        InlineKeyboardButton(helper_global.value("option_finish", ""),
                             callback_data="option_finish")
    ]]

    motd_markup = InlineKeyboardMarkup(motd_keyboard)

    bot.edit_message_text(chat_id=chat_id,
                          message_id=origin_message_id,
                          text=helper_global.value("option_choose_item", ""),
                          reply_markup=motd_markup)
コード例 #22
0
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
        )
コード例 #23
0
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
    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"),
            url="http://telegram.me/%s?start=add_%d_%d" %
            (helper_global.value('bot_username', ''), channel_id, msg_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', ''), channel_id, msg_id)),
        InlineKeyboardButton(
            helper_global.value("show_all_comments", "Show All"),
            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),
                          chat_id=channel_id,
                          message_id=comment_id,
                          parse_mode=telegram.ParseMode.HTML,
                          reply_markup=motd_markup)
コード例 #24
0
def add_compact_comment(bot, chat_id, config, message_id, message):
    channel_lang = config[1]

    # Fallback media group message
    if message.media_group_id:
        add_comment(bot,
                    chat_id,
                    config,
                    message_id,
                    media_group_id=message.media_group_id)
        return

    if message.forward_from or message.forward_from_chat:
        new_message = deforward(bot, message, channel_lang)
        message_id = new_message.message_id
        message = new_message

    # 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)

    try:
        bot.edit_message_reply_markup(chat_id=chat_id,
                                      message_id=message_id,
                                      reply_markup=motd_markup).result()
    except:
        add_comment(bot, chat_id, config, message_id)
コード例 #25
0
ファイル: helper_main.py プロジェクト: artxia/backup
def bot_reload(bot, update):
    global helper_const
    global helper_string
    global helper_database
    global command_module
    if not check_admin(update.message.from_user.id):
        permission_denied = helper_global.value("permission_denied_text", "")
        bot.send_message(chat_id=update.message.chat_id,
                         text=permission_denied)
        return

    ## update constant
    helper_const = importlib.reload(helper_const)
    helper_string = importlib.reload(helper_string)
    helper_database = importlib.reload(helper_database)
    reload_admin_list()

    ## remove old handlers
    for current_module in command_module:
        dispatcher.remove_handler(current_module._handler)

    ## reload modules and update handlers
    try:
        command_module = []
        for module_name in helper_const.MODULE_NAME:
            logger.msg("Reloading module \"%s\"..." % module_name,
                       tag="main",
                       log_level=100)
            current_module = importlib.import_module("modules." + module_name)
            current_module = importlib.reload(current_module)
            command_module.append(current_module)
            dispatcher.add_handler(current_module._handler)

        success_text = helper_global.value("reload_cmd_success", "")
        bot.send_message(chat_id=update.message.chat_id, text=success_text)
    except Exception as e:
        failed_text = helper_global.value("reload_cmd_failed", "")
        bot.send_message(chat_id=update.message.chat_id, text=failed_text)
        bot.send_message(chat_id=update.message.chat_id,
                         text=traceback.print_exc())
コード例 #26
0
ファイル: channel_msg.py プロジェクト: zrt/channel-helper-bot
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)
コード例 #27
0
def user_ban(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]
    try:
        helper_database.ban_user(channel_id, user_id, name)
    except:
        bot.answer_callback_query(
            callback_query_id=update.callback_query.id,
            text=helper_global.value("user_banned_failed", "", lang=channel_lang)
        )
    bot.answer_callback_query(
        callback_query_id=update.callback_query.id,
        text=helper_global.value("user_banned", "", lang=channel_lang)
    )
    msg_detail(bot, update, chat_id, origin_message_id, ["msg_detail", channel_id, msg_id, row_id])
コード例 #28
0
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)
コード例 #29
0
def execute(sql, params):
    lock.acquire()
    try:
        conn = helper_global.value("database", None)
        if conn is None:
            return
        cursor = conn.cursor()
        result = cursor.execute(sql, params)
        conn.commit()
    except Exception as e:
        lock.release()
        raise e
    lock.release()
    return result
コード例 #30
0
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]
    )