Exemple #1
0
def verify_login(update, context):
    END = -1
    USERNAME = 0
    bot = context.bot
    password = update.message.text
    context.user_data['password'] = generate_token(
        password)  # 记录使用者的 Instapaper 密码(password)
    message = update.message.reply_text('登入中,请稍候…')
    if get_client(context.user_data):
        context.user_data['client'] = get_client(context.user_data)
        bot_persistence.flush()
        msg = bot.edit_message_text(chat_id=message.chat_id,
                                    message_id=message.message_id,
                                    text='登入成功!试试发送带链接的消息')
        del_msg.later(update, context, msg)
        context.user_data['logged_in'] = True
        if not context.user_data.__contains__('today'):
            context.user_data['today'] = {}  # Initialization
        context.user_data['today']['date'] = get_today()
        bot_persistence.flush()
        return END

    else:
        keyboard = [[
            InlineKeyboardButton("重新尝试", callback_data='login_confirm')
        ]]
        markup = InlineKeyboardMarkup(keyboard)
        msg = bot.edit_message_text(chat_id=message.chat_id,
                                    message_id=message.message_id,
                                    text='抱歉,未登入成功。',
                                    reply_markup=markup)
        del_msg.later(update, context, msg)
        context.user_data.pop('username')
        context.user_data.pop('password')
        return USERNAME
Exemple #2
0
def about(update, context):
    keyboard = [[
        InlineKeyboardButton("源    代    码",
                             url='https://github.com/dahawong/instasaver-bot')
    ], [InlineKeyboardButton("工    作    室", url='https://office.daha.me/')]]
    markup = InlineKeyboardMarkup(keyboard)
    msg = update.message.reply_markdown('*Instasaver*  `2.0.4`',
                                        reply_markup=markup)
    del_msg.later(update, context, msg)
Exemple #3
0
def confirm_quit(update, context):
    END = -1
    context.user_data.clear()
    bot_persistence.flush()
    bot = context.bot
    query = update.callback_query
    msg = bot.edit_message_text(chat_id=query.message.chat_id,
                                message_id=query.message.message_id,
                                text='解绑成功!')
    del_msg.later(update, context, msg, timeup=2)
    return END
Exemple #4
0
def request_username(update, context):
    PASSWORD = 1
    bot = context.bot
    query = update.callback_query
    msg = bot.edit_message_text(
        chat_id=query.message.chat_id,
        message_id=query.message.message_id,
        text="请输入<strong>用户名</strong>或者<strong>邮箱</strong>:",
        parse_mode='HTML')
    del_msg.later(update, context, msg, timeup=120)
    return PASSWORD
Exemple #5
0
def quit_(update, context):
    CONFIRM_QUIT = 0
    END = -1
    if context.user_data.__contains__('logged_in'):
        keyboard = [[
            InlineKeyboardButton("返 回", callback_data='cancel_quit'),
            InlineKeyboardButton("解 绑", callback_data='confirm_quit')
        ]]
        markup = InlineKeyboardMarkup(keyboard)
        message = update.message.reply_text('确认解绑账号吗?', reply_markup=markup)
        del_msg.later(update, context, message)
        return CONFIRM_QUIT
    else:
        message = update.message.reply_text("你还没有登入呢。\n前往:/start")
        del_msg.later(update, context, message)
        return END
Exemple #6
0
def start(update, context):
    USERNAME = 0
    END = -1
    if not context.user_data.__contains__('logged_in'):
        keyboard_lan = [[
            InlineKeyboardButton("注 册", url='https://www.instapaper.com/'),
            InlineKeyboardButton("登 入", callback_data='login_confirm')
        ]]
        markup = InlineKeyboardMarkup(keyboard_lan)
        message = update.message.reply_text(
            '欢迎使用 Instasaver!\n开始使用前,请先登录您的账号。', reply_markup=markup)
        del_msg.later(update, context, message)
        return USERNAME
    else:
        message = update.message.reply_text('您已登录成功,可以直接使用!')
        del_msg.later(update, context, message)
        return END
