def initialize_poll(session, user, chat): """Initialize a new poll and send the user the poll creation message. This function also prevents users from: - Having too many polls. - Creating a new poll, when they're already in the middle of creating another poll """ user.started = True # Early return, if the user owns too many polls if len(user.polls) > config["telegram"]["max_polls_per_user"]: chat.send_message( i18n.t("creation.too_many_polls", locale=user.locale, count=len(user.polls)) ) return # Early return, if the user is already in the middle of creating a poll if user.current_poll is not None and not user.current_poll.created: chat.send_message( i18n.t("creation.already_creating", locale=user.locale), reply_markup=get_cancel_creation_keyboard(user.current_poll), ) return poll = Poll.create(user, session) text = get_init_text(poll) keyboard = get_init_keyboard(poll) chat.send_message( text, parse_mode="markdown", reply_markup=keyboard, disable_web_page_preview=True, )
def open_init_text(message, poll): """Open the initial poll creation message.""" keyboard = get_init_keyboard(poll) message.edit_text( get_init_text(poll), parse_mode='markdown', reply_markup=keyboard, )
def change_poll_type(session, context, poll): """Change the vote type.""" if poll.created: return i18n.t('callback.poll_created', locale=context.user.locale) poll.poll_type = PollType(context.action).name keyboard = get_init_keyboard(poll) context.query.message.edit_text(get_init_text(poll), parse_mode='markdown', reply_markup=keyboard)
def toggle_results_visible(session, context, poll): """Change the results visible settings of a poll.""" if poll.created: return i18n.t('callback.poll_already_created', locale=context.user.locale) poll.results_visible = not poll.results_visible keyboard = get_init_keyboard(poll) context.query.message.edit_text(get_init_text(poll), parse_mode='markdown', reply_markup=keyboard) return i18n.t('callback.visibility_changed', locale=context.user.locale)
def toggle_anonymity(session, context, poll): """Change the anonymity settings of a poll.""" if poll.created: return i18n.t('callback.poll_already_created', locale=context.user.locale) poll.anonymous = not poll.anonymous keyboard = get_init_keyboard(poll) context.query.message.edit_text(get_init_text(poll), parse_mode='markdown', reply_markup=keyboard) return i18n.t('callback.anonymity_changed', locale=context.user.locale)
def open_init_text(message, poll: Poll): """Open the initial poll creation message.""" if poll.created_from_native: keyboard = get_native_poll_merged_keyboard(poll) message.edit_text( get_native_poll_merged_text(poll), parse_mode="markdown", reply_markup=keyboard, ) else: keyboard = get_init_keyboard(poll) message.edit_text(get_init_text(poll), parse_mode="markdown", reply_markup=keyboard)
def create_poll(bot, update, session, user): """Create a new poll.""" # The previous unfinished poll will be removed user.started = True if user.current_poll is not None and not user.current_poll.created: update.message.chat.send_message( i18n.t('creation.already_creating', locale=user.locale), reply_markup=get_cancel_creation_keyboard(user.current_poll)) return poll = Poll.create(user, session) text = get_init_text(poll) keyboard = get_init_keyboard(poll) update.message.chat.send_message( text, parse_mode='markdown', reply_markup=keyboard, disable_web_page_preview=True, )
def init_poll(session, context): """Start the creation of a new poll.""" user = context.user chat = context.query.message.chat if user.current_poll is not None and not user.current_poll.created: chat.send_message( i18n.t("creation.already_creating", locale=user.locale), reply_markup=get_cancel_creation_keyboard(user.current_poll), ) return poll = Poll.create(user, session) text = get_init_text(poll) keyboard = get_init_keyboard(poll) chat.send_message( text, parse_mode="markdown", reply_markup=keyboard, disable_web_page_preview=True, )
def create_poll(bot, update, session, user): """Create a new poll.""" # The previous unfinished poll will be removed user.started = True if user.current_poll is not None and not user.current_poll.created: update.message.chat.send_message( i18n.t('creation.already_creating', locale=user.locale), reply_markup=get_cancel_creation_keyboard(user.current_poll)) return poll = Poll(user) poll.european_date_format = user.european_date_format poll.locale = user.locale user.current_poll = poll user.expected_input = ExpectedInput.name.name session.add(poll) session.commit() text = get_init_text(poll) keyboard = get_init_keyboard(poll) update.message.chat.send_message(text, parse_mode='markdown', reply_markup=keyboard)