コード例 #1
0
def module(update: Update, context: CoreContext):
    datas = context.query.data.split(CALLBACK_SEPARATOR)
    # MODULE|module_id
    module_id = int(datas[1])
    cm = None
    cm = BaseCourse(context.moodle).get_course_module(module_id)
    context.query.answer()
    if not cm:
        context.query.edit_message_text("Data tidak ditemukan.")
        return -1

    course_module = cm.cm
    text = course_module.name + "\n"
    text += "<i>Fitur masih dikembangkan, tolong tekan tombol Buka di elearning</i>"
    buttons: List[InlineKeyboardButton] = list()

    url = MOODLE_D + f"mod/{course_module.modname}/view.php?id={module_id}"
    button = InlineKeyboardButton("Buka di elearning", url)
    buttons.append(button)

    data = make_data(
        course_module.modname.upper(), course_module.course, course_module.instance
    )
    button = InlineKeyboardButton(course_module.name, callback_data=data)
    buttons.append(button)

    back_data = make_data("CONTENT", course_module.course, course_module.section, 0)
    footer = [
        InlineKeyboardButton("< Kembali", callback_data=back_data),
        InlineKeyboardButton("Tutup ❌", callback_data="CLOSE"),
    ]
    keyboard = build_menu(buttons, footer_buttons=footer)

    context.query.edit_message_text(text, reply_markup=InlineKeyboardMarkup(keyboard))
    return -1
コード例 #2
0
ファイル: page.py プロジェクト: hexatester/ut-telegram-bot
def page(update: Update, context: CallbackContext):
    callback_query: CallbackQuery = update.callback_query
    if not callback_query or not callback_query.data:
        return -1
    callback_query.answer()

    # Validate data
    data = str(callback_query.data).split(CALLBACK_SEPARATOR)
    if len(data) == 7:
        _, subfolder, doc, end, current, txt, number = data
        end, current, number = int(end), int(current), int(number)
    else:
        _, subfolder, doc, end, current, txt = data
        end, current = int(end), int(current)
        number = current // 10

    # Number buttons
    a = 10 * number if 10 * number > 1 else 1
    b = 10 * (number + 1)
    keyboard = []
    limit = False
    for page_number in range(a, b):
        if page_number > end:
            limit = True
            break
        datas = f"MODUL,{subfolder},{doc},{end},{page_number},{txt}"
        datas = datas.replace(",", CALLBACK_SEPARATOR)
        keyboard.append(InlineKeyboardButton(str(page_number), callback_data=datas))

    # Header buttons
    header = []
    if number > 0:
        datas = f"PAGE,{subfolder},{doc},{end},{current},{txt},{number - 1}"
        datas = datas.replace(",", CALLBACK_SEPARATOR)
        header.append(InlineKeyboardButton("Sebelumnya", callback_data=datas))
    if not limit:
        datas = f"PAGE,{subfolder},{doc},{end},{current},{txt},{number + 1}"
        datas = datas.replace(",", CALLBACK_SEPARATOR)
        header.append(InlineKeyboardButton("Selanjutnya", callback_data=datas))

    # Footer buttons

    foooter = [
        InlineKeyboardButton(
            "< Kembali",
            callback_data=f"MODUL,{subfolder},{doc},{end},{current},{txt}".replace(
                ",", CALLBACK_SEPARATOR
            ),
        ),
        InlineKeyboardButton("Tutup ❌", callback_data="CLOSE"),
    ]

    menu = build_menu(
        buttons=keyboard,
        n_cols=3,
        header_buttons=header,
        footer_buttons=foooter,
    )
    callback_query.edit_message_reply_markup(InlineKeyboardMarkup(menu))
    return -1
