Exemplo n.º 1
0
    def test_equality(self):
        a = InputVenueMessageContent(123, 456, 'title', 'address')
        b = InputVenueMessageContent(123, 456, 'title', '')
        c = InputVenueMessageContent(123,
                                     456,
                                     'title',
                                     'address',
                                     foursquare_id=123)
        d = InputVenueMessageContent(456,
                                     123,
                                     'title',
                                     'address',
                                     foursquare_id=123)
        e = Location(123, 456)

        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 de_json(data):
        data = super(InputMessageContent, InputMessageContent).de_json(data)

        if not data:
            return None

        try:
            from telegram import InputTextMessageContent
            return InputTextMessageContent.de_json(data)
        except TypeError:
            pass

        try:
            from telegram import InputLocationMessageContent
            return InputLocationMessageContent.de_json(data)
        except TypeError:
            pass

        try:
            from telegram import InputVenueMessageContent
            return InputVenueMessageContent.de_json(data)
        except TypeError:
            pass

        try:
            from telegram import InputContactMessageContent
            return InputContactMessageContent.de_json(data)
        except TypeError:
            pass

        return None
Exemplo n.º 3
0
    def de_json(data, bot):
        data = super(InputMessageContent, InputMessageContent).de_json(data, bot)

        if not data:
            return None

        try:
            from telegram import InputTextMessageContent
            return InputTextMessageContent.de_json(data, bot)
        except TypeError:
            pass

        try:
            from telegram import InputVenueMessageContent
            return InputVenueMessageContent.de_json(data, bot)
        except TypeError:
            pass

        try:
            from telegram import InputLocationMessageContent
            return InputLocationMessageContent.de_json(data, bot)
        except TypeError:
            pass

        try:
            from telegram import InputContactMessageContent
            return InputContactMessageContent.de_json(data, bot)
        except TypeError:
            pass

        return None
Exemplo n.º 4
0
def input_venue_message_content():
    return InputVenueMessageContent(
        TestInputVenueMessageContent.latitude,
        TestInputVenueMessageContent.longitude,
        TestInputVenueMessageContent.title,
        TestInputVenueMessageContent.address,
        foursquare_id=TestInputVenueMessageContent.foursquare_id)
Exemplo n.º 5
0
    def inline_query_callback(self, update, context):
        if update.effective_user.id not in whitelist:
            update.inline_query.answer(results=[])
            return

        message = InputVenueMessageContent(
            title=ADDRESS,
            address=ADDRESS_DETAILS,
            latitude=LATITUDE,
            longitude=LONGITUDE,
        )
        open_button = InlineKeyboardButton(
            text='Открыть дверь подъезда',
            callback_data=f'open|{int((datetime.now() + timedelta(days=1)).timestamp())}',
        )
        result_open_button = InlineQueryResultArticle(
            id='1',
            title='Кнопка на сутки',
            description=f'{ADDRESS}, открыть дверь',
            input_message_content=message,
            reply_markup=InlineKeyboardMarkup([[
                open_button
            ]])
        )
        open_button_week = InlineKeyboardButton(
            text='Открыть дверь подъезда',
            callback_data=f'open|{int((datetime.now() + timedelta(weeks=1)).timestamp())}',
        )
        result_open_button_week = InlineQueryResultArticle(
            id='2',
            title='Кнопка на неделю',
            description=f'{ADDRESS}, открыть дверь',
            input_message_content=message,
            reply_markup=InlineKeyboardMarkup([[
                open_button_week
            ]])
        )
        open_button_quarter = InlineKeyboardButton(
            text='Открыть дверь подъезда',
            callback_data=f'open|{int((datetime.now() + timedelta(weeks=13)).timestamp())}',
        )
        result_open_button_quarter = InlineQueryResultArticle(
            id='3',
            title='Кнопка на квартал',
            description=f'{ADDRESS}, открыть дверь',
            input_message_content=message,
            reply_markup=InlineKeyboardMarkup([[
                open_button_quarter
            ]])
        )
        update.inline_query.answer(
            results=[
                result_open_button,
                result_open_button_week,
                result_open_button_quarter,
            ],
            is_personal=True,
            cache_time=0,
        )
