Exemplo n.º 1
0
def connect_button(update, context) -> str:
    query = update.callback_query
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]

    connect_match = re.match(r"connect\((.+?)\)", query.data)
    disconnect_match = query.data == "connect_disconnect"
    clear_match = query.data == "connect_clear"
    connect_close = query.data == "connect_close"

    if connect_match:
        target_chat = connect_match.group(1)
        getstatusadmin = context.bot.get_chat_member(target_chat, query.from_user.id)
        isadmin = getstatusadmin.status in ('administrator', 'creator')
        ismember = getstatusadmin.status in ('member')
        isallow = sql.allow_connect_to_chat(target_chat)
        if (isadmin) or (isallow and ismember) or (user.id in SUDO_USERS):
            connection_status = sql.connect(query.from_user.id, target_chat)
            if connection_status:
                conn_chat = dispatcher.bot.getChat(connected(context.bot, update, chat, user.id, need_admin=False))
                chat_name = conn_chat.title
                query.message.edit_text(languages.tl(update.effective_message, "Berhasil tersambung ke *{}*. Gunakan /connection untuk informasi perintah apa saja yang tersedia.").format(chat_name), parse_mode=ParseMode.MARKDOWN)
                sql.add_history_conn(user.id, str(conn_chat.id), chat_name)
            else:
                query.message.edit_text(languages.tl(update.effective_message, "Koneksi gagal!"))
        else:
            context.bot.answer_callback_query(query.id, languages.tl(update.effective_message, "Sambungan ke obrolan ini tidak diizinkan!"), show_alert=True)
    elif disconnect_match:
        disconnection_status = sql.disconnect(query.from_user.id)
        if disconnection_status:
           sql.disconnected_chat = query.message.edit_text(languages.tl(update.effective_message, "Terputus dari obrolan!"))
        else:
           context.bot.answer_callback_query(query.id, languages.tl(update.effective_message, "Anda tidak terkoneksi!"), show_alert=True)
    elif clear_match:
        sql.clear_history_conn(query.from_user.id)
        query.message.edit_text(languages.tl(update.effective_message, "Riwayat yang terhubung telah dihapus!"))
    elif connect_close:
        query.message.edit_text(languages.tl(update.effective_message, "Closed.\nTo open again, type /connect"))
    else:
        connect_chat(update, context)