コード例 #3
0
ファイル: course.py プロジェクト: hexatester/ut-telegram-bot
def course(update: Update, context: CoreContext):
    datas = context.query.data.split(CALLBACK_SEPARATOR)
    # COURSE|course_id
    course_id = int(datas[-1])

    moodle_course = BaseCourse(context.moodle)
    courses = moodle_course.get_courses_by_field("id", course_id)
    context.query.answer()
    if not courses:
        context.query.edit_message_text("Kursus tidak ditemukan.")
        return -1
    course_ = courses[0]

    buttons = list()
    sections = moodle_course.get_contents(course_.id)
    for section in sections:
        if not section.uservisible:
            continue
        data = make_data("CONTENT", course_id, section.id, 0)
        button = InlineKeyboardButton(section.name, callback_data=data)
        buttons.append(button)
    if not buttons:
        data = make_data("FORUMS", course_id)
        button = InlineKeyboardButton("Daftar Forum", callback_data=data)
        buttons.append(button)
    keyboard = build_menu(
        buttons,
        footer_buttons=InlineKeyboardButton("Tutup ❌", callback_data="CLOSE"),
    )
    context.query.edit_message_text(
        text=course_text(course_),
        reply_markup=InlineKeyboardMarkup(keyboard),
    )
    return -1
コード例 #4
0
def folder(update: Update, context: CoreContext):
    datas = context.query.data.split(CALLBACK_SEPARATOR)
    # FOLDER|course_id|folder_id
    course_id = int(datas[1])
    folder_id = int(datas[2])
    base_folder = BaseFolder(context.moodle)
    folders = base_folder.get_folders_by_courses([course_id])
    context.query.answer()
    if not folders:
        context.query.edit_message_text("Data tidak ditemukan!")
        return -1
    for folder in folders:
        if folder.id == folder_id:
            break
    buttons = list()
    for file in folder.introfiles:
        if file.isexternalfile:
            button = InlineKeyboardButton(file.filename, file.fileurl)
        else:
            button = InlineKeyboardButton(
                file.filename,
                switch_inline_query_current_chat=file.fileurl,
            )
        buttons.append(button)
    text = folder.name + "\n\n"
    text += clean_html(folder.intro, **BLEACH_CONFIG)
    keyboard = build_menu(buttons)
    context.query.edit_message_text(
        text, reply_markup=InlineKeyboardMarkup(keyboard))
    return -1
コード例 #5
0
def modul(context: CallbackContext, chat_id: int, message_id: int, data: str):
    modul_, page = Modul.from_data(data)
    txt = data.endswith("txt")
    keyboard: List[InlineKeyboardButton] = []
    if page > 1:
        keyboard.append(
            InlineKeyboardButton(
                "❮❮ Halaman Sebelumnya",
                callback_data=modul_.callback_data(page - 1, txt=txt),
            )
        )
    if page < modul_.end:
        keyboard.append(
            InlineKeyboardButton(
                "Halaman Selanjutnya ❯❯",
                callback_data=modul_.callback_data(page + 1, txt=txt),
            )
        )
    footer = []
    footer.append(
        InlineKeyboardButton(
            f"▲ Ke modul? ({modul_.doc})", callback_data=f"BUKU|{modul_.subfolder}"
        )
    )
    footer.append(InlineKeyboardButton("Tutup ❌", callback_data="CLOSE"))
    header = []
    header.append(
        InlineKeyboardButton(
            f"⬌ Ke halaman? ({page})",
            callback_data=modul_.callback_data(page, "PAGE", txt=txt),
        )
    )
    # Switch text or image
    if txt:
        header.append(
            InlineKeyboardButton(
                "Versi Gambar 📖", callback_data=modul_.callback_data(page, txt=False)
            )
        )
    else:
        header.append(
            InlineKeyboardButton(
                "Versi Teks 📄", callback_data=modul_.callback_data(page)
            )
        )
    menu = build_menu(
        buttons=keyboard,
        n_cols=2,
        header_buttons=header,
        footer_buttons=footer,
    )
    text = modul_.get_page_text(page) if txt else modul_.message_page(page)
    context.bot.edit_message_text(
        text,
        chat_id,
        message_id,
        reply_markup=InlineKeyboardMarkup(menu),
        disable_web_page_preview=txt,
    )
    return -1