Exemplo n.º 6
0
    def test_de_json(self, json_dict, bot):
        input_venue_message_content_json = InputVenueMessageContent.de_json(
            json_dict, bot)

        assert input_venue_message_content_json.longitude == self.longitude
        assert input_venue_message_content_json.latitude == self.latitude
        assert input_venue_message_content_json.title == self.title
        assert input_venue_message_content_json.address == self.address
        assert input_venue_message_content_json.foursquare_id == self.foursquare_id
def input_venue_message_content():
    return InputVenueMessageContent(
        TestInputVenueMessageContent.latitude,
        TestInputVenueMessageContent.longitude,
        TestInputVenueMessageContent.title,
        TestInputVenueMessageContent.address,
        foursquare_id=TestInputVenueMessageContent.foursquare_id,
        foursquare_type=TestInputVenueMessageContent.foursquare_type,
        google_place_id=TestInputVenueMessageContent.google_place_id,
        google_place_type=TestInputVenueMessageContent.google_place_type,
    )
Exemplo n.º 8
0
def inline_caps(update, context):
    query = update.inline_query.query
    if not query:
        return

    inline_results = []

    if datetime.datetime.now().month != config["CURRENT_MONTH"]:
        config["CURRENT_MONTH"] = datetime.datetime.now().month
        config["NOF_API_QUERYS"] = 0

    if config["NOF_API_QUERYS"] > 2000:
        inline_results.append(
            InlineQueryResultArticle(
                id=query.upper(),
                title="API limit reached",
                input_message_content=InputTextMessageContent("API limit reached"),
            )
        )
    else:
        geocoding_results = get_coordinates(config["PLACES_API_KEY"], query)

        config["NOF_API_QUERYS"] += 1

        if len(geocoding_results) == 0:
            inline_results.append(
                InlineQueryResultArticle(
                    id=query.upper(),
                    title="No results",
                    input_message_content=InputTextMessageContent("No results"),
                )
            )
        else:
            for gr in geocoding_results:
                inline_results.append(
                    InlineQueryResultArticle(
                        id=query.upper(),
                        title=gr["formatted"],
                        input_message_content=InputVenueMessageContent(
                            title=gr["name"],
                            address=gr["formatted"],
                            latitude=gr["lat"],
                            longitude=gr["lng"],
                        ),
                    )
                )
    context.bot.answer_inline_query(update.inline_query.id, inline_results)
