S
S
sunekosuri2021-04-20 09:33:25
Python
sunekosuri, 2021-04-20 09:33:25

How can I make a function run if the previous one is executed?

I have two functions with mailing. One sends a photo, and the other tex. Now when you send a photo to a dialogue with the bot, it will automatically be shared with everyone. How to make it so that the photo is sent only if the function with sending text is completed or is being performed.

#mailing 
@bot.message_handler(commands=['news'])
def hendler_newsletter(message):

  connect = sqlite3.connect('admins.db')
  cursor = connect.cursor()
  admin = [x[0] for x in cursor.execute('SELECT * FROM admins_id ORDER BY id') if x[0] == message.chat.id]
  connect.commit()
  if admin:		

    msg = bot.send_message(message.chat.id, "Что вы хотите отправить всем пользователям бота ?")
    bot.register_next_step_handler(msg, mailing)

def mailing(message):
  # рассылка текста 
  connect = sqlite3.connect('admins.db')
  cursor = connect.cursor()

  admin = [x[0] for x in cursor.execute('SELECT * FROM admins_id ORDER BY id') if x[0] == message.chat.id]
  connect.commit()
  
  if admin:
    connect = sqlite3.connect('users.db')
    cursor = connect.cursor()
    for user in cursor.execute('SELECT * FROM login_id ORDER BY id'):
      bot.send_message(user[0],message.text)
    connect.commit()

    kb = types.ReplyKeyboardMarkup(True)
    kb.row('✅Да', '❌Нет')

  bot.send_message(message.chat.id,"Прикрепить фото ?" , reply_markup=kb)

  if message.text == '✅Да':
    bot.register_next_step_handler(msg, handle_docs_photo)
  if message.text == '❌Нет':
    bot.register_next_step_handler(msg, hendler_keyboard)	
      

@bot.message_handler(content_types=['photo'])
def handle_docs_photo(message):
  # рассылка фото
  connect = sqlite3.connect('admins.db')
  cursor = connect.cursor()
  admin = [x[0] for x in cursor.execute('SELECT * FROM admins_id ORDER BY id') if x[0] == message.chat.id]
  connect.commit()
  if admin:
    file_info = bot.get_file(message.photo[len(message.photo)-1].file_id)
    downloaded_file = bot.download_file(file_info.file_path)

    src='C:/Python/Bot/'+file_info.file_path;
    with open(src, 'wb') as new_file:
      new_file.write(downloaded_file)

    connect = sqlite3.connect('users.db')
    cursor = connect.cursor()
    for user in cursor.execute('SELECT * FROM login_id ORDER BY id'):
      bot.send_photo(user[0], open(src, 'rb'))

    msg = bot.send_message(message.chat.id, "Рассылка окончена")
    bot.register_next_step_handler(msg, hendler_keyboard)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dmshar, 2021-04-20
@dmshar

What, do you have multiprocessing? In Python, as in any other programming language, if you don't make any special effort and call two functions in sequence, the second one will be executed after the first one finishes.
Well, call your photo sending function after calling the text sending function.

S
Sadatake, 2021-04-20
@Sadatake

And what's the problem with inserting a function call at the end of the last one? If the second one needs to be called in 100% of cases after the first one is executed, then why not?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question