Commit 5b4011ee authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

telegram alerts integrated to admin views

parent 155f984b
......@@ -371,3 +371,27 @@ Traceback (most recent call last):
jinja2.exceptions.TemplateSyntaxError: expected token 'end of print statement', got '['
[2020-06-30 17:18:31,111] INFO in views: superuser admin created alert 8bbd83 - devmpack.lever == 1 for application 3b56f3d8
[2020-06-30 17:19:11,846] INFO in views: superuser admin created alert ddb142 - ddmjson.lever == 0 for application 3b56f3d8
[2020-06-30 17:41:15,560] ERROR in app: Exception on /administration/admin/recent-activity [GET]
Traceback (most recent call last):
File "/home/pi/thso.server/env/lib/python3.7/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/home/pi/thso.server/env/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/pi/thso.server/env/lib/python3.7/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/pi/thso.server/env/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/pi/thso.server/env/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/home/pi/thso.server/env/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "./app/helpers/decorators.py", line 23, in restricted_function
return f(*args, **kwargs)
File "./app/views_admin.py", line 518, in administration_user_recent_activity
ra += '<tr><th scope="row">'+r[1]+'</th><th>'+r[2]+'</th><th>'+r[0]+'</th><th>'+str(ddm.read_data(r[3], dev[3]))+'</th></tr>'
File "./app/helpers/device_data_model.py", line 147, in read_data
return json.loads(data.decode('utf-8'))
AttributeError: 'memoryview' object has no attribute 'decode'
[2020-06-30 17:44:34,584] WARNING in views_admin: Administrator admin created alert 7ab5c9 for application 3b56f3d8 for admin
[2020-06-30 17:52:07,946] WARNING in views_admin: Administrator admin created alert 3235b2 for application 3b56f3d8 for admin
[2020-06-30 17:52:27,874] WARNING in views_admin: Administrator admin deleted alert 3235b2 for application 3b56f3d8 for admin
......@@ -49,7 +49,6 @@ def listening():
app.logger.info('Automation for application {} with format {} was fired.'.format(d['appkey'], d['action']))
except Exception as e:
app.logger.error('Notification manager thread error: {}'.format(e))
logger.
pass
......
......@@ -35,7 +35,8 @@
<thead>
<th> Name </th>
<th> Condition </th>
<th> Email </th>
<th> Alert Type </th>
<th> Receiver </th>
<th> </th>
</thead>
<tbody>
......@@ -43,6 +44,7 @@
<tr>
<th> {{ a[3] }} </th>
<th> {{ a[4] }} </th>
<th> {{ a[5].split('_')[1] }} </th>
<th> {{ a[6] }} </th>
<th> <a href="javascript:void(0)" onclick="return remove_alert('{{ a[0] }}', '{{ a[2] }}');"> <span class="fa fa-remove"</span> </a> </th>
</tr>
......
......@@ -52,14 +52,20 @@
</select>
<br>
<input type='text' class='form-control notifelem' name='avalue' id='avalue' placeholder='Value' title='use n (e.g. 5) for integer notation, n.n (e.g. 5.0) for float, and true or false for boolean' required>
<p> Use n (e.g. 5) for integer notation, n.n (e.g. 5.0) for float, and true or false for boolean. <br>
Keep types consistently for each variable for both, alerts and automations. </p>
<br>
<h4> THEN </h4>
<h5> Send email to: </h5>
<select class='form-control notifelem' name='alerttype' id='alerttype' onchange="return onalerttype();" required>
<option selected="selected" value="email">Email</option>
<option value="telegram">Telegram Message</option>
<option value="sms">SMS Message</option>
</select>
<br>
<input class="form-control" type="email" id="alertemail" name="alertemail" placeholder="Type email..." required>
<input class="form-control" type="text" id="alertemail" name="alertemail" placeholder="Type email..." required>
<p id="hint"></p>
<br><br>
<div class="form-group">
<button type="submit" id="submit" class="btn btn-primary">Create Alert</button>
......@@ -99,5 +105,21 @@
}
validate_form();
}
function onalerttype() {
var dev_sel = document.getElementById("alerttype");
var sel_op = dev_sel.options[dev_sel.selectedIndex].text;
var input = document.getElementById("alertemail");
$("#hint").text("");
if (sel_op[0] == "E") {
input.setAttribute("placeholder", "Type email...");
} else if (sel_op[0] == "T") {
input.setAttribute("placeholder", "Type chat id...");
$("#hint").text("Hint: send /start to @userinfobot bot.");
} else if (sel_op[0] == "S") {
input.setAttribute("placeholder", "Type telephone number...");
}
}
</script>
{% endblock %}
......@@ -237,10 +237,10 @@ def administration_user_application_new_alert(name, appkey):
try:
desc = dev[1][0]+'.'+request.form['varname']+' '+request.form['operation']+' '+request.form['avalue']
res = nfs.create(nid, appkey, request.form['devid'], request.form['alertname'], desc, 'alert', request.form['alertemail'])
res = nfs.create(nid, appkey, request.form['devid'], request.form['alertname'], desc, 'alert_'+request.form['alerttype'], request.form['alertemail'])
if res[0]:
# create new function and trigger
tr.create_function(appkey, request.form['devid'], nid, [request.form['varname'],request.form['operation'],request.form['avalue']])
tr.create_function_rt(appkey, request.form['devid'], nid, [request.form['varname'],request.form['operation'],request.form['avalue']],'alert_'+request.form['alerttype'], request.form['alertemail'])
tr.create(appkey, request.form['devid'], nid)
app.logger.warning('Administrator %s created alert %s for application %s for %s', session['name'], nid, appkey, name)
flash('Alert created', 'success')
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment