-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram.py
262 lines (230 loc) · 9.26 KB
/
telegram.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/python
import json
import requests
import telebot
from os import path
from requests.packages import urllib3
import logging
logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO)
urllib3.disable_warnings()
class Telegram():
# def __init__(self, url):
# self.url = url
# def send(self, message):
# # send messages to Telegram bot (FTEL.SCC.syslog)
# try:
# url = "https://alerts.soc.fpt.net/webhooks/auto-pool/kgY9JnTVxfretKCEYGLNI32MB6Z7eiXa"
# payload = {'text': message, 'parse_mode': 'Markdown'}
# headers = {
# 'Content-Type': 'application/json',
# }
# response = requests.request("POST", url, data=json.dumps(payload), headers=headers)
# return response.text
# except requests.exceptions.RequestException as error:
# return 0
# def send(self, message):
# message = "New ticket"
# try:
# # load config file
# dir_path = path.dirname(path.abspath(__file__))
# filename = path.join(dir_path, "config.json")
# with open(filename, "r") as config_file:
# config = json.load(config_file)
# token = config["telegram"]["token"]
# contacts = config["telegram"]["contacts"]
# # call bot api with token
# bot = telebot.TeleBot(token)
# logging.info("Debug: %s"%(bot.get_me()))
# # send a message to each user in contact list
# for user in contacts:
# bt = bot.send_message(user, message)
# logging.info("Send \"%s\" to %s successful"%(message, user))
# logging.debug(bot.get_updates())
# except Exception as error:
# logging.error(error)
def send(self, message):
try:
# load config file
dir_path = path.dirname(path.abspath(__file__))
filename = path.join(dir_path, "config.json")
with open(filename, "r") as config_file:
config = json.load(config_file)
token = config["telegram"]["token"]
contacts = config["telegram"]["contacts"]
url = "https://api.telegram.org/bot%s/sendMessage"%(token)
payload = {
"chat_id": contacts,
"text": message,
}
headers = {
'content-type': "application/json",
}
response = requests.request("POST", url, data=json.dumps(payload), headers=headers)
logging.info(response.text)
except Exception as e:
logging.error("Telegram send: %s"%(e))
raise e
class Ticket():
def __init__(self):
# load ticket config from config file
try:
dir_path = path.dirname(path.abspath(__file__))
filename = path.join(dir_path, "config.json")
with open(filename, "r") as config_file:
self.config = json.load(config_file)
self.method = self.config["ticket"]['method']
self.url = self.config["ticket"]["url"]
self.interval = self.config["ticket"]["interval"] # minutes
self.payload = self.config["ticket"]["payload"]
except Exception as e:
logging.error("Ticket init: %s"%(e))
raise e
def get(self):
# get ticket infor from Ticket API with FIXED time
self.headers = {
'content-type': "application/json"
}
try:
response = requests.request(self.method, self.url, data=json.dumps(self.payload), headers=self.headers,
verify=False)
# return json unicode data
if response.text:
return response.text
else:
return None
except Exception as e:
logging.error("Ticket get: %s"%(e))
raise e
def request_data(self):
# send a request to API with interval time
# time format for request Ticket API date
time_format = "%d/%m/%Y %H:%M:%S"
interval = self.config["ticket"]["interval"]
# convert system time to API time
self.payload["FromDate"] = (datetime.datetime.now() - datetime.timedelta(minutes=int(interval))).strftime(
time_format)
self.payload["ToDate"] = datetime.datetime.now().strftime(time_format)
try:
# return data from API response
return self.get()
except Exception as e:
logging.error("Ticket request_data %s: "%(e))
raise e
def ticket_status(self, from_date, to_date, code):
# send a request to update ticket status
time_format = "%d/%m/%Y %H:%M:%S" #datetime api format
try:
self.payload["FromDate"] = datetime.datetime.strptime(from_date, "%Y-%m-%d %H:%M:%S").strftime(time_format)
self.payload["ToDate"] = datetime.datetime.strptime(to_date, "%Y-%m-%d %H:%M:%S").strftime(time_format)
data = json.loads(self.get())["Result"]["Data"]
for idx, ticket in enumerate(data):
if ticket["TicketCode"] == code:
return ticket["TicketStatus"]
else:
pass
except Exception as e:
logging.error("Ticket ticket_status: %s"%(e))
raise e
class Database():
def __init__(self):
# load database config from config file
try:
dir_path = path.dirname(path.abspath(__file__))
filename = path.join(dir_path, "config.json")
with open(filename, "r") as config_file:
self.config = json.load(config_file)
self.host = self.config["database"]["host"]
self.dbname = self.config["database"]["dbname"]
self.table = self.config["database"]["table"]
self.usr = self.config["database"]["user"]
self.passwd = self.config["database"]["pass"]
except Exception as e:
raise e
# create a connection to database
try:
self.cnx = mysql.connector.connect(user=self.usr, password=self.passwd, host=self.host,
database=self.dbname)
self.cursor = self.cnx.cursor(buffered=True)
except Exception as e:
raise e
def get_data(self):
# get ticket info from database
try:
query = self.config["database"]["query"]["get_data"]
self.cursor.execute(query)
data = self.cursor.fetchall()
return data
except Exception as e:
raise e
def insert(self, *values):
# insert new data to database
try:
query = self.config["database"]["query"]["insert"]
self.cursor.execute(query, values)
# Commit your changes in the database
self.cnx.commit()
status = 1
except Exception as e:
status = -1
logging.error("Database insert: %s"%(e))
raise e
finally:
return status
def update(self, ticket_code, ticket_status):
# update data in database when is changed
try:
query = self.config["database"]["query"]["update"]
self.cursor.execute(query, (ticket_status, ticket_code,))
self.cnx.commit()
except Exception as e:
logging.error("Database update: %s"%(e))
raise e
def delete(self, ticket_code):
# delete data in database
try:
query = self.config["database"]["query"]["delete"]
self.cursor.execute(query, (ticket_code,))
self.cnx.commit()
except Exception as e:
logging.error("Database delete: %s"%(e))
raise e
def is_exist(self, ticket_code):
# search Ticket code in database, return 1 if exist, else return 0
try:
query = self.config["database"]["query"]["is_exist"]
self.cursor.execute(query, (ticket_code,))
data = self.cursor.fetchall()
if data:
# ticket is exist in db
status = 1
else:
# ticket not is exist in db
status = 0
except Exception as e:
status = -1
raise e
finally:
return status
def get_status(self, ticket_code):
# get ticket status from database
try:
query = self.config["database"]["query"]["ticket_status"]
self.cursor.execute(query, (ticket_code,))
data = self.cursor.fetchall()
return data
except Exception as e:
raise e
def get_long_time_data(self, timeline):
# get ticket data from database
try:
query = self.config["database"]["query"]["long_time_data"]
self.cursor.execute(query, (timeline,))
data = self.cursor.fetchall()
return data
except Exception as e:
raise e
def __del__(self):
self.cnx.close()
self.cursor.close()
#Updated by Manhvc
#28102020