-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpager_telegram_forwarder.py
80 lines (77 loc) · 2.88 KB
/
pager_telegram_forwarder.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
import sys, getopt
import subprocess
import collections
from time import gmtime, strftime
from pytgbot import Bot
def start_multimon(freq, prot, minlen, tid, rid):
# replace POCSAG512 with -a POCSAG512
prot = prot.replace(" ", " -a ")
# create telegram bot
bot = Bot(tid)
# create deque
d = collections.deque(maxlen=100)
# call multimon
call = "rtl_fm -d0 -f "+freq+" -s 22050 | multimon-ng -t raw -a "+prot+" -f alpha -t raw /dev/stdin -"
print (call)
mm = subprocess.Popen(call,shell=True,stdout=subprocess.PIPE)
while mm.poll() is None:
# process new pocsag entry
output = mm.stdout.readline().decode('utf-8') # read new line
print (output)
if "Alpha" not in output: # check if non alpha element
continue # if yes, continue
output = output.replace("<NUL>", "") # replace terminator sequence
if output in d: # if the message is already in the list -> skip (pager messages might be sent multiple times)
continue
d.append(output) # add to deque
msg = output.split("Alpha:",1)[1] # get the message
if int(len(msg))<int(minlen): # if msg length < minlen skip
continue;
time = strftime("%Y-%m-%d %H:%M", gmtime()) # get timestamp
print (msg) # print the message
try:
bot.send_message(rid, 'Time: '+time+'\nMessage: '+msg) # send it.
except :
pass
def main(argv):
print ("Pager telegram forwarder")
print (argv)
freq = ""
protocols = ""
min_len = ""
tel_ID = ""
rec_ID = ""
usage = 'pager_telegram_forwarder.py --freq="<freq>" --prot="<protocols>" --min="<minimum message length>" --tID="<Telegram API ID>" --rID="<Telegram recipient ID>"'
if len(argv) != 5:
print ("#args = " + len(argv))
print (usage)
sys.exit(2)
try:
opts, args = getopt.getopt(argv,"hf:p:m:t:r",["freq=","prot=","min=","tID=","rID="])
except (getopt.GetoptError, exc):
print (exc.msg)
print (usage)
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print (usage)
sys.exit()
elif opt in ("--freq"):
freq = arg
elif opt in ("--prot"):
protocols = arg
elif opt in ("--min"):
min_len = arg
elif opt in ("--tID"):
tel_ID = arg
elif opt in ("--rID"):
rec_ID = arg
print ('Freq ', freq)
print ('Protocols ', protocols)
print ('Minimum length ', min_len)
print ('Telegram ID ', tel_ID)
print ('Receiver ID ', rec_ID)
# start multimon
start_multimon(freq, protocols, min_len, tel_ID, rec_ID)
if __name__ == "__main__":
main(sys.argv[1:])