예제 #1
0
def addSourceChannel(chat_id, update_id, cursor, user_id, connection):
    print("in")
    message = 'Write the invitational link from the channel where you want to get messages:'
    send_message(chat_id, message)
    chat_id, text, update_id = get_last_id_text(get_updates(update_id + 1))

    while text.lower() == 'Add Master Channel':
        chat_id, text, update_id = get_last_id_text(get_updates(update_id + 1))
        sleep(0.5)

    id_channel = 0

    if "https://t.me/joinchat/" in text:

        text_proc = text.rpartition('https://t.me/joinchat/')
        try:
            updates = client(ImportChatInviteRequest(text_proc[2]))
        except Exception as e:
            print(e)
            pass

        id_channel = client.get_entity(text).id

    elif "t.me/joinchat/" in text:

        text_proc = text.rpartition('t.me/joinchat/')
        try:
            updates = client(ImportChatInviteRequest(text_proc[2]))
        except Exception as e:
            print(e)
            pass

        id_channel = client.get_entity("https://" + text).id

    else:

        try:
            updates = client(ImportChatInviteRequest(text))
        except Exception as e:
            print(e)
            pass

        id_channel = client.get_entity("https://t.me/joinchat/" + text).id

    sql_insert_query = "INSERT IGNORE INTO `channel` (`entity`) VALUES (" + str(id_channel) + ")"
    cursor = connection.cursor()
    result = cursor.execute(sql_insert_query)

    sql_insert_query = "INSERT IGNORE INTO `channel_user` (`id_user`, `id_channel` ,`type`) VALUES (" + \
                       str(user_id) + ", " + str(id_channel) + ", 2 )"
    print(sql_insert_query)
    result = cursor.execute(sql_insert_query)
    connection.commit()
    print("Record inserted successfully into python_users table")
    connection.commit()
    message = "Added!"
    send_message(chat_id, message)
예제 #2
0
def viewChannel(chat_id, connection, cursor, user_id):
    message = '-----Master Channels-----'
    send_message(chat_id, message)
    sql_query = "select `id_channel` from `channel_user` where `id_user` = " + str(user_id) + " and `type` = 1"
    print(sql_query)
    cursor.execute(sql_query)
    record = cursor.fetchall()
    for row in record:
        entity = client.get_entity(row[0])
        msg = entity.title
        send_message(chat_id, msg)
    message = '-----Source Channels-----'
    send_message(chat_id, message)
    sql_query = "select `id_channel` from `channel_user` where `id_user` = " + str(user_id) + " and `type` = 2"
    cursor.execute(sql_query)
    record = cursor.fetchall()
    for row in record:
        entity = client.get_entity(row[0])
        msg = entity.title
        send_message(chat_id, msg)
예제 #3
0
def main():

    try:
        connection = mysql.connector.connect(host='localhost',
                                             database='crypto_db',
                                             user='******',
                                             password='******')
        if connection.is_connected():
            db_Info = connection.get_server_info()
            print("Connected to MySQL database... MySQL Server version on ", db_Info)
            cursor = connection.cursor()
            cursor.execute("select database();")
            record = cursor.fetchone()
            print("Your connected to - ", record)

        text = ''
        chat_id, text, update_id = get_last_id_text(get_updates())
        chat_id, text, update_id = start(chat_id)
        print('Started')

        while text.lower() != 'y':
            sleep(1)
            text = 'start'

            user_id = client.get_entity(text).id
            cursor = connection.cursor()

            sql_insert_query = "INSERT IGNORE INTO `user` (`entity`, `last_forward`) VALUES (" + str(user_id) + \
                               ", (select UNIX_TIMESTAMP()))"
            print(sql_insert_query)
            result = cursor.execute(sql_insert_query)
            connection.commit()

            menu(chat_id, text, update_id, cursor, user_id, connection)
            text = 'y'
            chat_id, text, update_id = get_last_id_text(get_updates())
            text = end(chat_id, text, update_id)

    except Error as e:
        print("Error while connecting to MySQL", e)
    finally:
        # closing database connection.
        if (connection.is_connected()):
            cursor.close()
            connection.close()
            print("MySQL connection is closed")