Exemplo n.º 2
0
def connect_chat(update, context):
    chat = update.effective_chat
    user = update.effective_user
    args = context.args

    if update.effective_chat.type == 'private':
        if args and len(args) >= 1:
            try:
                connect_chat = int(args[0])
                getstatusadmin = context.bot.get_chat_member(
                    connect_chat, update.effective_message.from_user.id)
            except ValueError:
                try:
                    connect_chat = str(args[0])
                    get_chat = context.bot.getChat(connect_chat)
                    connect_chat = get_chat.id
                    getstatusadmin = context.bot.get_chat_member(
                        connect_chat, update.effective_message.from_user.id)
                except error.BadRequest:
                    send_message(
                        update.effective_message,
                        languages.tl(update.effective_message,
                                     "ID Obrolan tidak valid!"))
                    return
            except error.BadRequest:
                send_message(
                    update.effective_message,
                    languages.tl(update.effective_message,
                                 "ID Obrolan tidak valid!"))
                return
            isadmin = getstatusadmin.status in ('administrator', 'creator')
            ismember = getstatusadmin.status in ('member')
            isallow = sql.allow_connect_to_chat(connect_chat)
            if (isadmin) or (isallow and ismember) or (user.id in SUDO_USERS):
                connection_status = sql.connect(
                    update.effective_message.from_user.id, connect_chat)
                if connection_status:
                    conn_chat = dispatcher.bot.getChat(
                        connected(context.bot,
                                  update,
                                  chat,
                                  user.id,
                                  need_admin=False))
                    chat_name = conn_chat.title
                    send_message(
                        update.effective_message,
                        languages.tl(
                            update.effective_message,
                            "Berhasil tersambung ke *{}*. Gunakan /connection untuk informasi perintah apa saja yang tersedia."
                        ).format(chat_name),
                        parse_mode=ParseMode.MARKDOWN)
                    sql.add_history_conn(user.id, str(conn_chat.id), chat_name)
                    # send_message(update.effective_message, languages.tl(update.effective_message, "supportcmd"), parse_mode="markdown")
                else:
                    send_message(
                        update.effective_message,
                        languages.tl(update.effective_message,
                                     "Koneksi gagal!"))
            else:
                send_message(
                    update.effective_message,
                    languages.tl(update.effective_message,
                                 "Sambungan ke obrolan ini tidak diizinkan!"))
        else:
            gethistory = sql.get_history_conn(user.id)
            if gethistory:
                buttons = [
                    InlineKeyboardButton(text=languages.tl(
                        update.effective_message, "❎ Close button"),
                                         callback_data="connect_close"),
                    InlineKeyboardButton(text=languages.tl(
                        update.effective_message, "🧹 Hapus riwayat"),
                                         callback_data="connect_clear")
                ]
            else:
                buttons = []
            conn = connected(context.bot,
                             update,
                             chat,
                             user.id,
                             need_admin=False)
            if conn:
                connectedchat = dispatcher.bot.getChat(conn)
                text = languages.tl(
                    update.effective_message,
                    "Anda telah terkoneksi pada *{}* (`{}`)").format(
                        connectedchat.title, conn)
                buttons.append(
                    InlineKeyboardButton(text=languages.tl(
                        update.effective_message, "🔌 Putuskan sambungan"),
                                         callback_data="connect_disconnect"))
            else:
                text = languages.tl(
                    update.effective_message,
                    "Tulis ID obrolan atau tagnya untuk terhubung!")
            if gethistory:
                text += languages.tl(update.effective_message,
                                     "\n\n*Riwayat koneksi:*\n")
                text += languages.tl(update.effective_message,
                                     "╒═══「 *Info* 」\n")
                text += languages.tl(update.effective_message,
                                     "│  Sorted: `Newest`\n")
                text += "│\n"
                buttons = [buttons]
                for x in sorted(gethistory.keys(), reverse=True):
                    htime = time.strftime("%d/%m/%Y", time.localtime(x))
                    text += "╞═「 *{}* 」\n│   `{}`\n│   `{}`\n".format(
                        gethistory[x]['chat_name'], gethistory[x]['chat_id'],
                        htime)
                    text += "│\n"
                    buttons.append([
                        InlineKeyboardButton(
                            text=gethistory[x]['chat_name'],
                            callback_data="connect({})".format(
                                gethistory[x]['chat_id']))
                    ])
                text += "╘══「 Total {} Chats 」".format(
                    str(len(gethistory)) +
                    " (max)" if len(gethistory) == 5 else str(len(gethistory)))
                conn_hist = InlineKeyboardMarkup(buttons)
            elif buttons:
                conn_hist = InlineKeyboardMarkup([buttons])
            else:
                conn_hist = None
            send_message(update.effective_message,
                         text,
                         parse_mode="markdown",
                         reply_markup=conn_hist)

    else:
        getstatusadmin = context.bot.get_chat_member(
            chat.id, update.effective_message.from_user.id)
        isadmin = getstatusadmin.status in ('administrator', 'creator')
        ismember = getstatusadmin.status in ('member')
        isallow = sql.allow_connect_to_chat(chat.id)
        if (isadmin) or (isallow and ismember) or (user.id in SUDO_USERS):
            connection_status = sql.connect(
                update.effective_message.from_user.id, chat.id)
            if connection_status:
                chat_name = dispatcher.bot.getChat(chat.id).title
                send_message(
                    update.effective_message,
                    languages.tl(
                        update.effective_message,
                        "Berhasil tersambung ke *{}*.").format(chat_name),
                    parse_mode=ParseMode.MARKDOWN)
                try:
                    sql.add_history_conn(user.id, str(chat.id), chat_name)
                    context.bot.send_message(
                        update.effective_message.from_user.id,
                        languages.tl(
                            update.effective_message,
                            "Anda telah terhubung dengan *{}*. Gunakan /connection untuk informasi perintah apa saja yang tersedia."
                        ).format(chat_name),
                        parse_mode="markdown")
                except BadRequest:
                    pass
                except error.Unauthorized:
                    pass
            else:
                send_message(
                    update.effective_message,
                    languages.tl(update.effective_message, "Koneksi gagal!"))
        else:
            send_message(
                update.effective_message,
                languages.tl(update.effective_message,
                             "Sambungan ke obrolan ini tidak diizinkan!"))
