From 5a84b1edb4e75addf71c265691d88b3e758b864f Mon Sep 17 00:00:00 2001 From: b0nz <79069172+rcbonz@users.noreply.github.com> Date: Sat, 12 Mar 2022 18:36:01 -0300 Subject: [PATCH 2/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 93c23b7..36c5b09 100644 --- a/README.md +++ b/README.md @@ -111,4 +111,4 @@ After running this command, due to the `-d` flag the container will be running i - Add proper logging and service to run the program and remove excessive printing. - Better single config YAML, or DB maybe -### [More References/Documentation](Refrences.md) +### [More References/Documentation](References.md) From 9f686c296ba3c2ba2edd21e7a302c984bc73e167 Mon Sep 17 00:00:00 2001 From: rcbonz Date: Sat, 12 Mar 2022 19:09:10 -0300 Subject: [PATCH 3/8] Remove Google Ads on screenshot --- defSS.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/defSS.py b/defSS.py index d29d607..de32d04 100644 --- a/defSS.py +++ b/defSS.py @@ -34,6 +34,12 @@ def get_adsbx_screenshot(file_path, url_params, enable_labels=False, enable_trac browser.execute_script("$('#infoblock-container').css('overflow', 'hidden');") except: print("Couldn't disable sidebar on map") + #Remove Google Ads + try: + element = browser.find_element_by_xpath("//*[contains(@id, 'FIOnDemandWrapper_')]") + browser.execute_script("""var element = arguments[0]; element.parentNode.removeChild(element); """, element) + except: + print("Couldn't remove Google Ads") #Remove share try: element = browser.find_element_by_xpath("//*[contains(text(), 'Share')]") From 0ae887a81e2b0aa8c5ad6c515d5a6affc2350e17 Mon Sep 17 00:00:00 2001 From: rcbonz Date: Sun, 13 Mar 2022 15:52:58 -0300 Subject: [PATCH 4/8] New feature: send a message using Telegram API with obtained data. --- Pipfile | 1 + README.md | 32 ++++++++++++++++++++++++++++++++ References.md | 4 ++++ configs/plane1.ini | 8 +++++++- defTelegram.py | 38 ++++++++++++++++++++++++++++++++++++++ planeClass.py | 16 +++++++++++++++- 6 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 defTelegram.py diff --git a/Pipfile b/Pipfile index 032f728..c712d1e 100644 --- a/Pipfile +++ b/Pipfile @@ -18,6 +18,7 @@ selenium = "*" opensky-api = {editable = true, git = "https://github.com/openskynetwork/opensky-api.git", subdirectory = "python"} webdriver-manager = "*" shapely = "*" +python-telegram-bot = "*" [requires] python_version = "3.9" diff --git a/README.md b/README.md index 36c5b09..25722a7 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,38 @@ docker-compose up -d After running this command, due to the `-d` flag the container will be running in the background. To see the logs of the docker container use `docker logs CONTAINER` (add `-f` to continue streaming the containers output) +### Telegram message feature - march/2022 + +Data obtained can be sent through Telegram to a chat (contact), channel or groups. + +Creating a Telegram Bot + - Start a conversation with [BotFather](https://t.me/BotFather); + - Send it to the BotFather: /newbot + - Choose a name for your bot; + - Choose a username for your bot; + - Done! You'll get a token to access the HTTP API. + +Getting channel or chat (contact) ID + - Start a conversation with [JsonDumpBot](https://t.me/JsonDumpBot); + - It will reply with a json with information from the message; + - Go to the channel or chat you want the id and forward a message from there to JsonDumpBot; + - Find the id in the reply. It'll look something like this: + ``` + {... +"forward_from_chat": { + "id": xxxxxxxxx, + ...} +``` + - Don't forget to add the bot as admin in channel so messages can be sent. + +Getting a group ID + - Open [Telegram web](https://web.telegram.org); + - Go to group and check the url on address bar of browser; + - That's the group ID (-xxxxxxxxx), it'll look something like this: + ``` + https://web.telegram.org/z/#-xxxxxxxxx + ``` + ### TODO - General Cleanup diff --git a/References.md b/References.md index fc7ef2c..041484f 100644 --- a/References.md +++ b/References.md @@ -66,3 +66,7 @@ ### OurAirports / airports.csv / regions.csv - + +### Telegram Bot + +- \ No newline at end of file diff --git a/configs/plane1.ini b/configs/plane1.ini index 9e77914..0ffe1be 100644 --- a/configs/plane1.ini +++ b/configs/plane1.ini @@ -39,4 +39,10 @@ URL = webhookurl #Role to tag optional, the role ID ROLE_ID = Title = -USERNAME = plane-notify \ No newline at end of file +USERNAME = plane-notify + +[TELEGRAM] +ENABLE = FALSE +TITLE = Title Of Telegram message +ROOM_ID = -100xxxxxxxxxx +BOT_TOKEN = xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \ No newline at end of file diff --git a/defTelegram.py b/defTelegram.py new file mode 100644 index 0000000..d56378c --- /dev/null +++ b/defTelegram.py @@ -0,0 +1,38 @@ +def sendTeleg(photo, message, config): + import telegram + sent = False + retry_c = 0 + while sent == False: + try: + bot = telegram.Bot(token=config.get('TELEGRAM', 'BOT_TOKEN'), request=telegram.utils.request.Request(connect_timeout=20, read_timeout=20)) + sent = bot.send_photo(chat_id=config.get('TELEGRAM', 'ROOM_ID'), photo=photo, caption=message, parse_mode=telegram.ParseMode.MARKDOWN, timeout=20) + except Exception as err: + print('err.args:') + print(err.args) + print(f"Unexpected {err=}, {type(err)=}") + print("\nString err:\n"+str(err)) + if retry_c > 4: + print('Telegram attempts exceeded. Message not sent.') + break + elif str(err) == 'Unauthorized': + print('Invalid Telegram bot token, message not sent.') + break + elif str(err) == 'Timed out': + retry_c += 1 + print('Telegram timeout count: '+str(retry_c)) + pass + elif str(err) == 'Chat not found': + print('Invalid Telegram Chat ID, message not sent.') + break + elif str(err)[:35] == '[Errno 2] No such file or directory': + print('Telegram module couldn\'t find an image to send.') + break + elif str(err) == 'Media_caption_too_long': + print('Telegram image caption lenght exceeds 1024 characters. Message not send.') + break + else: + print('[X] Unknown Telegram error. Message not sent.') + break + else: + print("Telegram message successfully sent.") + return sent \ No newline at end of file diff --git a/planeClass.py b/planeClass.py index 83bb786..0dd495a 100644 --- a/planeClass.py +++ b/planeClass.py @@ -69,7 +69,7 @@ class Plane: self.reg = get_aircraft_reg_by_icao(self.icao) self.type = get_type_code_by_icao(self.icao) self.last_pos_datetime = datetime.fromtimestamp(ac_dict.time_position) - except ValueError as e: + except Exception as e: print("Got data but some data is invalid!") print(e) self.printheader("foot") @@ -407,6 +407,11 @@ class Plane: append_airport(self.map_file_name, nearest_airport_dict) else: raise ValueError("Map option not set correctly in this planes conf") + #Telegram + if self.config.getboolean('TELEGRAM', 'ENABLE'): + from defTelegram import sendTeleg + photo = open(self.map_file_name, "rb") + sendTeleg(photo, message, self.config) #Discord if self.config.getboolean('DISCORD', 'ENABLE'): dis_message = f"{self.dis_title} {message}".strip() @@ -436,6 +441,11 @@ class Plane: route_to = self.route_info() if route_to != None: print(route_to) + #Telegram + if self.config.getboolean('TELEGRAM', 'ENABLE'): + message = f"{self.dis_title} {route_to}".strip() + photo = open(self.map_file_name, "rb") + sendTeleg(photo, message, self.config) #Discord if self.config.getboolean('DISCORD', 'ENABLE'): dis_message = f"{self.dis_title} {route_to}".strip() @@ -563,6 +573,10 @@ class Plane: else: message = f"Circling {round(nearest_airport_dict['distance_mi'], 2)}mi {cardinal} of {nearest_airport_dict['icao']}, {nearest_airport_dict['name']} at {self.alt_ft}ft" print(message) + #Telegram + if self.config.getboolean('TELEGRAM', 'ENABLE'): + photo = open(self.map_file_name, "rb") + sendTeleg(photo, message, self.config) if self.config.getboolean('DISCORD', 'ENABLE'): role_id = self.config.get('DISCORD', 'ROLE_ID') if self.config.has_option('DISCORD', 'ROLE_ID') else None sendDis(message, self.config, self.map_file_name, role_id) From efa1ba70df4966850c1dc06e3d8f506579f0a188 Mon Sep 17 00:00:00 2001 From: b0nz <79069172+rcbonz@users.noreply.github.com> Date: Sun, 13 Mar 2022 16:04:09 -0300 Subject: [PATCH 5/8] Minor fix on README.md --- README.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 25722a7..82760f3 100644 --- a/README.md +++ b/README.md @@ -109,29 +109,29 @@ After running this command, due to the `-d` flag the container will be running i Data obtained can be sent through Telegram to a chat (contact), channel or groups. Creating a Telegram Bot - - Start a conversation with [BotFather](https://t.me/BotFather); - - Send it to the BotFather: /newbot - - Choose a name for your bot; - - Choose a username for your bot; - - Done! You'll get a token to access the HTTP API. +- Start a conversation with [BotFather](https://t.me/BotFather); +- Send it to the BotFather: /newbot +- Choose a name for your bot; +- Choose a username for your bot; +- Done! You'll get a token to access the HTTP API. Getting channel or chat (contact) ID - - Start a conversation with [JsonDumpBot](https://t.me/JsonDumpBot); - - It will reply with a json with information from the message; - - Go to the channel or chat you want the id and forward a message from there to JsonDumpBot; - - Find the id in the reply. It'll look something like this: +- Start a conversation with [JsonDumpBot](https://t.me/JsonDumpBot); +- It will reply with a json with information from the message; +- Go to the channel or chat you want the id and forward a message from there to JsonDumpBot; +- Find the id in the reply. It'll look something like this: ``` {... "forward_from_chat": { "id": xxxxxxxxx, ...} ``` - - Don't forget to add the bot as admin in channel so messages can be sent. +- Don't forget to add the bot as admin in channel so messages can be sent. Getting a group ID - - Open [Telegram web](https://web.telegram.org); - - Go to group and check the url on address bar of browser; - - That's the group ID (-xxxxxxxxx), it'll look something like this: +- Open [Telegram web](https://web.telegram.org); +- Go to group and check the url on address bar of browser; +- That's the group ID (-xxxxxxxxx), it'll look something like this: ``` https://web.telegram.org/z/#-xxxxxxxxx ``` From ec87ae03d8c883e18bd907ab5274bbbae51b5ca3 Mon Sep 17 00:00:00 2001 From: b0nz <79069172+rcbonz@users.noreply.github.com> Date: Sun, 13 Mar 2022 16:08:56 -0300 Subject: [PATCH 6/8] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 82760f3..1a2cf4c 100644 --- a/README.md +++ b/README.md @@ -120,10 +120,10 @@ Getting channel or chat (contact) ID - It will reply with a json with information from the message; - Go to the channel or chat you want the id and forward a message from there to JsonDumpBot; - Find the id in the reply. It'll look something like this: - ``` +``` {... -"forward_from_chat": { - "id": xxxxxxxxx, + "forward_from_chat": { + "id": xxxxxxxxx, ...} ``` - Don't forget to add the bot as admin in channel so messages can be sent. From 57615d98d997ed1b3ecd098bc609088b807563e7 Mon Sep 17 00:00:00 2001 From: b0nz <79069172+rcbonz@users.noreply.github.com> Date: Sun, 13 Mar 2022 16:12:55 -0300 Subject: [PATCH 7/8] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1a2cf4c..d28500b 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,7 @@ Getting channel or chat (contact) ID - It will reply with a json with information from the message; - Go to the channel or chat you want the id and forward a message from there to JsonDumpBot; - Find the id in the reply. It'll look something like this: -``` +```bash {... "forward_from_chat": { "id": xxxxxxxxx, @@ -132,9 +132,9 @@ Getting a group ID - Open [Telegram web](https://web.telegram.org); - Go to group and check the url on address bar of browser; - That's the group ID (-xxxxxxxxx), it'll look something like this: - ``` +```bash https://web.telegram.org/z/#-xxxxxxxxx - ``` +``` ### TODO From 12c4f9b44242801876ad53d1a8535447e33a8692 Mon Sep 17 00:00:00 2001 From: b0nz <79069172+rcbonz@users.noreply.github.com> Date: Tue, 29 Mar 2022 23:02:55 -0300 Subject: [PATCH 8/8] Update planeClass.py --- planeClass.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/planeClass.py b/planeClass.py index 0dd495a..ff82f4b 100644 --- a/planeClass.py +++ b/planeClass.py @@ -408,7 +408,7 @@ class Plane: else: raise ValueError("Map option not set correctly in this planes conf") #Telegram - if self.config.getboolean('TELEGRAM', 'ENABLE'): + if self.config.has_section('TELEGRAM') and self.config.getboolean('TELEGRAM', 'ENABLE'): from defTelegram import sendTeleg photo = open(self.map_file_name, "rb") sendTeleg(photo, message, self.config) @@ -442,7 +442,7 @@ class Plane: if route_to != None: print(route_to) #Telegram - if self.config.getboolean('TELEGRAM', 'ENABLE'): + if self.config.has_section('TELEGRAM') and self.config.getboolean('TELEGRAM', 'ENABLE'): message = f"{self.dis_title} {route_to}".strip() photo = open(self.map_file_name, "rb") sendTeleg(photo, message, self.config) @@ -574,7 +574,7 @@ class Plane: message = f"Circling {round(nearest_airport_dict['distance_mi'], 2)}mi {cardinal} of {nearest_airport_dict['icao']}, {nearest_airport_dict['name']} at {self.alt_ft}ft" print(message) #Telegram - if self.config.getboolean('TELEGRAM', 'ENABLE'): + if self.config.has_section('TELEGRAM') and self.config.getboolean('TELEGRAM', 'ENABLE'): photo = open(self.map_file_name, "rb") sendTeleg(photo, message, self.config) if self.config.getboolean('DISCORD', 'ENABLE'): @@ -651,4 +651,4 @@ class Plane: print(time_since_ra) if time_since_ra.seconds >= 600: print(ra_type) - self.recent_ra_types.pop(ra_type) \ No newline at end of file + self.recent_ra_types.pop(ra_type)