コード例 #6
0
def url(update: Update, context: CoreContext):
    datas = context.query.data.split(CALLBACK_SEPARATOR)
    # URL|course_id|url_id
    course_id = int(datas[1])
    url_id = int(datas[2])
    base_url = BaseUrl(context.moodle)
    urls = base_url.get_urls_by_courses([course_id])
    context.query.answer()
    url_ = None
    for url in urls.urls:
        if url.id == url_id:
            url_ = url
            break
    if not url_ or url_.id != url_id:
        return -1

    buttons = list()
    name = unquote(url_.name)
    disable_preview = True
    if url_.externalurl:
        disable_preview = False
        button = InlineKeyboardButton(name, url_.externalurl)
        buttons.append(button)
        name = format_html.href(name, url_.externalurl)

    text = name + "\n"
    text += clean_html(url_.intro, **BLEACH_CONFIG)

    for file in url_.introfiles:
        url = file.fileurl
        if not file.isexternalfile:
            url += "?token=" + context.moodle.token
        button = InlineKeyboardButton(file.filename, url)
        buttons.append(button)

    # TODO : Use CONTENT
    back_button = make_data("COURSE", course_id)
    footer = [
        InlineKeyboardButton("< Kembali", callback_data=back_button),
        InlineKeyboardButton("Tutup ❌", callback_data="CLOSE"),
    ]

    keyboard = build_menu(buttons, footer_buttons=footer)
    context.chat.send_message(
        text,
        reply_markup=InlineKeyboardMarkup(keyboard),
        disable_web_page_preview=disable_preview,
    )
    return -1
コード例 #7
0
def discussions(update: Update, context: CoreContext):
    datas = context.query.data.split(CALLBACK_SEPARATOR)
    # DISCUSSIONS|course_id|forum_id|page
    course_id = int(datas[1])
    forum_id = int(datas[2])
    page = int(datas[3])

    base_forum = BaseForum(context.moodle)
    discussions = base_forum.get_forum_discussions(
        forumid=forum_id,
        sortorder=1,
        page=page,
    )
    context.query.answer()

    buttons = list()
    for discussion in discussions:
        name = discussion.name
        text = name[:30]
        if len(name) > 30:
            text += "..."
        text += f" by {discussion.userfullname}"
        data = make_data("DISCUSSION", course_id, forum_id, discussion.id, 0)
        button = InlineKeyboardButton(text, callback_data=data)
        buttons.append(button)
    header = list()
    if page > 1:
        data = make_data("DISCUSSIONS", course_id, forum_id, page - 1)
        button = InlineKeyboardButton("Sebelumnya", callback_data=data)
        header.append(button)
    if len(buttons) >= 9:
        data = make_data("DISCUSSIONS", course_id, forum_id, page + 1)
        button = InlineKeyboardButton("Selanjutnya", callback_data=data)
        header.append(button)
    back_data = make_data("FORUM", course_id, forum_id)
    footer = [
        InlineKeyboardButton("< Kembali", callback_data=back_data),
        InlineKeyboardButton("Tutup ❌", callback_data="CLOSE"),
    ]
    keyboard = build_menu(buttons,
                          header_buttons=header,
                          footer_buttons=footer)

    context.query.edit_message_text(
        "Daftar diskusi", reply_markup=InlineKeyboardMarkup(keyboard))
    return -1
