def test_equality(self): a = KeyboardButtonPollType(Poll.QUIZ) b = KeyboardButtonPollType(Poll.QUIZ) c = KeyboardButtonPollType(Poll.REGULAR) assert a == b assert hash(a) == hash(b) assert a is not b assert a != c assert hash(a) != hash(c)
def preview(update: Update, _: CallbackContext) -> None: """Ask user to create a poll and display a preview of it""" # using this without a type lets the user chooses what he wants (quiz or poll) button = [[KeyboardButton("Press me!", request_poll=KeyboardButtonPollType())]] message = "Press the button to let the bot generate a preview for your poll" # using one_time_keyboard to hide the keyboard update.effective_message.reply_text( message, reply_markup=ReplyKeyboardMarkup(button, one_time_keyboard=True) )
def poll_input(update, context): '''show button for poll creation''' button = [[ KeyboardButton("Create a Poll", request_poll=KeyboardButtonPollType()) ]] message = "Press the button to create a poll." update.effective_message.reply_text(message, reply_markup=ReplyKeyboardMarkup( button, one_time_keyboard=True)) return POLL
def preview(update: Update, context: CallbackContext) -> None: """Просим пользователя создать опрос и отобразить его предварительный просмотр""" try: button = [[ KeyboardButton("Нажми для создания опроса!", request_poll=KeyboardButtonPollType()) ]] message = "Нажмите кнопку, чтобы бот сгенерировал предварительный просмотр вашего опроса" update.effective_message.reply_text( message, reply_markup=ReplyKeyboardMarkup(button, one_time_keyboard=True)) except: message = "Опрос можно запросить только в приватных чатах" update.effective_message.reply_text(message)
def vote_intent(context, message): """Handle the intent to create a poll Args: context (Context): the Telegram context object message (Message): the Telegram message object """ if message.chat.type != Chat.PRIVATE: chat_id = message.from_user.id context.user_data[consts.VOTE] = message.chat.id message.reply_text("I've messaged you privately to create a poll.") else: chat_id = message.chat.id keyboard = [ [ KeyboardButton( "Create poll", request_poll=KeyboardButtonPollType(type=Poll.REGULAR) ) ] ] reply_markup = ReplyKeyboardMarkup( keyboard, one_time_keyboard=True, selective=True, resize_keyboard=True ) try: context.bot.send_message( chat_id, "You can press the button below to create your own poll", reply_markup=reply_markup, ) except Unauthorized: message.reply_text( "I couldn't message your privately, " f"please start a chat with @{context.bot.username}" )
class TestKeyboardButton: text = "text" request_location = True request_contact = True request_poll = KeyboardButtonPollType("quiz") web_app = WebAppInfo(url="https://example.com") def test_slot_behaviour(self, keyboard_button, mro_slots): inst = keyboard_button for attr in inst.__slots__: assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'" assert len(mro_slots(inst)) == len(set( mro_slots(inst))), "duplicate slot" def test_expected_values(self, keyboard_button): assert keyboard_button.text == self.text assert keyboard_button.request_location == self.request_location assert keyboard_button.request_contact == self.request_contact assert keyboard_button.request_poll == self.request_poll assert keyboard_button.web_app == self.web_app def test_to_dict(self, keyboard_button): keyboard_button_dict = keyboard_button.to_dict() assert isinstance(keyboard_button_dict, dict) assert keyboard_button_dict["text"] == keyboard_button.text assert keyboard_button_dict[ "request_location"] == keyboard_button.request_location assert keyboard_button_dict[ "request_contact"] == keyboard_button.request_contact assert keyboard_button_dict[ "request_poll"] == keyboard_button.request_poll.to_dict() assert keyboard_button_dict[ "web_app"] == keyboard_button.web_app.to_dict() def test_de_json(self, bot): json_dict = { "text": self.text, "request_location": self.request_location, "request_contact": self.request_contact, "request_poll": self.request_poll.to_dict(), "web_app": self.web_app.to_dict(), } inline_keyboard_button = KeyboardButton.de_json(json_dict, None) assert inline_keyboard_button.text == self.text assert inline_keyboard_button.request_location == self.request_location assert inline_keyboard_button.request_contact == self.request_contact assert inline_keyboard_button.request_poll == self.request_poll assert inline_keyboard_button.web_app == self.web_app none = KeyboardButton.de_json({}, None) assert none is None def test_equality(self): a = KeyboardButton("test", request_contact=True) b = KeyboardButton("test", request_contact=True) c = KeyboardButton("Test", request_location=True) d = KeyboardButton("Test", web_app=WebAppInfo(url="https://ptb.org")) e = InlineKeyboardButton("test", callback_data="test") assert a == b assert hash(a) == hash(b) assert a != c assert hash(a) != hash(c) assert a != d assert hash(a) != hash(d) assert a != e assert hash(a) != hash(e)
def keyboard_button_poll_type(): return KeyboardButtonPollType(TestKeyboardButtonPollType.type)