def empty_query(bot, update): user = user_manager.get_user(update.effective_user) user_lists = list_manager.get_lists_from_user(user.id) lang = get_lang(user.language_code) results = [] if user_lists: for list in user_lists: message, keyboard = _create_list_message(list, lang), _create_list_keyboard(list, lang) new_result = InlineQueryResultArticle(id=list.id, title=list.title, description=lang.get_text("send_existent"), reply_markup=InlineKeyboardMarkup(keyboard), input_message_content=InputTextMessageContent( message_text=message, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True)) results.append(new_result) else: message = lang.get_text("no_existent_meeting_message", bot_username=const.aux.BOT_USERNAME) results = [InlineQueryResultArticle(id="no_meeting", title=lang.get_text("no_existent_meeting_query_title"), description=lang.get_text("no_existent_meeting_query_description"), input_message_content=InputTextMessageContent( message_text=message, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True))] update.inline_query.answer(results, is_personal=True, cache_time=const.QUERY_CACHE_TIME, switch_pm_text=lang.get_text("donations_accepted"), switch_pm_parameter="donation")
def add_telegram_user(self, telegram_user): # Añade unusuario a la lista de participantes del grupo. Se le debe da una instancia de Telegram. user = user_manager.get_user(telegram_user) if user in self.user_list: return user self.user_list.append(user) return user
def get_current_user(self): user_id = self.get_secure_cookie('user') if user_id is None: return user_id = user_id.decode() if self.allow_nonactive: return user_id user = user_manager.get_user(user_id) if not user or not user['active']: return return user_id
def full_query(bot, update): query_split = update.inline_query.query.split("*") user = user_manager.get_user(update.effective_user) lang = get_lang(user.language_code) # If the query starts with id* this is a list search by id. if query_split[0] == "id": list_id = query_split[1] list = list_manager.get_list_by_id(list_id) if list: message, keyboard = _create_list_message(list, lang), _create_list_keyboard(list, lang) results = [InlineQueryResultArticle(id=list.id, title=list.title, description=lang.get_text("send_existent_meeting_query_description"), reply_markup=InlineKeyboardMarkup(keyboard), input_message_content=InputTextMessageContent( message_text=message, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True) )] else: message = lang.get_text("list_not_found_message") results = [InlineQueryResultArticle(id="not_found", title=lang.get_text("list_not_found_query_title"), description=lang.get_text("list_not_found_query_description"), input_message_content=InputTextMessageContent( message_text=message, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True ))] else: list_id = uuid4() keyboard = _create_list_keyboard(List, lang, list_id) results = [InlineQueryResultArticle(id=list_id, title=update.inline_query.query, description=lang.get_text("press_to_send"), reply_markup=InlineKeyboardMarkup(keyboard), input_message_content=InputTextMessageContent( message_text="*%s*" % update.inline_query.query, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True ))] update.inline_query.answer(results, is_personal=True, cache_time=const.QUERY_CACHE_TIME, switch_pm_text=lang.get_text("donations_accepted"), switch_pm_parameter="donation")
def start(bot, update, args): if not args: generic_message(bot, update, "start", bot_username=const.aux.BOT_USERNAME) else: # Send the complete list of votes for a particular option. list_id, vote_option = args[0].split("_") list = list_manager.get_list_by_id(list_id) user = user_manager.get_user(update.effective_user) lang = get_lang(user.language_code) text = _create_complete_list_message(list, vote_option, lang) keyboard = _create_complete_list_keyboard(list, lang) update.effective_message.reply_text(text, reply_markup=InlineKeyboardMarkup(keyboard), parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True)
def get_clist(bot, update): user = user_manager.get_user(update.effective_user) lang = get_lang(user.language_code) vote_option, list_id = update.callback_query.data.split("*")[1:3] list = list_manager.get_list_by_id(list_id) text = _create_complete_list_message(list, vote_option, lang) keyboard = _create_complete_list_keyboard(list, lang) try: update.effective_message.edit_text(text, reply_markup=InlineKeyboardMarkup(keyboard), parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True) except Exception as e: pass update.callback_query.answer()
def get(self): if self.get_argument('code', False): user0 = yield super( OrcidOAuth2LoginHandler, self).get_authenticated_user( redirect_uri=self.settings['orcid_oauth']['redirect_uri'], code=self.get_argument('code')) # auth code is expired if 'errorDesc' in user0: state = self._get_state() self.set_secure_cookie('openid_state', state) yield self.authorize_redirect(state) return orcid = user0['orcid'] user1 = yield super( OrcidOAuth2LoginHandler, self).get_read_public_access( redirect_uri=self.settings['orcid_oauth']['redirect_uri']) access_token = user1['access_token'] self.set_secure_cookie("orcid_access_token", access_token) bio = yield super(OrcidOAuth2LoginHandler, self).get_user_bio(orcid_id=orcid, access_token=access_token) profile = self.get_profile(json.loads(bio.decode('utf-8'))) email = self.get_profile_email(profile) user = user_manager.get_user(orcid) active = True if user is None: active = bool(email) user_manager.store_user(orcid, email, active=active, profile=profile) self.set_secure_cookie('user', orcid) if not active: self.redirect('enteremail') else: self.redirect('/') return state = self._get_state() self.set_secure_cookie('openid_state', state) yield self.authorize_redirect(state)
def cast_vote(bot, update): user = user_manager.get_user(update.effective_user) lang = get_lang(user.language_code) vote, list_id = update.callback_query.data.split("*")[1:3] list = list_manager.get_list_by_id(list_id) if list: conn = database.get_connection() # Check if the user has voted before this list cur = conn.execute("SELECT vote FROM user_votes WHERE user_id=? AND list_id=?", [user.id, list.id]) past_vote = cur.fetchone() if past_vote: # If it is a new vote type, we change the vote type. if past_vote[0] != vote: conn.execute("UPDATE user_votes SET vote=?, vote_timestamp=? WHERE user_id=? AND list_id=?", [vote, time(), user.id, list.id]) # If it is the same, we remove the vote else: conn.execute("DELETE FROM user_votes WHERE user_id=? AND list_id=?", [user.id, list.id]) else: conn.execute("INSERT INTO user_votes VALUES (?, ?, ?, ?)", [user.id, list.id, vote, time()]) conn.commit() conn.close() message, keyboard = _create_list_message(list, lang), _create_list_keyboard(list, lang) bot.edit_message_text(text=message, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True, reply_markup=InlineKeyboardMarkup(keyboard), inline_message_id=update.callback_query.inline_message_id) update.callback_query.answer("okay 👍") else: update.callback_query.answer(lang.get_text("warning_list_does_not_exist"), shot_alert=True) bot.edit_message_reply_markup(inline_message_id=update.callback_query.inline_message_id, reply_markup=InlineKeyboardMarkup([[]]))
def remove_telegram_user(self, telegram_user): user = user_manager.get_user(telegram_user) if user in self.user_list: self.user_list.remove(user)
def get_user(self): user_id = self.current_user if user_id: return user_manager.get_user(user_id)