def get_false_update_fixture_decorator_params(): message = Message(1, User(1, '', False), DATE, Chat(1, ''), text='test') params = [{ 'callback_query': CallbackQuery(1, User(1, '', False), 'chat', message=message) }, { 'channel_post': message }, { 'edited_channel_post': message }, { 'inline_query': InlineQuery(1, User(1, '', False), '', '') }, { 'chosen_inline_result': ChosenInlineResult('id', User(1, '', False), '') }, { 'shipping_query': ShippingQuery('id', User(1, '', False), '', None) }, { 'pre_checkout_query': PreCheckoutQuery('id', User(1, '', False), '', 0, '') }, { 'callback_query': CallbackQuery(1, User(1, '', False), 'chat') }] ids = tuple(key for kwargs in params for key in kwargs) return {'params': params, 'ids': ids}
def test_all_update_types(self, dp, bot, user1): handler = ConversationHandler( entry_points=[CommandHandler('start', self.start_end)], states={}, fallbacks=[]) message = Message(0, user1, None, self.group, text='ignore', bot=bot) callback_query = CallbackQuery(0, user1, None, message=message, data='data', bot=bot) chosen_inline_result = ChosenInlineResult(0, user1, 'query', bot=bot) inline_query = InlineQuery(0, user1, 'query', 0, bot=bot) pre_checkout_query = PreCheckoutQuery(0, user1, 'USD', 100, [], bot=bot) shipping_query = ShippingQuery(0, user1, [], None, bot=bot) assert not handler.check_update( Update(0, callback_query=callback_query)) assert not handler.check_update( Update(0, chosen_inline_result=chosen_inline_result)) assert not handler.check_update(Update(0, inline_query=inline_query)) assert not handler.check_update(Update(0, message=message)) assert not handler.check_update( Update(0, pre_checkout_query=pre_checkout_query)) assert not handler.check_update( Update(0, shipping_query=shipping_query))
def inline_query(bot): return InlineQuery(TestInlineQuery.id, TestInlineQuery.from_user, TestInlineQuery.query, TestInlineQuery.offset, location=TestInlineQuery.location, bot=bot)
async def test_context_pattern(self, app, inline_query): handler = InlineQueryHandler(self.callback_pattern, pattern=r"(?P<begin>.*)est(?P<end>.*)") app.add_handler(handler) async with app: await app.process_update(inline_query) assert self.test_flag app.remove_handler(handler) handler = InlineQueryHandler(self.callback_pattern, pattern=r"(t)est(.*)") app.add_handler(handler) await app.process_update(inline_query) assert self.test_flag update = Update(update_id=0, inline_query=InlineQuery(id="id", from_user=None, query="", offset="")) assert not handler.check_update(update) update.inline_query.query = "not_a_match" assert not handler.check_update(update)
def inline_query(bot): return Update(0, inline_query=InlineQuery('id', User(2, 'test user', False), 'test query', offset='22', location=Location( latitude=-23.691288, longitude=-46.788279)))
def test_equality(self): a = InlineQuery(self.id, User(1, ""), "", "") b = InlineQuery(self.id, User(1, ""), "", "") c = InlineQuery(self.id, User(0, ""), "", "") d = InlineQuery(0, User(1, ""), "", "") e = Update(self.id) assert a == b assert hash(a) == hash(b) assert a is not 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 test_equality(self): a = InlineQuery(self.id, User(1, '', False), '', '') b = InlineQuery(self.id, User(1, '', False), '', '') c = InlineQuery(self.id, User(0, '', False), '', '') d = InlineQuery(0, User(1, '', False), '', '') e = Update(self.id) assert a == b assert hash(a) == hash(b) assert a is not 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 inline_query(bot): return InlineQuery( TestInlineQuery.id_, TestInlineQuery.from_user, TestInlineQuery.query, TestInlineQuery.offset, location=TestInlineQuery.location, chat_type=TestInlineQuery.chat_type, bot=bot, )
def inline_query(bot): return Update( 0, inline_query=InlineQuery( "id", User(2, "test user", False), "test query", offset="22", location=Location(latitude=-23.691288, longitude=-46.788279), ), )
def get_inline_query(self, user=None, query=None, offset=None, location=None): """ Returns a telegram.Update object containing a inline_query. Parameters: location (Optional[telegram.Location or True]): simulates a location offset (Optional[str]): query (Optional[str]): user (Optional[telegram.User): If omitted will be randomly generated Returns: telegram.Update: an update containing a :py:class:`telegram.InlineQuery` """ if user: if not isinstance(user, User): raise BadUserException else: user = self.ug.get_user() if query: if not isinstance(query, str): raise AttributeError("query must be string") if offset: if not isinstance(offset, str): raise AttributeError("offset must be string") if location: if isinstance(location, Location): pass elif isinstance(location, bool): import random location = Location(random.uniform(-180, 180), random.uniform(-90, 90)) else: raise AttributeError( "Location must be either telegram.Location or True") return InlineQuery(self._gen_id(), from_user=user, query=query, offset=offset, location=location, bot=self.bot)
def create_inline_query(self, json_obj): query_id = self._get_simple_field(json_obj, 'id') from_user = self.get_from(json_obj) location = self.get_location(json_obj) query = self._get_simple_field(json_obj, 'query') offset = self._get_simple_field(json_obj, 'offset') inline_query = InlineQuery(id=query_id, from_user=from_user, query=query, offset=offset) if location: inline_query.location = location return inline_query
def test_regexGroupHandlerInlineQuery(self): self._setup_updater('', messages=0) d = self.updater.dispatcher handler = InlineQueryHandler( self.regexGroupHandlerTest, pattern='^(This).*?(?P<testgroup>regex group).*', pass_groupdict=True, pass_groups=True) d.add_handler(handler) queue = self.updater.start_polling(0.01) queue.put( Update( update_id=0, inline_query=InlineQuery( 0, None, 'This is a test message for regex group matching.', None))) sleep(.1) self.assertEqual(self.received_message, (('This', 'regex group'), { 'testgroup': 'regex group' }))
def test_regex_handler_without_message(self): self._setup_updater('Test3') d = self.updater.dispatcher handler = RegexHandler(r'Te.*', self.telegramHandlerTest) d.add_handler(handler) # message, no text m = Message(1, User(1, "testuser"), None, Chat(2, "private"), video="My_vid", caption="test ") d.process_update(Update(1, message=m)) self.assertEqual(self.message_count, 0) # no message c = InlineQuery(2, User(1, "testuser"), "my_query", offset=15) d.process_update(Update(2, inline_query=c)) self.assertEqual(self.message_count, 0)
def get_false_update_fixture_decorator_params(): message = Message(1, DATE, Chat(1, ""), from_user=User(1, "", False), text="test") params = [ { "callback_query": CallbackQuery(1, User(1, "", False), "chat", message=message) }, { "channel_post": message }, { "edited_channel_post": message }, { "inline_query": InlineQuery(1, User(1, "", False), "", "") }, { "chosen_inline_result": ChosenInlineResult("id", User(1, "", False), "") }, { "shipping_query": ShippingQuery("id", User(1, "", False), "", None) }, { "pre_checkout_query": PreCheckoutQuery("id", User(1, "", False), "", 0, "") }, { "callback_query": CallbackQuery(1, User(1, "", False), "chat") }, ] ids = tuple(key for kwargs in params for key in kwargs) return {"params": params, "ids": ids}
) from telegram.ext import Filters, MessageHandler, CallbackContext, JobQueue, UpdateFilter message = Message(1, None, Chat(1, ''), from_user=User(1, '', False), text='Text') params = [ { 'callback_query': CallbackQuery(1, User(1, '', False), 'chat', message=message) }, { 'inline_query': InlineQuery(1, User(1, '', False), '', '') }, { 'chosen_inline_result': ChosenInlineResult('id', User(1, '', False), '') }, { 'shipping_query': ShippingQuery('id', User(1, '', False), '', None) }, { 'pre_checkout_query': PreCheckoutQuery('id', User(1, '', False), '', 0, '') }, { 'callback_query': CallbackQuery(1, User(1, '', False), 'chat') },
}, { "edited_message": message }, { "callback_query": CallbackQuery(1, User(1, "", False), "chat", message=message) }, { "channel_post": message }, { "edited_channel_post": message }, { "inline_query": InlineQuery(1, User(1, "", False), "", "") }, { "chosen_inline_result": ChosenInlineResult("id", User(1, "", False), "") }, { "pre_checkout_query": PreCheckoutQuery("id", User(1, "", False), "", 0, "") }, { "callback_query": CallbackQuery(1, User(1, "", False), "chat") }, ] ids = (
def test_inline_query(patched_validate_token): patched_validate_token().return_value = True bot = Bot('token') test_user = User(1, is_bot=False, username='******', first_name='test') test_user_without_username = User(1, is_bot=False, first_name='test') result_list = [] def patch_answer_inline_query(query_id, results, cache_time): result_list.append(results) with mock.patch.object(Bot, 'answer_inline_query', side_effect=patch_answer_inline_query): # Modifier Query without Username query = TEST_QUERY_WITH_MODIFIER ilq = InlineQuery(1, test_user_without_username, '{0}'.format(query), 0) update = Update(0, inline_query=ilq) run.inlinequery(bot, update) received_results = result_list.pop() assert len(received_results) == 1 actual_response = received_results[0].input_message_content[ 'message_text'] expected_response = user_and_query_to_static_response( test_user_without_username, query) assert actual_response == expected_response # Modifier Query with Username query = TEST_QUERY_WITH_MODIFIER ilq = InlineQuery(1, test_user, '{0}'.format(query), 0) update = Update(0, inline_query=ilq) run.inlinequery(bot, update) received_results = result_list.pop() assert len(received_results) == 1 actual_response = received_results[0].input_message_content[ 'message_text'] expected_response = user_and_query_to_static_response(test_user, query) assert actual_response == expected_response # Non-Modifier Query query = TEST_QUERY_WITHOUT_MODIFIER ilq = InlineQuery(1, test_user, '{0}'.format(query), 0) update = Update(0, inline_query=ilq) run.inlinequery(bot, update) received_results = result_list.pop() assert len(received_results) == 3 actual_response = received_results[0].input_message_content[ 'message_text'] expected_response = user_and_query_to_static_response(test_user, query, advantage=True) assert actual_response == expected_response actual_response = received_results[1].input_message_content[ 'message_text'] expected_response = user_and_query_to_static_response( test_user, query, disadvantage=True) assert actual_response == expected_response actual_response = received_results[2].input_message_content[ 'message_text'] expected_response = user_and_query_to_static_response(test_user, query) assert actual_response == expected_response # Invalid Query query = TEST_QUERY_INVALID ilq = InlineQuery(1, test_user, '{0}'.format(query), 0) update = Update(0, inline_query=ilq) run.inlinequery(bot, update) received_results = result_list.pop() assert len(received_results) == 1 actual_response = received_results[0].input_message_content[ 'message_text'] expected_response = 'Query: {0}\n\n{1}'.format( query, run.INVALID_DICE_NOTATION_MSG) assert actual_response == expected_response
# You should have received a copy of the GNU Lesser Public License # along with this program. If not, see [http://www.gnu.org/licenses/]. import pytest from telegram import (Message, Update, Chat, Bot, User, CallbackQuery, InlineQuery, ChosenInlineResult, ShippingQuery, PreCheckoutQuery) from telegram.ext import CommandHandler, Filters message = Message(1, User(1, ''), None, Chat(1, ''), text='test') params = [ {'callback_query': CallbackQuery(1, User(1, ''), 'chat', message=message)}, {'channel_post': message}, {'edited_channel_post': message}, {'inline_query': InlineQuery(1, User(1, ''), '', '')}, {'chosen_inline_result': ChosenInlineResult('id', User(1, ''), '')}, {'shipping_query': ShippingQuery('id', User(1, ''), '', None)}, {'pre_checkout_query': PreCheckoutQuery('id', User(1, ''), '', 0, '')}, {'callback_query': CallbackQuery(1, User(1, ''), 'chat')} ] ids = ('callback_query', 'channel_post', 'edited_channel_post', 'inline_query', 'chosen_inline_result', 'shipping_query', 'pre_checkout_query', 'callback_query_without_message',) @pytest.fixture(scope='class', params=params, ids=ids) def false_update(request): return Update(update_id=1, **request.param)
def test_no_chat(self, chat_creator_role, update): update.message = None update.inline_query = InlineQuery(1, User(0, 'TestUser', False), 'query', 0) assert not chat_creator_role(update)