We have a telegram bot with methods

@asyncio.coroutine def on_chat_message(msg): chat_id = msg['chat']['id'] command = msg['text'] full_name = msg['chat']['first_name'] + ' ' + msg['chat']['last_name'] username = msg['chat']['username'] print('User:', full_name, '\nNickname:', username, '\nGot command:', command) markup = ReplyKeyboardMarkup(keyboard=[ ['Check once'], ['Start autoinformer', 'Stop autoinformer'] ]) if command == '/start': yield from bot.sendMessage(chat_id, 'Choose option:', reply_markup=markup) elif command == 'Check once': yield from bot.sendMessage(chat_id, latest_build_number) elif command == 'Start autoinformer': stop_markup = ReplyKeyboardMarkup(keyboard=[['Stop autoinformer']], resize_keyboard=True) yield from bot.sendMessage(chat_id, //TODO, reply_markup=stop_markup) elif command == 'Stop autoinformer': yield from bot.sendMessage(chat_id, 'Got you.'//TODO, reply_markup=markup) def check_always(chat_id): global latest_build_number latest_build_number = check_build() print(latest_build_number) while 1: build_new = check_build() if latest_build_number == build_new: time.sleep(10) else: latest_build_number = build_new bot.sendMessage(chat_id, latest_build_number) 

How can I implement the ability to receive new messages to the on_chat_message (msg) method and, moreover, if the Start autoinformer command is received, start the check_always (chat_id) method until the auto automaker command is received?

  • You do not need all the code that you have lead. Instead, create a minimal (but complete) example that demonstrates the problem ( minimal reproducible example ) - jfs

2 answers 2

In fact, everything turned out to be simple:

 def on_chat_msg(msg): do_smth() def cycle(): while True: do_smth_else() th = threading.Thread(target=cycle, args=[], daemon=True) th.start() 
  • asyncio has other methods for doing several things at once. Care should be taken when creating new threads. - jfs

MODE = [0]

 @asyncio.coroutine def on_chat_message(msg): chat_id = msg['chat']['id'] command = msg['text'] full_name = msg['chat']['first_name'] + ' ' + msg['chat']['last_name'] username = msg['chat']['username'] print('User:', full_name, '\nNickname:', username, '\nGot command:', command) markup = ReplyKeyboardMarkup(keyboard=[ ['Check once'], ['Start autoinformer', 'Stop autoinformer'] ]) if command == '/start': yield from bot.sendMessage(chat_id, 'Choose option:', reply_markup=markup) elif command == 'Check once': yield from bot.sendMessage(chat_id, latest_build_number) elif command == 'Start autoinformer': stop_markup = ReplyKeyboardMarkup(keyboard=[['Stop autoinformer']], resize_keyboard=True) MODE[0] = 1 from threading import Thread Thread(target=check_always, args=(chat_id,)).start() yield from bot.sendMessage(chat_id, //TODO, reply_markup=stop_markup) elif command == 'Stop autoinformer': MODE[0] = 0 yield from bot.sendMessage(chat_id, 'Got you.'//TODO, reply_markup=markup) def check_always(chat_id): global latest_build_number latest_build_number = check_build() print(latest_build_number) while MODE[0]: build_new = check_build() if latest_build_number == build_new: time.sleep(10) else: latest_build_number = build_new bot.sendMessage(chat_id, latest_build_number) 

  • Try to write more detailed answers - jfs