def initialize_participants(job_queue: JobQueue): user_map = DataSet() try: db = sqlite3.connect('survey/participants.db') cursor = db.cursor() cursor.execute("SELECT * FROM participants ORDER BY (ID)") participants = cursor.fetchall() for row in participants: user = Participant(row[1], init=False) user.conditions_ = pickle.loads(row[2]) user.data_set_ = pickle.loads(row[0]) user.timezone_ = row[3] user.country_ = row[4] user.gender_ = row[5] user.language_ = row[6] user.question_ = row[7] user.age_ = row[8] user.day_ = row[9] user.q_idle_ = row[10] user.active_ = row[11] user.block_ = row[12] user.pointer_ = row[13] user_map.participants[row[1]] = user if user.language_ != '': q_set = user_map.return_question_set_by_language( user.language_) user.q_set_ = q_set if user.country_ != '' and user.timezone_ != '' and user.gender_ != '': user.set_next_block() next_day = user.set_next_block() if next_day is None and user.active_ and user.pointer_ > -1: finished(user, job_queue) continue element = user.next_block[2] day_offset = next_day - user.day_ time_t = calc_block_time(element["time"]) due = calc_delta_t(time_t, day_offset, user.timezone_) debug('QUEUE', 'next block in ' + str(due) + ' seconds. User: ' + str(user.chat_id_), log=True) new_job = Job(queue_next, due, repeat=False, context=[user, job_queue]) job_queue._put(new_job) except sqlite3.Error as error: print(error) return user_map
def load_jobs(dispatcher: Dispatcher, jq: JobQueue): now = time() for job in Job_model.all(): next_t, job = pickle.loads(job.job_blob) enabled = job._enabled removed = job._remove job._enabled = Event() job._remove = Event() if enabled: job._enabled.set() if removed: job._remove.set() next_t -= now jq._put(job, next_t) _add_job_to_chat_data(dispatcher, job)
def question_handler(bot: Bot, update: Update, user_map: DataSet, job_queue: JobQueue): try: # Get the user from the dict and its question_set (by language) user = user_map.participants[ update.message.chat_id] # type: Participant # Case for very first question. if user.question_ == -1: user.set_active(True) user.set_language(update.message.text) user.set_block(0) q_set = user_map.return_question_set_by_language(user.language_) user.q_set_ = q_set current_day = q_set[0]["day"] user.set_day(current_day) user.set_block(0) elif user.q_idle_: q_set = user.q_set_ # Get the matching question for the users answer. pointer = user.pointer_ d_prev = q_set[pointer] b_prev = d_prev["blocks"][user.block_] q_prev = b_prev["questions"][user.question_] if not valid_answer(q_prev, update.message.text, user): user.set_q_idle(True) return # Storing the answer and moving on the next question store_answer(user, update.message.text, q_prev, job_queue) user.set_q_idle(False) else: # User has send something without being asked a question. return except KeyError as error: print(error) return if not user.active_: return message, question = find_next_question(user) if question is not None: message = question["text"] q_keyboard = get_keyboard(question["choice"], user) try: bot.send_message(chat_id=user.chat_id_, text=message, reply_markup=q_keyboard) debug(flag="MSG", text=str(user.chat_id_) + ": " + message + "\n") except TelegramError as error: if error.message == 'Unauthorized': user.pause() user.set_q_idle(True) elif user.auto_queue_ is False: user.block_complete_ = True next_day = user.set_next_block() if next_day is None: finished(user, job_queue) return element = user.next_block[2] day_offset = next_day - user.day_ time_t = calc_block_time(element["time"]) due = calc_delta_t(time_t, day_offset, user.timezone_) debug('QUEUE', 'next block in ' + str(due) + ' seconds. User: ' + str(user.chat_id_), log=True) new_job = Job(queue_next, due, repeat=False, context=[user, job_queue]) user.job_ = new_job job_queue._put(new_job)