Exemplo n.º 3
0
def connect_chat(bot, update, args):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    if update.effective_chat.type == 'private':
        if len(args) >= 1:
            try:
                connect_chat = int(args[0])
            except ValueError:
                update.effective_message.reply_text(
                    tld(chat.id, "common_err_invalid_chatid"))
                return
            if (bot.get_chat_member(
                    connect_chat, update.effective_message.from_user.id).status
                    in ('administrator', 'creator') or
                (sql.allow_connect_to_chat(connect_chat) is True)
                    and bot.get_chat_member(
                        connect_chat,
                        update.effective_message.from_user.id).status in
                ('member')) or (user.id in SUDO_USERS):

                connection_status = sql.connect(
                    update.effective_message.from_user.id, connect_chat)
                if connection_status:
                    chat_name = dispatcher.bot.getChat(
                        connected(bot, update, chat, user.id,
                                  need_admin=False)).title
                    update.effective_message.reply_text(
                        tld(chat.id, "connection_success").format(chat_name),
                        parse_mode=ParseMode.MARKDOWN)

                    if history := sql.get_history(user.id):
                        # Vars
                        if history.chat_id1:
                            history1 = int(history.chat_id1)
                        if history.chat_id2:
                            history2 = int(history.chat_id2)
                        if history.chat_id3:
                            history3 = int(history.chat_id3)
                        if history.updated:
                            number = history.updated

                        if number == 1 and connect_chat != history2 and connect_chat != history3:
                            history1 = connect_chat
                            number = 2
                        elif number == 2 and connect_chat != history1 and connect_chat != history3:
                            history2 = connect_chat
                            number = 3
                        elif number >= 3 and connect_chat != history2 and connect_chat != history1:
                            history3 = connect_chat
                            number = 1
                        else:
                            print("Error")

                        print(history.updated)
                        print(number)

                        sql.add_history(user.id, history1, history2, history3,
                                        number)
                    else:
                        sql.add_history(user.id, connect_chat, "0", "0", 2)
                    # Rebuild user's keyboard
                    keyboard(bot, update)

                else:
                    update.effective_message.reply_text(
                        tld(chat.id, "connection_fail"))
            else:
                update.effective_message.reply_text(
                    tld(chat.id, "connection_err_not_allowed"))
        else:
            update.effective_message.reply_text(
                tld(chat.id, "connection_err_no_chatid"))
            history = sql.get_history(user.id)
Exemplo n.º 4
0
            update.effective_message.reply_text(
                tld(chat.id, "connection_err_no_chatid"))
            history = sql.get_history(user.id)
            # print(history.user_id, history.chat_id1, history.chat_id2, history.chat_id3, history.updated)

    elif update.effective_chat.type == 'supergroup':
        connect_chat = chat.id
        if (bot.get_chat_member(connect_chat,
                                update.effective_message.from_user.id).status
                in ('administrator', 'creator') or
            (sql.allow_connect_to_chat(connect_chat) is True)
                and bot.get_chat_member(
                    connect_chat, update.effective_message.from_user.id).status
                in 'member') or (user.id in SUDO_USERS):

            connection_status = sql.connect(
                update.effective_message.from_user.id, connect_chat)
            if connection_status:
                update.effective_message.reply_text(
                    tld(chat.id, "connection_success").format(chat.id),
                    parse_mode=ParseMode.MARKDOWN)
            else:
                update.effective_message.reply_text(
                    tld(chat.id, "connection_fail"),
                    parse_mode=ParseMode.MARKDOWN)
        else:
            update.effective_message.reply_text(
                tld(chat.id, "common_err_no_admin"))

    else:
        update.effective_message.reply_text(tld(chat.id, "common_cmd_pm_only"))
