-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencrypted_telegram_notifier.py
120 lines (98 loc) · 4.8 KB
/
encrypted_telegram_notifier.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
#
# Copyright (C) Daniel Baumgartner 2023
#
import os
import re
import time
import datetime
import telebot
def set_process_name(name):
import ctypes
libc = ctypes.cdll.LoadLibrary("libc.so.6")
buffer = ctypes.create_string_buffer(len(name) + 1)
buffer.value = str.encode(name)
libc.prctl(15, ctypes.byref(buffer), 0, 0, 0)
# Replace YOUR_API_TOKEN with your actual bot token
bot = telebot.TeleBot("YOUR_API_TOKEN")
# Replace YOUR_CHAT_ID with the actual chat ID
chat_id = "YOUR_CHAT_ID"
# Replace DIRECTORY_PATH with the path where sondemod creates the file for an encrypetd sonde
directory_path = "DIRECTORY_PATH"
# Replace FILE_NAME with the name of the file which sondemod will create
file_name = "FILE_NAME"
# Combine filename and path
file_path = os.path.join(directory_path, file_name)
# If you want you can define your receiver callsign here, e.g. callsign = "BAUMI2-15"
callsign = None
# If you want you can define your receiver position here, e.g. position = "47.123, 12.123"
position = None
# Set this to True if you always want to get the file generated by sondemod
always_send_file = False
if __name__ == "__main__":
print(f"Started Encrypted Sonde Notifier!\n")
# Set process name to find the process afterwards
set_process_name("encrypted-tg-notifier")
while True:
try:
# Check if sondemod created a new file
if os.path.exists(file_path):
print("\nDetected new encrypted Radiosonde!")
# Open the file in read mode
with open(file_path, "r") as file:
# Read the content of the file
content = file.read()
# The content of the file looks like this (the frequency is not always there): T1234567 403.500MHz
# Find the SN and frequency with regex expressions
serial = re.findall(r"[A-Z]\d{7}", content)
frequency = re.findall(r"\d{3}\.\d{3}MHz", content)
# The serial number is only valid if there was one match in the file
if len(serial) != 1:
serial = "Unknown"
print("Failed to parse the serial number.")
else:
serial = serial[0]
# The frequency is only valid if there was one match in the file
if len(frequency) != 1:
if len(frequency) > 1:
print("Failed to parse the frequency.")
frequency = "Unknown"
else:
frequency = frequency[0]
# Get time information
date_time = datetime.datetime.utcnow()
_date = date_time.strftime("%Y-%m-%d")
_time = date_time.strftime("%H:%M:%S")
# Determining the content of the chat message
info = f"Serial Number: {serial}\nFrequency: {frequency}\nType: RS41-SGM\nDate: {_date}\nTime: {_time} UTC"
# Add the receiver callsign to message if the user specified it
if callsign is not None:
info += f"\nReceiver Callsign: {callsign}"
# Add the receiver position to message if the user specified it
if position is not None:
info += f"\nReceiver Position: {position}"
print(info)
info = f"Received new encrypted Radiosonde!\n\n" + info
# Only send the file generated by sondemod if the user set "always_send_file" or we failed to parse the SN
# Elsewise we only send a message with the information
if always_send_file or serial == "Unknown":
with open(file_path, "rb") as file:
# Define the filename
_time = _time.replace(":", "-")
filename = f"encrypted_{_date}_{_time}.txt"
# Send the user the information and the file via Telegram
sent_message = bot.send_document(chat_id, file, caption=info, visible_file_name=filename)
else:
# Send the user the information via Telegram
sent_message = bot.send_message(chat_id, text=info)
# Check if the transmission was successful
if type(sent_message) == telebot.types.Message:
print(f"Succesfully notified user via Telegram.")
else:
print("Failed to notify the user via Telegram.")
# Delete the file after reading it
os.remove(file_path)
# Check for a new encrypted sonde every 5 seconds
time.sleep(5)
except Exception:
print("Something went wrong!")
time.sleep(30)