英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论