예제 #4
0
def delete_channel(connection, cursor, user_id):
    chat_id, text, update_id = get_last_id_text(get_updates())
    text = 'Select the channel you want to delete:'

    channels = []
    channels_name = {}

    sql_query = "select `id_channel` from `channel_user` where `id_user` = " + str(user_id)
    cursor.execute(sql_query)
    record = cursor.fetchall()

    for row in record:
        entity = client.get_entity(row[0])
        channels.append(entity.title)
        channels_name[entity.title.lower()] = row[0]

    keyboard = []
    for i in range(0, len(channels), 2):
        key = []
        key.append(channels[i].title())
        try:
            key.append(channels[i + 1].title())
        except:
            pass
        keyboard.append(key)

    reply_markup = {"keyboard": keyboard, "one_time_keyboard": True}
    send_message(chat_id, text, json.dumps(reply_markup))

    while text.lower() == 'select the channel you want to delete:':
        chat_id, text, update_id = get_last_id_text(get_updates(update_id + 1))
        sleep(0.5)
    x = channels_name.get(text.lower())

    sql_query = "DELETE FROM `crypto_db`.`channel_user` WHERE `id_channel` = " + str(x) + " and id_user = " +\
                str(user_id)
    cursor.execute(sql_query)

    connection.commit()
예제 #5
0
def crontab():
    try:
        connection = mysql.connector.connect(host='localhost',
                                             database='crypto_db',
                                             user='******',
                                             password='******')
        if connection.is_connected():
            db_Info = connection.get_server_info()
            cursor = connection.cursor()
            cursor.execute("select database();")
            record = cursor.fetchone()

        master = 0
        start = False
        new_last = None
        last_msg = None
        cursor = connection.cursor()
        sql_query = "select `entity` from `user`"
        cursor.execute(sql_query)
        record = cursor.fetchall()
        # For para user
        for row in record:
            sql_query = "select `start` from `user` where `entity` = " + str(
                row[0])
            cursor.execute(sql_query)
            record = cursor.fetchall()
            for row2 in record:
                start = row2[0]

            if start:
                print("Starting")
                sql_query = "select `last_forward` from `user` where `entity` = " + str(
                    row[0])
                cursor.execute(sql_query)
                record = cursor.fetchall()
                for row2 in record:
                    last_msg = row2[0]

                sql_query = "select `id_channel` from `channel_user` where `id_user` = " + str(
                    row[0]) + " and type = 1"
                cursor.execute(sql_query)
                record2 = cursor.fetchall()
                # For para channel master
                for row2 in record2:
                    master = row2[0]

                sql_query = "select `id_channel` from `channel_user` where `id_user` = " + str(
                    row[0]) + " and type = 2"
                cursor.execute(sql_query)
                record2 = cursor.fetchall()
                # For para channel source
                for row2 in record2:

                    # Check si hay filtro

                    sql_query = "SELECT text FROM crypto_db.filter where id_user = "******"1: " + str(row3[0]))
                            for message in client.iter_messages(
                                    row2[0], search=row3[0]):
                                if message.text:
                                    date_time = str(message.date)
                                    pattern = '%Y-%m-%d %H:%M:%S+00:00'
                                    epoch = int(
                                        time.mktime(
                                            time.strptime(date_time, pattern)))
                                    if epoch > last_msg:
                                        # print(message.sender_id, ':', message.text, " date: ", message.date)
                                        entity = client.get_entity(
                                            message.sender_id)
                                        final_msg = ("Message: \"" +
                                                     message.text +
                                                     "\"\nSender: " +
                                                     entity.first_name + " " +
                                                     entity.last_name +
                                                     "\nDate: " +
                                                     str(message.date))
                                        client.send_message(entity=master,
                                                            message=final_msg)
                                        if new_last:
                                            if epoch > new_last:
                                                new_last = epoch
                                        else:
                                            new_last = epoch
                    else:
                        # for para mensajes dentro del canal source sin filtro
                        for message in client.iter_messages(row2[0]):
                            if message.text:
                                date_time = str(message.date)
                                pattern = '%Y-%m-%d %H:%M:%S+00:00'
                                epoch = int(
                                    time.mktime(
                                        time.strptime(date_time, pattern)))
                                if epoch > last_msg:
                                    # print(message.sender_id, ':', message.text, " date: ", message.date)
                                    entity = client.get_entity(
                                        message.sender_id)
                                    final_msg = ("Message: \"" + message.text +
                                                 "\"\nSender: " +
                                                 entity.first_name + " " +
                                                 entity.last_name +
                                                 "\nDate: " +
                                                 str(message.date))
                                    client.send_message(entity=master,
                                                        message=final_msg)
                                    if new_last:
                                        if epoch > new_last:
                                            new_last = epoch
                                    else:
                                        new_last = epoch
                                        print("new_last 2: " + str(new_last))

                if new_last is not None:
                    print("Updating: " + str(new_last))
                    sql_select_query = "UPDATE user SET last_forward = " + str(
                        new_last) + " WHERE entity = " + str(row[0])
                    result = cursor.execute(sql_select_query)

    except Error as e:
        print("Error while connecting to MySQL", e)
    finally:
        if connection.is_connected():
            connection.commit()
            cursor.close()
            connection.close()