コード例 #8
0
def discussion(update: Update, context: CoreContext):
    datas = context.query.data.split(CALLBACK_SEPARATOR)
    # DISCUSSION|course_id|forum_id|discussion_id|page
    course_id = int(datas[1])
    forum_id = int(datas[2])
    discussion_id = int(datas[3])
    page = int(datas[4])

    base_forum = BaseForum(context.moodle)
    posts = []
    posts = base_forum.get_discussion_posts(discussion_id)
    context.query.answer()
    text = "Diskusi"
    for post in posts:
        if post.isdeleted:
            continue
        subject = post.replysubject or post.subject
        title = f"{subject} [{post.author.fullname}]"
        url = f"https://elearning.ut.ac.id/mod/forum/discuss.php?d={post.id}"
        text += format_html.href(title, url) + "\n"
        text += clean_html(post.message, **BLEACH_CONFIG) + "\n\n"
    header = list()
    if len(text) > MAX_MESSAGE_LENGTH:
        MESSAGE_LENGTH = len(text)
        if page > 0:
            data = make_data("DISCUSSION", course_id, forum_id, discussion_id,
                             page - 1)
            button = InlineKeyboardButton("Sebelumnya", callback_data=data)
            header.append(button)
        if MESSAGE_LENGTH >= (page + 1) * MAX_MESSAGE_LENGTH:
            data = make_data("DISCUSSION", course_id, forum_id, discussion_id,
                             page + 1)
            button = InlineKeyboardButton("Selanjutnya", callback_data=data)
            header.append(button)
    back_data = make_data("DISCUSSIONS", course_id, forum_id, 1)
    footer = [
        InlineKeyboardButton("< Kembali", callback_data=back_data),
        InlineKeyboardButton("Tutup ❌", callback_data="CLOSE"),
    ]
    keyboard = build_menu(header_buttons=header, footer_buttons=footer)

    context.query.edit_message_text(
        text, reply_markup=InlineKeyboardMarkup(keyboard))
    return -1
コード例 #9
0
ファイル: forum.py プロジェクト: hexatester/ut-telegram-bot
def forum(update: Update, context: CoreContext):
    datas = context.query.data.split(CALLBACK_SEPARATOR)
    # FORUM|course_id|forum_id
    course_id = int(datas[1])
    base_forum = BaseForum(context.moodle)
    forums = base_forum.get_forums_by_courses([course_id])
    context.query.answer()
    if not forums:
        context.query.edit_message_text("Forum tidak ditemukan.")
        return -1
    forum_id = int(datas[2])
    for fo in forums:
        if fo.id == forum_id:
            base_forum.view_forum(fo.id)
            break
    buttons = list()
    if fo.numdiscussions and fo.numdiscussions > 0:
        data = make_data("DISCUSSIONS", course_id, forum_id, 1)
        button = InlineKeyboardButton("Diskusi", callback_data=data)
        buttons.append(button)
    text = forum_text(fo)
    header = InlineKeyboardButton(
        fo.name,
        url=f"https://elearning.ut.ac.id/mod/forum/view.php?id={fo.cmid}")
    back_data = make_data("COURSE", course_id)
    footer = [
        InlineKeyboardButton("< Kembali", callback_data=back_data),
        InlineKeyboardButton("Tutup ❌", callback_data="CLOSE"),
    ]
    keyboard = build_menu(
        buttons,
        header_buttons=header,
        footer_buttons=footer,
    )

    context.query.edit_message_text(
        text, reply_markup=InlineKeyboardMarkup(keyboard))
    return -1
コード例 #10
0
ファイル: courses.py プロジェクト: hexatester/ut-telegram-bot
def courses(update: Update, context: CoreContext):
    message = context.message.reply_text("Mendapatkan kursus...")
    message = resolve(message, Message)
    try:
        courses = BaseCourse(
            context.moodle).get_enrolled_courses_by_timeline_classification(
                "all")
    except ConnectionError:
        message.edit_text(
            "Elearning tidak merespon, coba beberapa saat lagi...")
        return -1
    except Exception as e:
        message.edit_text("Gagal mendapatkan kursus.")
        raise e
    if not courses:
        message.edit_text("Tidak ada kursus yang sedang diikuti.")
        return -1
    buttons: List[InlineKeyboardButton] = list()
    for course in courses:
        name = course.fullname or course.shortname
        text = name[:30]
        if len(name) > 30:
            text += "..."
        if course.progress:
            text += f" ({course.progress}%)"
        data = make_data("COURSE", course.id)
        button = InlineKeyboardButton(text, callback_data=data)
        buttons.append(button)
    keyboard = build_menu(
        buttons=buttons,
        footer_buttons=InlineKeyboardButton("Tutup ❌", callback_data="CLOSE"),
    )
    message.edit_text(
        "Daftar kursus yang diikuti",
        reply_markup=InlineKeyboardMarkup(keyboard),
    )
    return -1