S
S
System042022-03-14 19:17:29
Bots
System04, 2022-03-14 19:17:29

How to exclude yourself from the telebot loop?

What is the question ...
You need to exclude the person from the list who sent the message ( His message should be sent to all users except himself )
I wrote the following code, but, no matter how I change the exception, my message comes to itself:

results = db.selectuser(message.from_user.id)
  print(results)
  for result in results:
    if result != message.from_user.id: 
      try:
        print(result)
        bot.send_message(result[0], message.text)
        print('Сообщение отправлено')
      except:
        print('Сообщение не отправлено')
    else:
      bot.send_message(message.from_user.id, "Повідомлення відправлено!")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zagir Majidov, 2022-03-14
@System04

Well, learn how to use Python elementary things you don't know!

sended = 0
for result in results:
   if result == message.from_user.id: pass
   else:
      try:
         bot.send_message(result[0], message.text)
         sended+=1
      except Exception as e: print(f"[Error] {e}")
bot.send_message(message.from_user.id, f"Отправлено {sended} сообщений!")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question