Example #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_photo(
        photo=open(sys.path[0] + "/default-comment.jpg", "rb"),
        caption=helper_global.records_to_str(records, channel_lang),
        chat_id=chat_id,
        reply_to_message_id=message_id,
        reply_markup=motd_markup,
        parse_mode=telegram.ParseMode.HTML).result()
    helper_database.add_reflect(chat_id, message_id,
                                comment_message.message_id)
def private_msg(bot, update):
    message = update.edited_message if update.edited_message else update.message
    # print(message)
    chat_id = message.chat_id

    args = helper_global.value(str(chat_id) + "_status", "0,0")
    params = args.split(",")
    channel_id = int(params[0])
    msg_id = int(params[1])
    if channel_id == 0:
        if msg_id == 1:
            check_channel_message(bot, message)
        return

    # Check comment message
    comment_exist = helper_database.check_reflect(channel_id, msg_id)
    if not comment_exist:
        config = helper_database.get_channel_config(channel_id)
        if config is None:
            return
        recent = config[3]
        records = helper_database.get_recent_records(channel_id, msg_id,
                                                     recent)

        comment_message = bot.send_message(
            chat_id=channel_id,
            text=helper_global.records_to_str(records),
            reply_to_message_id=msg_id,
            parse_mode=telegram.ParseMode.HTML).result()
        helper_database.add_reflect(channel_id, msg_id,
                                    comment_message.message_id)
        bot.edit_message_reply_markup(chat_id=channel_id,
                                      message_id=msg_id,
                                      reply_markup=None)

    result = add_record(channel_id, msg_id, message)

    # Update Dirty List
    lock.acquire()
    dirty_list = helper_global.value("dirty_list", [])
    if not (channel_id, msg_id) in dirty_list:
        dirty_list.append((channel_id, msg_id))
    helper_global.assign("dirty_list", dirty_list)
    lock.release()

    if result == 0:
        bot.send_message(chat_id=chat_id,
                         text=helper_global.value("comment_success",
                                                  "Success!"))
    elif result == 1:
        bot.send_message(chat_id=chat_id,
                         text=helper_global.value("comment_edit_success",
                                                  "Success!"))
def 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))
Example #4
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)
    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))
Example #5
0
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)