Exemplo n.º 1
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
    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)
Exemplo n.º 2
0
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)
Exemplo n.º 3
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)
Exemplo n.º 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)
    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!"))
Exemplo n.º 5
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)
Exemplo n.º 6
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
    )
Exemplo n.º 7
0
def add_comment(bot, chat_id, config, message_id, media_group_id=None):
    channel_lang = config[1]
    recent = config[3]

    # Avoid duplicated comment for media group
    if media_group_id:
        last_media_group = helper_global.value(
            str(chat_id) + '_last_media_group', '0')
        print(last_media_group)
        if last_media_group == media_group_id:
            return
        helper_global.assign(
            str(chat_id) + '_last_media_group', media_group_id)

    # Prepare Keyboard
    motd_keyboard = [[
        InlineKeyboardButton(
            helper_global.value("add_comment",
                                "Add Comment",
                                lang=channel_lang),
            url="http://telegram.me/%s?start=add_%d_%d" %
            (helper_global.value('bot_username', ''), chat_id, message_id)),
        InlineKeyboardButton(
            helper_global.value("show_all_comments",
                                "Show All",
                                lang=channel_lang),
            url="http://telegram.me/%s?start=show_%s_%d" %
            (helper_global.value('bot_username', ''), chat_id, message_id))
    ]]
    motd_markup = InlineKeyboardMarkup(motd_keyboard)

    records = helper_database.get_recent_records(chat_id, message_id, recent)

    comment_message = bot.send_message(
        chat_id=chat_id,
        text=helper_global.records_to_str(records, channel_lang),
        reply_to_message_id=message_id,
        reply_markup=motd_markup,
        parse_mode=telegram.ParseMode.HTML).result()
    helper_database.add_reflect(chat_id, message_id,
                                comment_message.message_id)
Exemplo n.º 8
0
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)
Exemplo n.º 9
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))
Exemplo n.º 10
0
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)