-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
288 lines (245 loc) · 11.8 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import asyncio
import json
import typing
from typing import Any, Dict, List, Optional, Text, Tuple
import aiohttp
import uvicorn
from aiogram import Bot
from aiogram.bot.api import TELEGRAM_PRODUCTION, TelegramAPIServer
from aiogram.types import (
InlineKeyboardButton,
InlineKeyboardMarkup,
KeyboardButton,
Message,
ReplyKeyboardMarkup,
ReplyKeyboardRemove,
Update,
)
from aiogram.utils.exceptions import TelegramAPIError
from env.settings import (
APPLICATION_HOST,
APPLICATION_PORT,
TELEGRAM_BOT_TOKEN,
TELEGRAM_BOT_WEBHOOK_URL,
logger,
)
from fastapi import FastAPI
from fastapi.requests import Request
from FlightRadar24.api import FlightRadar24API
from utils.tools import flight_information_parser, start_background_task
live_locations = {}
class TelegramBot(Bot):
@classmethod
def name(cls) -> str:
return "Telegram"
def __init__(self,
access_token: Optional[Text]):
self.access_token = access_token
super().__init__(token=access_token, parse_mode="HTML")
async def send_text_message(
self, recipient_id: Text, text: Text,
reply_markup=None, **kwargs: Any
) -> None:
"""Sends text message."""
for message_part in text.strip().split("\n\n"):
await self.send_message(recipient_id, message_part,
reply_markup=reply_markup, parse_mode="HTML")
async def send_airplane_information(
self, recipient_id: Text, flights_detail: List[Dict],
message_type: Text, live: bool = False
) -> None:
"""Sends text message."""
for flight_detail in flights_detail:
logger.debug(f"Sending flight detail: {flight_detail}")
flight_detail_string = flight_information_parser(flight_detail)
logger.debug(f"Sending flight detail: {flight_detail_string}")
if message_type == 'message':
message = await self.send_message(recipient_id, flight_detail_string,
parse_mode="HTML")
if live:
live_locations[recipient_id]=message.message_id
elif message_type == 'edited_message':
message_id = live_locations.get(recipient_id)
await self.edit_message_text(flight_detail_string, recipient_id, message_id,
parse_mode="HTML")
class PlaneGoesToBot:
def __init__(self) -> None:
self.rest_api_app = FastAPI()
self.create_rest_api_route()
self._set_telegram_webhook()
self.background_process = set()
logger.info("PlaneGoesToBot is ready.")
logger.info(f"Telegram webhook url: {TELEGRAM_BOT_WEBHOOK_URL}")
def create_rest_api_route(self):
default_responses = {
200: {"description": "Success"},
400: {"description": "Bad Request"},
404: {"description": "Not found"},
403: {"description": "Unauthorized access"},
422: {"description": "Unprocessable Entity"},
500: {"description": "Internal Server Error"},
}
from pydantic import BaseModel
class ResponseMessage(BaseModel):
ok: bool = True
description: Text = "Success"
def __init__(self, ok: bool, description: Text):
super().__init__()
self.ok = ok
self.description = description
@self.rest_api_app.get('/', tags=["main"], responses=default_responses)
@self.rest_api_app.get('/planeBot', tags=["Root"], responses=default_responses)
@self.rest_api_app.post('/planeBot', tags=["Root"], responses=default_responses)
async def rest_api_slack_main() -> ResponseMessage:
return ResponseMessage(True, "Success")
@self.rest_api_app.get('/planeBot/health',
tags=["Health"], responses=default_responses)
@self.rest_api_app.post('/planeBot/health',
tags=["Health"], responses=default_responses)
async def rest_api_slack_health() -> ResponseMessage:
return ResponseMessage(True, "Success")
@self.rest_api_app.get('/webhooks/telegram/webhook',
tags=["Webhooks"], responses=default_responses)
@self.rest_api_app.post('/webhooks/telegram/webhook',
tags=["Webhooks"], responses=default_responses)
async def rest_api_telegram_webhook(updates: Request) -> ResponseMessage:
if not updates:
return ResponseMessage(False, "No updates received.")
try:
updates = await updates.json()
if not updates:
return ResponseMessage(False, "No updates received.")
except Exception as e:
logger.error(f"Failed to parse updates: {e}")
return ResponseMessage(False, "Failed to parse updates.")
try:
message = updates.get('message', updates.get('edited_message', {}))
message_type = 'message' if updates.get('message', None) \
else 'edited_message'
logger.debug(f"Received message: {message}")
if message.get('location', None) is None:
await self.telegram_channel.send_text_message(
message.get('chat', {}).get('id', None),
"Please share your location to receive flight details."
)
return ResponseMessage(False, "No location found.")
start_background_task(
self.background_process,
self.information_retrieval, message, message_type
)
return ResponseMessage(True, "Success")
except Exception as e:
logger.exception(f"Failed to process updates: {e}")
return ResponseMessage(False, "Failed to process updates.")
async def information_retrieval(self, message: Dict[Any, Any], message_type: Text):
user_location = message.get('location')
if user_location and isinstance(user_location, dict):
latitude = user_location.get('latitude', None)
longitude = user_location.get('longitude', None)
live = bool(user_location.get('live_period', None))
if latitude and longitude:
try:
if retrieved_information := self.flight_details(
(latitude, longitude)
):
await self.telegram_channel.send_airplane_information(
message.get('chat', {}).get('id', None),
retrieved_information,
message_type,
live
)
else:
await self.telegram_channel.send_text_message(
message.get('chat', {}).get('id', None),
"No information found at this moment. "\
"Please try again later."
)
return
except Exception as e:
logger.exception(f"Failed to retrieve information: {e}")
await self.telegram_channel.send_text_message(
message.get('chat', {}).get('id', None),
"Failed to retrieve information. "\
"Please try again later."
)
return
await self.telegram_channel.send_text_message(
message.get('chat', {}).get('id', None),
"Please share your current location with telegram "\
"options to receive flight details."
)
return
def run(self) -> None:
uvicorn.run(
app=self.rest_api_app,
host=APPLICATION_HOST,
port=APPLICATION_PORT,
)
def _set_telegram_webhook(self) -> None:
"""Sets Telegram webhook."""
logger.info("Setting Telegram webhook...")
try:
self.telegram_channel = TelegramBot(TELEGRAM_BOT_TOKEN)
asyncio.run(self.telegram_channel.set_webhook(
url=TELEGRAM_BOT_WEBHOOK_URL,
drop_pending_updates=True,
max_connections=1000))
logger.info("Telegram webhook set.")
except TelegramAPIError as e:
logger.exception("Failed to set Telegram webhook.")
raise e
def flight_details(self, coordinates: Tuple):
fr_api = FlightRadar24API()
lat, lon = coordinates
def get_square(lat, lon, distance):
from geopy import Point as GPoint
from geopy.distance import geodesic
location = GPoint(lat, lon)
north = geodesic(kilometers=distance).destination(location, 0)
east = geodesic(kilometers=distance).destination(location, 90)
south = geodesic(kilometers=distance).destination(location, 180)
west = geodesic(kilometers=distance).destination(location, 270)
return f"{north.latitude},{south.latitude},{west.longitude},{east.longitude}"
logger.info(f"Getting flight details..., from: {get_square(lat, lon, 50)}")
flights_detail = fr_api.get_flights(bounds=f"{get_square(lat, lon, 50)}")
logger.info(f"Founds {len(flights_detail)} flights.")
flights_information = []
for flight in flights_detail:
flight_full_information = fr_api.get_flight_details(flight)
flight.set_flight_details(flight_full_information)
flights_information.append({
'id': flight.id,
'number': flight.number,
'callsign': flight.callsign,
'airline_name': flight.airline_name,
'airline_code': flight.airline_icao,
'aircraft_name': flight.aircraft_model,
'aircraft_code': flight.aircraft_code,
'aircraft_history': [
{
'origin_airport': item.get("airport", {}).get(
"origin", {}).get("name", ""),
'destination_airport': item.get("airport", {}).get(
"destination", {}).get("name", "")
} for item in flight.aircraft_history if item.get("airport", {}) \
and item.get("airport", {}).get("origin", {}) \
and item.get("airport", {}).get("destination", {})
] if flight.aircraft_history else [],
"origin_airport_country_name": flight.origin_airport_country_name,
"origin_airport_country_code": flight.origin_airport_country_code,
"origin_airport_name": flight.origin_airport_name,
"origin_airport_code": flight.origin_airport_iata,
"destination_airport_country_name": flight.destination_airport_country_name,
"destination_airport_country_code": flight.destination_airport_country_code,
"destination_airport_name": flight.destination_airport_name,
"destination_airport_code": flight.destination_airport_iata,
'altitude': flight.altitude,
'heading': flight.heading,
'speed': flight.ground_speed,
'vertical_speed': flight.vertical_speed,
'status': "On Ground" if flight.on_ground else "On Air",
'status_text': flight.status_text,
'status_icon': flight.status_icon,
'time_details': flight.time_details,
})
return flights_information