-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctiv3ws.py
executable file
·148 lines (124 loc) · 5.88 KB
/
ctiv3ws.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/python3
import configparser
import requests
import json
import mysql.connector
from mysql.connector import errorcode
import datetime
import logging
import os
import websocket
import sys
import sdnotify
import signal
config = configparser.ConfigParser()
config.read(os.path.dirname(sys.argv[0]) + '/cti2sql.conf')
config_sql = {
'user': config['sql']['user'],
'password': config['sql']['password'],
'host': config['sql']['host'],
'database': config['sql']['database'],
'raise_on_warnings': True,
}
def signal_handler(sig, frame):
if 'cursor' in vars():
cursor.close()
if 'cnx' in vars():
cnx.close()
if 'ws' in vars():
ws.close()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
logging.basicConfig(filename=config['misc']['log_file'],level=logging.WARNING)
logging.warning(str(datetime.datetime.today()) + " cti2sql starting ")
token = config['ovh']['token']
urlv2 = config['ovh']['endpoint']
def on_error(ws, error):
logging.warning(f"Erreur {error}")
#print(f"Erreur {error}")
def on_close(ws):
#print("### closed ###")
pass
#ws.run_forever()
def on_open(ws):
ws.send('')
def on_message(ws, message):
try:
n = sdnotify.SystemdNotifier()
event = json.loads(message)
#print(event)
#print(res)
cnx = mysql.connector.connect(**config_sql)
cursor = cnx.cursor(buffered=True)
#print(res)
#print(type(res))
#print(event["event"])
if event["event"] == "member-queue-end":
#print(event)
pass
if "event" in event:
if event["event"] == "member-queue-end":
if event["data"]["QueueCause"] == "Terminated":
QueueMemberJoinedTime = datetime.datetime.fromtimestamp(int(event["data"]["QueueMemberJoinedTime"])).strftime('%Y-%m-%d %H:%M:%S')
QueueAgentAnsweredTime = datetime.datetime.fromtimestamp(int(event["data"]["QueueAgentAnsweredTime"])).strftime('%Y-%m-%d %H:%M:%S')
QueueMemberLeavingTime = datetime.datetime.fromtimestamp(int(event["data"]["QueueMemberLeavingTime"])).strftime('%Y-%m-%d %H:%M:%S')
query = "insert into events (`QueueMemberSessionUUID`, `Calling`, `QueueMemberJoinedTime`, `QueueAgentAnsweredTime`, `Agent`, `QueueMemberLeavingTime`, `QueueCause`) VALUES ('" + event["data"]["QueueMemberSessionUUID"]+ "', '" + event["data"]["Calling"] + "', '" + QueueMemberJoinedTime + "', '" + QueueAgentAnsweredTime + "', '" + event["data"]["Agent"] + "', '" + QueueMemberLeavingTime + "', 'Terminated');"
cursor.execute(query)
cnx.commit()
elif event["data"]["QueueCause"] == "Cancel":
QueueMemberJoinedTime = datetime.datetime.fromtimestamp(int(event["data"]["QueueMemberJoinedTime"])).strftime('%Y-%m-%d %H:%M:%S')
QueueMemberLeavingTime = datetime.datetime.fromtimestamp(int(event["data"]["QueueMemberLeavingTime"])).strftime('%Y-%m-%d %H:%M:%S')
query = "insert into events (`QueueMemberSessionUUID`, `Calling`, `QueueMemberJoinedTime`, `QueueMemberLeavingTime`, `QueueCause`) VALUES ('" + event["data"]["QueueMemberSessionUUID"]+ "', '" + event["data"]["Calling"] + "', '" + QueueMemberJoinedTime + "', '" + QueueMemberLeavingTime + "', 'Cancel');"
cursor.execute(query)
cnx.commit()
if message == "Session not found\n":
ws.keep_running = False
else:
pass
#pass
#logging.warning(str(datetime.datetime.today()) + " not a requests response")
except mysql.connector.Error as err:
#print(f"Erreur {err}")
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
logging.warning(str(datetime.datetime.today()) + " Something is wrong with your user name or password")
#print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
logging.warning(str(datetime.datetime.today()) + " Database does not exists")
#print("Database does not exists")
else:
logging.warning(str(datetime.datetime.today()) + " sql error: " + str(err.args) + " " + str(err.errno))
#print("sql error: " + str(err.args) + " " + str(err.errno))
except ValueError:
#print(f"Erreur value error")
logging.warning(str(datetime.datetime.today()) + " value error")
except requests.exceptions.ReadTimeout:
#print(f"Erreur timeout")
logging.warning(str(datetime.datetime.today()) + " requests timeout")
except Exception as e:
logging.warning(str(datetime.datetime.today()) + " erreur2 " + str(e.args))
#print(f"Erreur {e}")
finally:
if 'cursor' in vars():
cursor.close()
#print('cursor ferme')
if 'cnx' in vars():
cnx.close()
#print('connexion fermee')
if __name__ == "__main__":
n = sdnotify.SystemdNotifier()
n.notify("READY=1")
while 1:
logging.warning(str(datetime.datetime.today()) + " requesting new session")
#print("requesting new session")
session = json.loads(requests.post("https://" + urlv2 + "/session", data = {}).text)
id = session["id"]
assoc = requests.post("https://" + urlv2 + "/session/" + id + "/subscribe/" + token, data = {})
logging.warning(str(datetime.datetime.today()) + " session created, id " + id)
#print("session created, id " + id)
websocket.enableTrace(False)
ws = websocket.WebSocketApp("wss://" + urlv2 + "/session/" + id + "/events/websocket",
on_message = on_message,
on_error = on_error,
on_close = on_close,
on_open = on_open)
ws.run_forever()