Exemplo n.º 9
0
def inlinequery(update, context):
    """Handle the inline query."""
    query = update.inline_query.query.strip()
    logger.info(query)

    if "," in query:
        if logic.is_coordinate_search(query):
            logger.info("is_coordinate_search True")
            lat, lon = query.split(",")
            status_code, title, address = logic.coordinate_search(
                lat, lon, api_url_base_reverse_geocode, api_key
            )

            logger.info(f"{lat} {lon}\n{status_code}\n{title}\n{address}")

            if status_code == 200:
                results = []
                results.append(
                    InlineQueryResultLocation(
                        type="location",
                        id="single_location",
                        latitude=float(lat),
                        longitude=float(lon),
                        live_period=60,
                        input_message_content=InputVenueMessageContent(
                            latitude=float(lat),
                            longitude=float(lon),
                            title="You've searched: " + query.capitalize(),
                            address=address,
                        ),
                        title=title,
                    )
                )

                context.bot.answerInlineQuery(update.inline_query.id, results)
                return
            elif status_code == 400:
                results = []
                results.append(
                    InlineQueryResultArticle(
                        id="single_location",
                        title=title,
                        input_message_content=InputTextMessageContent(title),
                    )
                )

                context.bot.answerInlineQuery(update.inline_query.id, results)
            else:
                results = []
                results.append(
                    InlineQueryResultArticle(
                        id="single_location",
                        title=title,
                        input_message_content=InputTextMessageContent(title),
                    )
                )

                context.bot.answerInlineQuery(update.inline_query.id, results)
    elif query:
        status_code, locations, _ = logic.get_locations(
            query, api_url_base_geocode, api_key
        )

        if status_code == 200:
            inline_result = []
            address_venue_str = ""
            title_str = ""
            for location in locations:
                address_venue_str = location.sub_division()
                title_str = f"{location.address}, {location.country}"

                if address_venue_str == "":
                    address_venue_str = location.address
                else:
                    title_str += f" ({address_venue_str})"

                inline_result.append(
                    InlineQueryResultLocation(
                        type="location",
                        id=location.location_id,
                        latitude=location.latitude,
                        longitude=location.longitude,
                        live_period=60,
                        input_message_content=InputVenueMessageContent(
                            latitude=location.latitude,
                            longitude=location.longitude,
                            title="You've searched: " + query.capitalize(),
                            address=address_venue_str,
                        ),
                        title=title_str,
                    )
                )

            context.bot.answerInlineQuery(update.inline_query.id, inline_result)
        elif status_code == 204:
            inline_result = []
            inline_result.append(
                InlineQueryResultArticle(
                    type="article",
                    id=str(uuid4()),
                    title="What are you searching?",
                    input_message_content=InputTextMessageContent(
                        "You need to add more infos in your inline query."
                    ),
                    thumb_url=image_url,
                )
            )

            context.bot.answerInlineQuery(update.inline_query.id, inline_result)
Exemplo n.º 10
0
def inlinequery(bot, update):
    """Handle the inline query."""
    query = update.inline_query.query.strip()
    logger.info(query)

    if "," in query:
        if is_coordinate_search(query):
            logger.info("is_coordinate_search True")
            lat, lon = query.split(",")
            status_code, title, address = coordinate_search(lat, lon)

            logger.info(f"{lat} {lon}\n{status_code}\n{title}\n{address}")

            if status_code == 200:
                results = []
                results.append(
                    InlineQueryResultLocation(
                        type="location",
                        id="single_location",
                        latitude=float(lat),
                        longitude=float(lon),
                        live_period=60,
                        input_message_content=InputVenueMessageContent(
                            latitude=float(lat),
                            longitude=float(lon),
                            title="You've searched: " + query.capitalize(),
                            address=address,
                        ),
                        title=title,
                    )
                )

                bot.answerInlineQuery(update.inline_query.id, results)
                return
            elif status_code == 400:
                results = []
                results.append(
                    # FIXME this classes are not even imported, why are they here?
                    InlineQueryResultArticle(
                        id="single_location",
                        title=title,
                        input_message_content=InputTextMessageContent(title),
                    )
                )

                bot.answerInlineQuery(update.inline_query.id, results)
            else:
                results = []
                results.append(
                    InlineQueryResultArticle(
                        id="single_location",
                        title=title,
                        input_message_content=InputTextMessageContent(title),
                    )
                )

                bot.answerInlineQuery(update.inline_query.id, results)
    elif query:
        ret = get_locations(query)

        if ret == 200:
            results = []
            address_venue_str = ""
            title_str = ""
            for location in locations:
                address_venue_str = location.subDivision()
                title_str = f"{location.address}, {location.country}"

                if address_venue_str == "":
                    address_venue_str = location.address
                else:
                    title_str += f" ({address_venue_str})"

                results.append(
                    InlineQueryResultLocation(
                        type="location",
                        id=location.id,
                        latitude=location.latitude,
                        longitude=location.longitude,
                        live_period=60,
                        input_message_content=InputVenueMessageContent(
                            latitude=location.latitude,
                            longitude=location.longitude,
                            title="You've searched: " + query.capitalize(),
                            address=address_venue_str,
                        ),
                        title=title_str,
                    )
                )

            bot.answerInlineQuery(update.inline_query.id, results)