-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from benyaming/aoigram3
Aoigram v3
- Loading branch information
Showing
24 changed files
with
541 additions
and
1,474 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,58 @@ | ||
from __future__ import annotations | ||
|
||
from datetime import datetime | ||
from typing import List, Optional | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
|
||
__all__ = ['Stop', 'Route', 'IncomingRoute', 'IncomingRoutesResponse', 'StopLocation'] | ||
|
||
|
||
class IncomingRoutesResponse(BaseModel): | ||
response_time: datetime = Field(default_factory=datetime.now) | ||
stop_info: 'Stop' | ||
incoming_routes: List['IncomingRoute'] | ||
|
||
|
||
class Agency(BaseModel): | ||
id: int | ||
name: str | ||
url: str | ||
phone: str | ||
|
||
|
||
class Stop(BaseModel): | ||
id: int | ||
code: int | ||
name: str | ||
city: str | None | ||
street: Optional[str] = None | ||
floor: Optional[str] = None | ||
platform: Optional[str] = None | ||
location: 'StopLocation' | ||
location_type: str | ||
parent_station_id: Optional[str] = None | ||
zone_id: Optional[str] = None | ||
|
||
|
||
class Route(BaseModel): | ||
id: str | ||
id: int | ||
agency: Agency | ||
short_name: str | ||
from_stop_name: str | ||
to_stop_name: str | ||
from_city: str | ||
to_city: str | ||
description: str | ||
type: str | ||
type: int | ||
color: str | ||
|
||
|
||
class IncomingRoute(BaseModel): | ||
eta: int | ||
route: 'Route' | ||
route: Route | ||
|
||
|
||
class StopLocation(BaseModel): | ||
type: str = 'Point' | ||
coordinates: List[float] | ||
coordinates: list[float] | ||
|
||
|
||
class Stop(BaseModel): | ||
id: int | ||
code: int | ||
name: str | ||
city: str | None | ||
street: str | None = None | ||
floor: str | None = None | ||
platform: str | None = None | ||
location: StopLocation | ||
location_type: int | ||
parent_station_id: str | None = None | ||
zone_id: int | None = None | ||
|
||
IncomingRoutesResponse.update_forward_refs() | ||
Stop.update_forward_refs() | ||
|
||
class IncomingRoutesResponse(BaseModel): | ||
response_time: datetime = Field(default_factory=datetime.now) | ||
stop_info: Stop | ||
incoming_routes: list[IncomingRoute] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,34 @@ | ||
from typing import Optional | ||
|
||
from pydantic import BaseSettings, Field | ||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
||
|
||
class Env(BaseSettings): | ||
class Config: | ||
env_file = '.env' | ||
env_file_encoding = 'utf-8' | ||
model_config = SettingsConfigDict(env_file='.env', env_file_encoding='utf-8') | ||
|
||
TOKEN: str = Field(env='TOKEN') | ||
TOKEN: str | ||
|
||
WEBAPP_HOST: Optional[str] = Field(None, env='WEBAPP_HOST') | ||
WEBAPP_PORT: Optional[int] = Field(None, env='WEBAPP_PORT') | ||
WEBHOOK_PATH: Optional[str] = Field(None, env='WEBHOOK_PATH') | ||
WEBHOOK_URL: Optional[str] = Field(None, env='WEBHOOK_URL') | ||
WEBAPP_HOST: str | None = None | ||
WEBAPP_PORT: int | None = None | ||
WEBHOOK_PATH: str | None = None | ||
WEBHOOK_URL: str | None = None | ||
|
||
DOCKER_MODE: bool = Field(False, env='DOCKER_MODE') | ||
DOCKER_MODE: bool = False | ||
|
||
DB_URL: str = Field('localhost', env='DB_URL') | ||
DB_NAME: str = Field(..., env='DB_NAME') | ||
DB_COLLECTION_NAME: str = Field(..., env='DB_COLLECTION_NAME') | ||
DB_URL: str = 'localhost' | ||
DB_NAME: str | ||
DB_COLLECTION_NAME: str | ||
|
||
PERIOD: int = Field(5, env='PERIOD') # how often message updates (seconds) | ||
TTL: int = Field(5, env='TTL') # how long message updates (seconds) | ||
API_URL: str | ||
MAPBOX_TOKEN: str | ||
|
||
METRICS_DSN: Optional[str] = Field(None, env='METRICS_DSN') | ||
METRICS_TABLE_NAME: Optional[str] = Field(None, env='METRICS_TABLE_NAME') | ||
PERIOD: int = 5 # how often message updates (seconds) | ||
TTL: int = 5 # how long message updates (seconds) | ||
|
||
SENTRY_KEY: Optional[str] = Field(None, env='SENTRY_KEY') | ||
API_URL: str = Field(..., env='API_URL') | ||
MAPBOX_TOKEN: str = Field(..., env='MAPBOX_TOKEN') | ||
THROTTLE_QUANTITY: int = 30 | ||
THROTTLE_PERIOD: int = 3 | ||
|
||
THROTTLE_QUANTITY: int = Field(30, env='THROTTLE_QUANTITY') | ||
THROTTLE_PERIOD: int = Field(3, env='THROTTLE_PERIOD') | ||
METRICS_DSN: str | None = None | ||
METRICS_TABLE_NAME: str | None = None | ||
SENTRY_KEY: str | None = None | ||
|
||
|
||
env = Env() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,32 @@ | ||
from aiogram.dispatcher.filters import Filter, Text | ||
from aiogram.types import ContentTypes, ContentType | ||
from aiogram.utils.exceptions import MessageNotModified | ||
from aiogram import F, Dispatcher | ||
from aiogram.exceptions import TelegramBadRequest | ||
from aiogram.filters.exception import ExceptionTypeFilter | ||
|
||
from bus_bot.clients.bus_api.exceptions import StationNonExistsError, ApiNotRespondingError, NoStopsError | ||
from bus_bot.helpers import CallbackPrefix | ||
from bus_bot.misc import dp | ||
|
||
from . import forms | ||
from . import errors | ||
from . import helpers | ||
from . import commands | ||
from . import bus_handlers | ||
from bus_bot.clients.bus_api.exceptions import StationNonExistsError, ApiNotRespondingError, NoStopsError | ||
from bus_bot.exceptions import StopAlreadySaved | ||
from bus_bot.handlers import errors | ||
from bus_bot.handlers.forms import router as forms_router | ||
from bus_bot.handlers.helpers import incorrect_message_router, cancel_router | ||
from bus_bot.handlers.commands import router as commands_router | ||
from bus_bot.handlers.bus_handlers import router as bus_handlers_router | ||
|
||
|
||
__all__ = ['register_handlers'] | ||
|
||
from .. import texts | ||
from ..exceptions import StopAlreadySaved | ||
|
||
from ..states import RenameStopState | ||
|
||
|
||
def register_handlers(): | ||
def register_handlers(dp: Dispatcher): | ||
# errors | ||
dp.register_errors_handler(errors.on_err_not_modified, exception=MessageNotModified) | ||
dp.register_errors_handler(errors.on_err_station_not_exists, exception=StationNonExistsError) | ||
dp.register_errors_handler(errors.on_err_api_not_responding, exception=ApiNotRespondingError) | ||
dp.register_errors_handler(errors.on_err_api_timeout, exception=ApiNotRespondingError) | ||
dp.register_errors_handler(errors.on_err_not_stations_found, exception=NoStopsError) | ||
dp.register_errors_handler(errors.on_err_stop_already_saved, exception=StopAlreadySaved) | ||
dp.register_errors_handler(errors.on_err_unknown_exception, exception=Exception) # Should be last in this list! | ||
|
||
dp.register_message_handler(helpers.on_cancel, text_startswith=texts.cancel_button, state='*') # Should be first after error handlers! | ||
|
||
# commands | ||
dp.register_message_handler(commands.on_start_command, commands=['start'], state='*') | ||
dp.register_message_handler(commands.on_help_command, commands=['help']) | ||
dp.register_message_handler(commands.on_saved_stops_command, commands=['my_stops']) | ||
|
||
# messages | ||
dp.register_message_handler( | ||
bus_handlers.on_stop_code, | ||
lambda msg: msg.text.isdigit(), | ||
content_types=ContentTypes.TEXT | ||
) | ||
dp.register_message_handler(bus_handlers.on_location, content_types=[ContentType.LOCATION, ContentType.VENUE]) | ||
|
||
# callbacks | ||
dp.register_callback_query_handler(bus_handlers.on_terminate_call, text_startswith=CallbackPrefix.terminate_stop_updating) | ||
dp.register_callback_query_handler( | ||
bus_handlers.on_stop_call, | ||
text_startswith=[CallbackPrefix.get_stop, CallbackPrefix.get_saved_stop] | ||
) | ||
dp.register_callback_query_handler(bus_handlers.on_save_stop, text_startswith=CallbackPrefix.save_stop) | ||
dp.register_callback_query_handler(bus_handlers.on_remove_stop, text_startswith=CallbackPrefix.remove_stop) | ||
|
||
# forms | ||
dp.register_message_handler(forms.on_stop_rename, state=RenameStopState.waiting_for_stop_name) | ||
|
||
# THIS ONE SHOULD BE LAST | ||
dp.register_message_handler(helpers.incorrect_message_handler) | ||
dp.error.register(errors.on_err_not_modified, ExceptionTypeFilter(TelegramBadRequest)) | ||
dp.error.register(errors.on_err_station_not_exists, ExceptionTypeFilter(StationNonExistsError)) | ||
dp.error.register(errors.on_err_api_not_responding, ExceptionTypeFilter(ApiNotRespondingError)) | ||
dp.error.register(errors.on_err_api_timeout, ExceptionTypeFilter(ApiNotRespondingError)) | ||
dp.error.register(errors.on_err_not_stations_found, ExceptionTypeFilter(NoStopsError)) | ||
dp.error.register(errors.on_err_stop_already_saved, ExceptionTypeFilter(StopAlreadySaved)) | ||
dp.error.register(errors.on_err_unknown_exception, ExceptionTypeFilter(Exception)) # Should be last in this list! | ||
|
||
dp.include_router(cancel_router) # Should be first after error handlers! | ||
dp.include_router(commands_router) | ||
dp.include_router(bus_handlers_router) | ||
dp.include_router(forms_router) | ||
dp.include_router(incorrect_message_router) |
Oops, something went wrong.