Exemple #7
0
def today(update, context):
    message = update.message
    today = datetime.date.today() + datetime.timedelta(hours=8)
    year, month, day = today.year, today.month, today.day
    if context.user_data.__contains__('logged_in'):
        if not is_today(context.user_data['today']['date']):
            context.user_data['today'].clear()
            context.user_data['today']['date'] = get_today()
            bot_persistence.flush()
        else:
            message_body = f'`{year}\-{month}\-{day}`\n\n'
            articles_today = filter(lambda x: not isinstance(x, datetime.date),
                                    context.user_data['today'].values())
            if list(articles_today):
                count = 0
                for article in articles_today:
                    count += 1
                    title, link = article['title'], article['link']
                    message_body += f"{count}\. [{title}]({link})\n\n"
                msg = context.bot.send_message(
                    chat_id=message.chat_id,
                    message_id=message.message_id,
                    text=message_body,
                    parse_mode="MarkdownV2",
                    disable_web_page_preview=True,
                    reply_to_message_id=message.message_id)
                del_msg.later(update, context, msg)
            else:
                msg = message.reply_text('今天还没有保存文章呢')
                del_msg.later(update, context, msg, timeup=2)
    else:
        message = update.message.reply_text("你还没有登入呢。\n前往:/start")
        del_msg.later(update, context, message)
Exemple #8
0
def save_link(update, context):
    logged_in = context.user_data.__contains__('client')
    if logged_in:
        client = context.user_data['client']
        message = update.message.text_html
        pattern = re.compile(
            r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
        )
        links = re.findall(pattern, message)
        link_ids = {}
        titles = {}
        # pattern_ignore = r'https://readhacker\.news/c/.+'
        if not links:
            msg = update.message.reply_text('消息中没有发现链接。')
            del_msg.later(update, context, msg, timeup=2)
        else:
            supported_iv = {
                r"http[s]?://liqi\.io/": "7610e8062aab10",
                r"http[s]?://m\.qdaily\.com/mobile/articles/.+":
                "19c55d0f6b1acb",
                r"http[s]?://www\.douban\.com/.+": "100c1db4937b79",
                r"http[s]?://www\.ifanr\.com/.+": "00b04cf87f66cb",
                r"http[s]?://sspai\.com/post/.+": "a6663c627d6258",
                r"http[s]?://matters\.news/.+": "6382d3f855b181",
                r"http[s]?://mp\.weixin\.qq\.com": "cc652f39dd9149"
            }

            def can_iv(link):
                for pattern in supported_iv.keys():
                    if re.match(pattern, link):
                        return pattern
                return False

            def use_iv(rhash):
                return f"https://t.me/iv?url={link}&rhash={rhash}"

            count = 0
            failed = 0
            illegal_end = [")", "(", "!", "."]
            message_saving = update.message.reply_text(f"保存中 …")

            # Start saving
            for link in links:
                if link[-1] in illegal_end:
                    link = link.strip(f"{link[-1]}")
                bookmark_id, title = save(client, link)
                link_ids[link] = bookmark_id
                titles[bookmark_id] = title
                if bookmark_id:
                    count += 1
                    context.bot.edit_message_text(
                        chat_id=message_saving.chat.id,
                        message_id=message_saving.message_id,
                        text=f"已保存({count}/{len(links)})…")
                else:
                    failed += 1

                if is_today(context.user_data['today']['date']):
                    context.user_data['today'][str(bookmark_id)] = {
                        'title': title,
                        'link': link
                    }
                else:
                    context.user_data['today'].clear()
                    context.user_data['today']['date'] = get_today()
                bot_persistence.flush()

            if count:
                failed_saving = f"另有 {failed} 篇未能保存。" if failed else ""
                msg = context.bot.edit_message_text(
                    chat_id=message_saving.chat.id,
                    message_id=message_saving.message_id,
                    text=f"成功保存 {count} 篇文章!\n" + failed_saving)
                del_msg.later(update, context, msg)
            else:
                msg = update.message.reply_text("未能成功保存 :(")
                del_msg.later(update, context, msg)

            # Return articles as preview messages
            for link in links:
                bookmark_id = link_ids[link]
                title = titles[bookmark_id]
                keyboard = [[
                    InlineKeyboardButton(
                        "🗑", callback_data=f'delete_{bookmark_id}'),
                    InlineKeyboardButton("💙",
                                         callback_data=f'like_{bookmark_id}')
                ]]
                markup = InlineKeyboardMarkup(keyboard)
                if can_iv(link):
                    rhash = supported_iv[can_iv(link)]
                    link = use_iv(rhash)
                context.bot.send_message(chat_id=update.message.chat_id,
                                         message_id=update.message.message_id,
                                         text=f"[{title}]({link})",
                                         reply_markup=markup,
                                         parse_mode='MARKDOWN')

    else:
        msg = update.message.reply_text('你还没有登入呢。\n前往:/start')
        del_msg.later(update, context, msg)
Exemple #9
0
def request_password(update, context):
    context.user_data[
        'username'] = update.message.text  # 记录使用者的 Instapaper 登录名(username)
    msg = update.message.reply_text('请输入密码:')
    del_msg.later(update, context, msg, timeup=120)
    return VERIFY