-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket_client.py
executable file
·77 lines (58 loc) · 2 KB
/
socket_client.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
#!/usr/bin/env python3
import websocket
import _thread
import time
import rel
import json
import logging
import sec_ecdh
import base64
logging.basicConfig(level='INFO')
ec = sec_ecdh.C_ECDH()
sample_session_string = "https://staging.smswithoutborders.com:1234567890987654321"
sample_token_string = "1234567890987654321"
session_id = "12345"
def on_message(ws, message):
# print(message)
message = json.loads(message)
if 'session_id' in message:
session_id = message['session_id']
logging.info("+ new session id began: %s", session_id)
if 'from_session_id' in message:
logging.info("+ new client message: %s", message)
"""
{
"type": "MESSAGES_TYPE_SINGLE",
"threadId": "24",
"smsList": "[{<sms item>}, ...]",
"action": "<Actions>"
}
"""
response = {"action":"send",
"smsList":[{"body":"hello world",
"address":"+237123456789" }],
"threadId":"24"
}
data = {
"for_session_id":message['from_session_id'],
"data":response
}
ws.send(json.dumps(data))
def on_error(ws, error):
print(error)
def on_close(ws, close_status_code, close_msg):
print("### closed ###")
def on_open(ws):
print("Opened connection")
if __name__ == "__main__":
import sys
websocket.enableTrace(True)
path = "" if len(sys.argv) < 2 else sys.argv[1]
ws = websocket.WebSocketApp(f"wss://staging.smswithoutborders.com:16000/{path}",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever(dispatcher=rel, reconnect=5) # Set dispatcher to automatic reconnection, 5 second reconnect delay if connection closed unexpectedly
rel.signal(2, rel.abort) # Keyboard Interrupt
rel.dispatch()