如何向多个聊天ID发送广播消息

huangapple go评论49阅读模式
英文:

How can I send a broadcast message to multiple chat ids

问题

I tried to send a message to all bot users through their CHAT_IDs but failed.

@bot.message_handler(commands=['cast'])
def send_cast(message):
    msg = message.text.split(None, 1)[1]
    user = message.from_user.id
    # start_db
    conn = connect_to_db()
    cursor = conn.cursor()
    db_users = "select user_id from UFM_USERS where type ='ORD'"
    cursor.execute(db_users)
    u = cursor.fetchall()
    users = [row[0] for row in u]
    conn.commit()
    # fetch users

users retrieve as 7667..., 4665..., 9877..., 8665..., 3566, 28579.., 5679..., 687... but again it doesn't send to the users.

    try:
        if user not in m.admin:
           bot.send_message(message.chat.id, text=f"You require admin permission to do this ‼️", parse_mode="Markdown")
        else:
            # bot.send_message(message.chat.id, f'_These are your users_\n{users}', parse_mode="Markdown")
            bot.send_message(chat_id=users, text=msg, parse_mode="Markdown")
    except Exception as e:
        bot.send_message(message.chat.id, f'*Oooops... Something went wrong.*', parse_mode="Markdown")

That's my code, but if it sends a message, it sends to one user, not all. How can I resolve it.

英文:

I tried to send a message to all bot users through their CHAT_IDs but failed.

@bot.message_handler(commands=['cast'])
def send_cast(message):
    msg = message.text.split(None,1)[1]
    user = message.from_user.id
    #start_db
    conn = connect_to_db()
    cursor = conn.cursor()
    db_users = "select user_id from UFM_USERS where type ='ORD'"
    cursor.execute(db_users)
    u = cursor.fetchall()
    users = [row[0] for row in u]
    conn.commit()
    #fetch users

users retrieve as 7667...,4665....,9877...,8665...,3566,28579..,5679...,687... but again it doesn't send to the users.

    try:
        if user not in m.admin:
           bot.send_message(message.chat.id,text=f"You require admin permission to do this ‼️",parse_mode = "Markdown")
        else:
            #bot.send_message(message.chat.id,f'_These are your users_\n{users}',parse_mode = "Markdown")
            bot.send_message(chat_id=users,text=msg,parse_mode = "Markdown")
    except Exception as e:
        bot.send_message(message.chat.id, 
                         f'*Oooops... Something went wrong.*',
                         parse_mode = "Markdown")

That's my code but if it's sends a massage, it sends to one user not all. How can I resolve it.

答案1

得分: 1

你指出 users 是一个列表。bot.send_message 接受一个 chat_id 作为值,而不是一个列表。因此,使用 for 循环可以解决这个问题:

for user in users:
    bot.send_message(chat_id=user, text=msg, parse_mode="Markdown")

另外,我建议阅读 telegram bot API 限制 以了解发送批量消息的限制。

英文:

You indicated that users is a list. bot.send_message takes one chat_id as a value not a list of them. So, for loop will solve this problem:

for user in users:
    bot.send_message(chat_id=user,text=msg,parse_mode = "Markdown")

In addition, I recommend reading telegram bot API limits for sending bulk messages.

huangapple
  • 本文由 发表于 2023年4月19日 15:36:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76051854.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定