-
Notifications
You must be signed in to change notification settings - Fork 1
/
inreach
103 lines (92 loc) · 2.88 KB
/
inreach
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
#!/usr/bin/python
'''
Skrypt do wysylania wiadomosc na urzadzenia inReach
by Bartosz Celmer <[email protected]>
'''
import requests
import sys
import os
import cPickle
def sendInReachMessage(dane):
isOk = False
err = None
resp = None
try:
resp = requests.post('https://inreach.garmin.com/TextMessage/TxtMsg',data=dane)
if resp.json()['Success'] == True:
isOk = True
except Exception as e:
err = e
return isOk,err,resp
def splitUpToN(text,n=150):
text = text.replace(', ',',').replace(' ,',',')
s = text.split(' ')
parts = []
out = ''
for w in s:
if len(out) + len(w) > n:
parts.append(out.strip())
out = ''
out = out + ' ' + w
parts.append(out.strip())
return [e for e in parts if len(e) > 0]
def sendMessage(addressTo,message,addressFrom='[email protected]',maxTry=100,splitOn=158,splitTo=150):
dane = {}
homeDir = os.environ['HOME']
adrToGuid = {}
try:
f = open('%s/.inreach.pb.cPickle'%homeDir,'r')
adrToGuid = cPickle.load(f)
f.close()
except Exception as e:
pass
if addressTo.lower() in adrToGuid.keys():
dane['Guid'] = adrToGuid[addressTo.lower()]
else:
dane['Guid'] = addressTo
dane['ReplyAddress'] = addressFrom
dane['ReplyMessage'] = ''
if len(message) > splitOn:
msgs = splitUpToN(message,n=splitTo)
allMsgs = len(msgs)
print 'Long massage to %s splited in %d messages'%(addressTo,allMsgs)
for (i,m) in enumerate(msgs):
msgs[i] = '[%2d/%2d] %s'%(i+1,allMsgs,m)
print msgs[i]
print '---------------------------------------------------------------------'
for (i,m) in enumerate(msgs):
print 'Sending %d message: '%(i+1),
sys.stdout.flush()
for i in range(maxTry):
dane['ReplyMessage'] = m
(status,e,r) = sendInReachMessage(dane)
if status:
print 'OK'
sys.stdout.flush()
break
else:
print 'ER, ',
sys.stdout.flush()
if i==maxTry-1:
print 'Giving up, sorry'
else:
print 'Short message to %s.\n%s\nSending: '%(addressTo,message),
sys.stdout.flush()
for i in range(maxTry):
dane['ReplyMessage'] = message
(status,e,r) = sendInReachMessage(dane)
if status:
print 'OK'
sys.stdout.flush()
break
else:
print 'ER, ',
sys.stdout.flush()
if i==maxTry-1:
print 'Giving up, sorry'
try:
ardTo = raw_input('To: ')
msg = raw_input('Msg: ')
sendMessage(ardTo,msg)
except KeyboardInterrupt as e:
print '\n\nExiting.'