forked from prusnak/smspushtx
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmessages.py
More file actions
79 lines (59 loc) · 1.81 KB
/
messages.py
File metadata and controls
79 lines (59 loc) · 1.81 KB
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
import binascii
import string
import requests
messages = {}
def process_msg(msg):
try:
# single message
if 'concat' not in msg:
# push it
pushtx(msg['text'])
return
# split message
ref = '%s:%s' % (msg['msisdn'], msg['concat-ref'])
idx = int(msg['concat-part'])
cnt = int(msg['concat-total'])
# not seen before
if ref not in messages:
messages[ref] = cnt * [None]
# assign value
messages[ref][idx - 1] = msg['text']
# are all value set?
if None not in messages[ref]:
# get joined message
joined = ''.join(messages[ref])
del messages[ref]
# and push it
pushtx(joined)
print('PROCESSED OK')
except Exception as ex:
print('PROCESS ERROR', ex)
def pushtx(data):
print('PUSHING "%s"' % data)
# is it hex format?
if all(c in string.hexdigits for c in data):
decoded = data
else:
# try base64 decode, then convert to hex
try:
decoded = binascii.hexlify(binascii.a2b_base64(data)).decode()
except:
decoded = None
if not decoded:
print('PUSH DECODE ERROR')
return
endpoints = [
'https://btc-bitcore1.trezor.io/api/tx/send',
'https://btc-bitcore4.trezor.io/api/tx/send',
'https://insight.bitpay.com/api/tx/send',
'https://blockexplorer.com/api/tx/send',
]
for e in endpoints:
try:
r = requests.post(e, json={'rawtx': decoded}, timeout=1)
if r.status_code == 200:
print('PUSH OK', e)
else:
print('PUSH ERROR', r.status_code, e)
except Exception as ex:
print('PUSH FAILED', e, ex)