Commit afc703fd authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

telegram alerts incorporated into user views

parent 6f9b910b
This diff is collapsed.
......@@ -83,7 +83,7 @@ def get_alerts_list(cur, appkey):
WHERE
app_key = %s
AND
action_type = 'alert'
action_type LIKE 'alert_%%'
"""
cur.execute(query, (appkey,))
......
......@@ -9,6 +9,8 @@ import app.helpers.misc as misc
import app.dao.device.device as dd
import app.helpers.device_data_model as ddm
import app.helpers.telegram_sender as ts
from binascii import unhexlify
@thread
......@@ -28,10 +30,14 @@ def listening():
dev = dd.get(d['appkey'], d['devid'])[1]
d['message']['data'] = ddm.read_data(unhexlify(d['message']['data'][2:]), dev[3])
if d['lvalue'] in d['message']['data'] and (d['op'][0] == 'O' or eval(str(d['message']['data'][d['lvalue']]) + d['op'] + d['rvalue'])):
if d['action_type'] == 'alert':
if d['action_type'] == 'alert_email':
# send mail
n = nf.get(d['appkey'], d['devid'], d['nfid'])[1]
mailer.send_mail(app, n, d)
elif d['action_type'] == 'alert_telegram':
# send telegram message
n = nf.get(d['appkey'], d['devid'], d['nfid'])[1]
ts.send_message(app, n, d)
elif d['action_type'] == 'automation':
# enqueue confid
# action format: '<devid>#<confid>#<arg>'
......
from flask import render_template
import app.dao.application.application as ad
import telegram
def send(msg, chat_id, token):
bot = telegram.Bot(token=token)
bot.sendMessage(chat_id, text=msg, parse_mode=telegram.ParseMode.HTML)
def send_message(app, alert, fired):
with app.app_context():
ap = ad.get(fired['appkey'])
context = {'alert':alert, 'username':ap[1][2], 'appname':ap[1][0], 'message':fired['message']}
msg = render_template('mailing/telegram_alert.html', **context)
send(msg, alert[6], app.config['TELEGRAM_BOT_TOKEN'])
Hello {{ username }},
The alert {{ alert[3] }} from your {{ appname }} application has been fired at {{ message['timedate'] }} with the next message: {{ message['data'] }}.
Remember the condition of the alert was {{ alert[4] }}.
Take care,
HPC&A IoT Server
......@@ -29,7 +29,8 @@
<thead>
<th> Name </th>
<th> Condition </th>
<th> Email </th>
<th> Alert Type </th>
<th> Receiver </th>
<th> </th>
</thead>
<tbody>
......@@ -37,6 +38,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>
......
......@@ -51,17 +51,25 @@
<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>
-->
</form>
<br>
<h4> THEN </h4>
<h5> Send email to: </h5>
<h5> Select alert type: </h5>
<input class="form-control" type="email" form="alertform" id="alertemail" name="alertemail" placeholder="Type email..." required>
<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="text" id="alertemail" name="alertemail" placeholder="Type email..." required>
<p id="hint"></p>
<br><br>
<div class="form-group">
<button type="submit" id="submit" form="alertform" class="btn btn-primary">Create Alert</button>
<button type="submit" id="submit" class="btn btn-primary">Create Alert</button>
</div>
</form>
</div>
</div>
......@@ -110,5 +118,21 @@
input.setAttribute("required", "true");
}
}
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 %}
......@@ -511,20 +511,20 @@ def application_new_alert(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
t = tr.create_function_rt(appkey, request.form['devid'], nid, [request.form['varname'],request.form['operation'],request.form['avalue']],'alert',request.form['alertemail'])
t = 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'])
t = tr.create(appkey, request.form['devid'], nid)
flash('Alert created', 'success')
app.logger.info('%s %s created alert %s - %s for application %s', session['alert'], session['name'], nid, desc, appkey)
app.logger.info('%s %s created alert %s - %s for application %s', session['role'], session['name'], nid, desc, appkey)
return redirect(url_for('application_alerts', appkey=appkey))
else:
app.logger.error('%s %s failed to create alert for application %s - %s', session['role'], session['name'], appkey, res[1])
flash('Error creating new alert: {}'.format(res[1]), 'danger')
return redirect(request.url)
except Exception as e:
app.logger.error('%s %s failed to create alert for application %s - %s', session['role'], session['name'], appkey, e)
app.logger.error('%s %s failed to create alert for application %s - Exception: %s', session['role'], session['name'], appkey, e)
flash('Error creating new alert: {}. Make sure you have filled all form fields.'.format(e), 'danger')
return redirect(request.url)
......
......@@ -32,6 +32,8 @@ class Config(object):
MAIL_PASSWORD = 'HPC&A10T.'
MAIL_DEFAULT_SENDER = 'hpcaiotserver@gmail.com'
TELEGRAM_BOT_TOKEN = '1240239295:AAE-RLGTRmUxds_yzk5l5mtl-hLhOaC_-50'
# in minutes
FIRE_NOTIFICATIONS_INTERVAL = 1
......
APScheduler==3.6.3
bcrypt==3.1.7
blinker==1.4
certifi==2020.6.20
cffi==1.14.0
click==7.1.1
cryptography==2.9.2
decorator==4.4.2
Flask==1.1.2
Flask-Mail==0.9.1
itsdangerous==1.1.0
......@@ -12,8 +15,10 @@ msgpack==1.0.0
pgpubsub==0.0.5
psycopg2==2.8.5
pycparser==2.20
python-telegram-bot==12.8
pytz==2019.3
six==1.14.0
tornado==6.0.4
tzlocal==2.0.0
uWSGI==2.0.18
Werkzeug==1.0.1
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