-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbarcoder.py
More file actions
131 lines (112 loc) · 4.88 KB
/
barcoder.py
File metadata and controls
131 lines (112 loc) · 4.88 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
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
from flask import Flask, render_template, request
import requests
import json
# pip install python-telegram-bot
# from telegram import Bot
app = Flask(__name__)
def load_config():
try:
with open('config.json', 'r') as config_file:
config_data = json.load(config_file)
return config_data
except FileNotFoundError:
print("Не найден файл конфигурации 'config.json'.")
return None
def get_inventory_data(api_url, api_key, inventory_number):
headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json',
}
url = f'{api_url}/hardware/bytag/{inventory_number}'
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
else:
error_json = {
"status": "error",
"messages": response.json().get('messages', f"Error: {response.status_code}"),
"payload": None
}
return error_json
def extract_values(data):
if not data:
return None, {"status": "error", "messages": "No data received from API", "payload": None}
if 'status' in data and data['status'] == 'error':
return None, data
elif data:
status_label = data.get('status_label', {}).get('name', '')
assigned_to_username = ''
if data.get('assigned_to'):
assigned_to_username = data.get('assigned_to', {}).get('username', 'Unknown')
notes = data.get('notes', '')
model = data.get('model', {}).get('name') or ''
serial = data.get('serial', '')
rtd_location_data = data.get('rtd_location', {})
rtd_location = rtd_location_data.get('name', '') if isinstance(rtd_location_data, dict) else ''
if data.get('image', '') is not None:
image = data.get('image', '')
else:
image = 'image.png'
return {
'status_label': status_label,
'assigned_to_username': assigned_to_username,
'notes': notes,
'model': model,
'serial': serial,
'rtd_location': rtd_location,
'image': image
}, None
else:
return None, None
def send_telegram_message(token, chat_id, thread_id, message):
url = f'https://api.telegram.org/bot{token}/sendMessage'
if thread_id:
params = {'message_thread_id': "f{thread_id}", 'chat_id': f"{chat_id}_{thread_id}", 'text': message}
else:
params = {'chat_id': f"{chat_id}", 'text': message}
response = requests.get(url, params=params)
if response.status_code != 200:
print(f"Failed to send message to Telegram. Status code: {response.status_code}")
@app.route('/', methods=['GET', 'POST'])
def index():
status_label = None
assigned_to_username = None
notes = None
model = None
serial = None
rtd_location = None
error_message = None
image = ''
asset_number = ''
if request.method == 'POST':
config = load_config()
if not config:
error_message = "Ошибка загрузки конфигурации."
else:
api_url = config.get('api_url', '')
api_key = config.get('api_key', '')
telegram_token = config.get('TELEGRAM_BOT_TOKEN', '')
telegram_chat_id = config.get('TELEGRAM_CHAT_ID', '')
thread_id = config.get('thread_id', '')
inventory_number = request.form.get('inventory_number', '')
data = get_inventory_data(api_url, api_key, inventory_number)
asset_number = inventory_number
values, error = extract_values(data)
print("Values:", values)
if values:
status_label = values['status_label']
assigned_to_username = values['assigned_to_username']
notes = values['notes']
model = values['model']
serial = values['serial']
rtd_location = values['rtd_location']
image = values['image']
print(f"Поиск {inventory_number} прошел успешно: {assigned_to_username}")
send_telegram_message(telegram_token, telegram_chat_id, thread_id, f"Поиск {inventory_number} прошел успешно: {values}")
elif error:
error_message = error.get('messages', 'Неизвестная ошибка.')
send_telegram_message(telegram_token, telegram_chat_id, thread_id, f"Ошибка при поиске {inventory_number}: {error_message}")
return render_template('index.html', status_label=status_label, assigned_to_username=assigned_to_username,
notes=notes, model=model, serial=serial, rtd_location=rtd_location, error_message=error_message, image=image, asset_number=asset_number)
if __name__ == '__main__':
app.run(debug=True, port=8885, host='0.0.0.0')