Exemplo n.º 5
0
def connect_chat(bot, update, args):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    if update.effective_chat.type == 'private':
        if len(args) >= 1:
            try:
                connect_chat = int(args[0])
            except ValueError:
                update.effective_message.reply_text(
                    tld(chat.id, "Invalid Chat ID provided!"))
                return
            if (bot.get_chat_member(
                    connect_chat, update.effective_message.from_user.id).status
                    in ('administrator', 'creator') or
                (sql.allow_connect_to_chat(connect_chat) == True)
                    and bot.get_chat_member(
                        connect_chat,
                        update.effective_message.from_user.id).status in
                ('member')) or (user.id in SUDO_USERS):

                connection_status = sql.connect(
                    update.effective_message.from_user.id, connect_chat)
                if connection_status:
                    chat_name = dispatcher.bot.getChat(
                        connected(bot, update, chat, user.id,
                                  need_admin=False)).title
                    update.effective_message.reply_text(
                        tld(chat.id, "Successfully connected to *{}*").format(
                            chat_name),
                        parse_mode=ParseMode.MARKDOWN)

                    # Add chat to connection history
                    history = sql.get_history(user.id)
                    if history:
                        # Vars
                        if history.chat_id1:
                            history1 = int(history.chat_id1)
                        if history.chat_id2:
                            history2 = int(history.chat_id2)
                        if history.chat_id3:
                            history3 = int(history.chat_id3)
                        if history.updated:
                            number = history.updated

                        if number == 1 and connect_chat != history2 and connect_chat != history3:
                            history1 = connect_chat
                            number = 2
                        elif number == 2 and connect_chat != history1 and connect_chat != history3:
                            history2 = connect_chat
                            number = 3
                        elif number >= 3 and connect_chat != history2 and connect_chat != history1:
                            history3 = connect_chat
                            number = 1
                        else:
                            print("Error")

                        print(history.updated)
                        print(number)

                        sql.add_history(user.id, history1, history2, history3,
                                        number)
                        # print(history.user_id, history.chat_id1, history.chat_id2, history.chat_id3, history.updated)
                    else:
                        sql.add_history(user.id, connect_chat, "0", "0", 2)
                    # Rebuild user's keyboard
                    keyboard(bot, update)

                else:
                    update.effective_message.reply_text(
                        tld(chat.id, "Connection failed!"))
            else:
                update.effective_message.reply_text(
                    tld(chat.id, "Connections to this chat not allowed!"))
        else:
            update.effective_message.reply_text(
                tld(chat.id, "Input chat ID to connect!"))
            history = sql.get_history(user.id)
            # print(history.user_id, history.chat_id1, history.chat_id2, history.chat_id3, history.updated)

    elif update.effective_chat.type == 'supergroup':
        connect_chat = chat.id
        if (bot.get_chat_member(connect_chat,
                                update.effective_message.from_user.id).status
                in ('administrator', 'creator') or
            (sql.allow_connect_to_chat(connect_chat) == True)
                and bot.get_chat_member(
                    connect_chat, update.effective_message.from_user.id).status
                in 'member') or (user.id in SUDO_USERS):

            connection_status = sql.connect(
                update.effective_message.from_user.id, connect_chat)
            if connection_status:
                update.effective_message.reply_text(
                    tld(chat.id,
                        "Succesfully connected to *{}*").format(chat.id),
                    parse_mode=ParseMode.MARKDOWN)
            else:
                update.effective_message.reply_text(
                    tld(chat.id, "Failed to connect to *{}*").format(chat.id),
                    parse_mode=ParseMode.MARKDOWN)
        else:
            update.effective_message.reply_text(
                tld(chat.id, "You are not admin!"))

    else:
        update.effective_message.reply_text(
            tld(chat.id, "Usage is limited to PMs only!"))