如何向bot中的所有用户发送消息?

时间:2020-07-13 12:53:16

标签: python mysql tuples

我正在尝试向所有僵尸程序发送消息,但我无法获得ID的整数。我得到元组,相反! 代码:

def send_msg_all_to(message):
    try:
        connect = pymysql.connect('localhost', 'root', '', 'python')
        user_id = message.chat.id
        text[user_id] = str(message.text)
        if len(text[user_id]) < 2 or len(text[user_id]) >= 120:
            raise ExpException("Error")
    except ExpException as e:
        msg = bot.send_message(message.chat.id, e)
        bot.register_next_step_handler(msg, send_msg_all_to)
    except ValueError as e:
        msg = bot.send_message(message.chat.id, "Error")
        bot.register_next_step_handler(msg, send_msg_all_to)
    except pymysql.Error as e:
        print("error in send_msg_all_to()", e)
    else:
        cursor = connect.cursor()
        query = "SELECT usr_id FROM users"
        cursor.execute(query)
        users = cursor.fetchall()
        msg = bot.send_message(message.chat.id, "Your message" % (text[user_id]))
        bot.register_next_step_handler(msg, start) 
        for user in users:
            print(user)
            #bot.send_message(user, "Admin %s" % (text[user_id]))
    finally:
        connect.close() 

此刻我在db中有2个ID,这是我的输出:

(1020347173,)
(130513514,)

我只想获取不带元组的所有ID(例如int),并将它们用于bot.send_message()。你能帮我实现这个目标吗?

1 个答案:

答案 0 :(得分:0)

由于仅选择一列,因此您将获得具有单个元素的元组。

因此,您只需要访问元组的第一个元素,即user[0]

for user in users:
    print(user[0])
    bot.send_message(user[0], "Admin %s" % (text[user_id]))