Create a Bot on Telegram
- Find BotFather on Telegram
- Use command /newbot and follow BotFather's guide to create a bot.
- Get your BOT_HTTP_API Token.
- Click on the link of your new bot and send some dummy messages to the bot.
-
Visit this URL by replacing BOT_HTTP_API with the token you got
in the previous step and get your CHAT_ID
https://api.telegram.org/bot
BOT_HTTP_API
/getUpdates
Note: If you don’t see the id, send more dummy messages to your bot and revisit the URL. After this step, we will have BOT_HTTP_API and CHAT_ID.
Set Alerts for Anolla and Intrusion
Asilla SDK will send alerts for abnormal actions and intrusion detection to your Telegram bot. The alert includes a gif file with the screenshot of when the incident happens and the human pose estimation, and other information such as recognition (name of abnormal action), location (camera name), event time (YYYY-MM-DD HH:MM:SS).
Important: You need to keep this running in the background to get alerts. Therefore, we recommend to use the same device where you installed Asilla SDK client.
-
Create a Python script name app.py and replace XXX:YYYYY with
your BOT_HTTP_API and ZZZZZZZ with your CHAT_ID.
# -*- coding: utf-8 -*- from flask import Flask, escape, request, jsonify from datetime import datetime, timedelta from io import BytesIO import os.path import json import base64 import requests app = Flask(__name__) ABNORMAL_UNKNOWN_ACTION = -4 STAY_ACTION = -2 ABNORMAL_ACTION = -1 STAGGERING_ACTION = 4 FIGHTING_ACTION = 5 FALLING_ACTION = 6 INTRUSION_ACTION = 8 DISPLAY_ACTIONS = { ABNORMAL_UNKNOWN_ACTION : "abnormal", STAY_ACTION : "stay", ABNORMAL_ACTION : "abnormal", STAGGERING_ACTION : "staggering", FIGHTING_ACTION : "fighting", FALLING_ACTION : "falling", INTRUSION_ACTION : "intrusion" } @app.route('/receive_abnormal_alert', methods = ['POST']) def main(): # Replace the XXX:YYYYY with your BOT HTTP API Token TELEGRAM_URL = "https://api.telegram.org/botXXX:YYYYY/sendPhoto" TELEGRAM_URL_GIF = "https://api.telegram.org/botXXX:YYYYY/sendAnimation" # Replace the ZZZZZZZ with your CHAT ID CHAT_ID = "ZZZZZZZ" bodyInfo = request.json time_format = '%Y-%m-%dT%H:%M:%S' eventTime = str(bodyInfo['eventTime']) eventTime = datetime.strptime(eventTime.split('.')[0], time_format) msg = ("Recognition: " + parse_recognition(bodyInfo['recognition']) + "\n" "Location: " + bodyInfo['location'] + "\n" "AttachedImageType: " + bodyInfo['attachedImageType'] + "\n" "EventTime: " + eventTime.strftime('%Y-%m-%d %H:%M:%S') + "\n" ) if "device" in bodyInfo: msg = msg + "Device: " + str(bodyInfo["device"]) + "\n" if "uid" in bodyInfo: msg = msg + "TimeID: " + str(bodyInfo["uid"]) + "\n" send_info = {'chat_id' : CHAT_ID, 'caption' : msg } upload_files = bodyInfo['attachedImages'] file_content = base64.b64decode(upload_files[0]) files = { 'photo': ('Image_' + bodyInfo['eventTime'] + "." + bodyInfo['attachedImageType'], BytesIO(file_content)) } url = TELEGRAM_URL if bodyInfo['attachedImageType'].lower() == "gif" : url = TELEGRAM_URL_GIF files = { 'animation': ('Image_' + bodyInfo['eventTime'] + "." + bodyInfo['attachedImageType'], BytesIO(file_content)) } r = requests.post(url, data=send_info, files=files) res = {'status': 'OK', 'message': 'Sent alert to Telegram.'} return jsonify(res) def parse_recognition(recognition): if isinstance(recognition, list): labels = "" for item in recognition: action_name = DISPLAY_ACTIONS.get(int(item["type"]), str(item["type"])) labels += ", " + item["label"] + "(" + action_name + ")" labels = labels.replace(", ", "", 1) return labels else: return recognition if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=5050) #run app in debug mode on port 5050
-
Install Flask (if don't have)
pip install -U Flask
-
Run Webhook
python3 app.py
-
Set Webhook to Asilla SDK Client by running below command in a
new terminal:
curl -X POST http://
SDK_IP_ADDRESS
:5000/setCommonCamConfig \
-H "Content-Type: application/json" \
-d '{"config_info": {"alert_api": "http://WEBHOOK_IP_ADDRESS
:5050/receive_abnormal_alert"}}'SDK_IP_ADDRESS is the IP Address of your machine you have installed Asilla SDK Client.
WEBHOOK_IP_ADDRESS is the IP Address of the machine you're running the webhook on. -
Enable sending alert by running below command:
curl -X POST http://
SDK_IP_ADDRESS
:5000/setSpecificCamConfig \
-H "Content-Type: application/json" \
-d '{"cam_id":CAMERA_ID
, "config_info": {"alert_enable": true}}'SDK_IP_ADDRESS is the IP Address of your machine you have installed Asilla SDK Client.
CAMERA_ID is the ID of the Camera that you have configured. Example: 1, 2, 3 -
A sample alert
Set Alerts for Linecross
Asilla SDK will send Line-Cross alerts every 60 seconds. You can change the frequency by using this API to configure lc_count_frequency parameter.
Important: You need to keep this running in the background to get alerts. Therefore, we recommend to use the same device where you installed Asilla SDK client.
-
Create a Python script name app.py and replace XXX:YYYYY with
your BOT_HTTP_API and ZZZZZZZ with your CHAT_ID.
# -*- coding: utf-8 -*- from flask import Flask, escape, request, jsonify from datetime import datetime, timedelta from io import BytesIO import os.path import json import base64 import requests app = Flask(__name__) @app.route('/receive_line_cross_alert', methods=['POST']) def main(): # Replace the XXX:YYYYY with your BOT HTTP API Token TELEGRAM_URL_MESSAGE = "https://api.telegram.org/botXXX:YYYYY/sendMessage" # Replace the ZZZZZZZ with your CHAT ID CHAT_ID = ""ZZZZZZZ"" bodyInfo = request.json time_format = '%Y-%m-%dT%H:%M:%S' eventTime = str(bodyInfo['eventTime']) eventTime = datetime.strptime(eventTime.split('.')[0], time_format) msg = "Location: " + str(bodyInfo['location']) + "\n" if "device" in bodyInfo: msg += "Device: " + str(bodyInfo["device"]) + "\n" line_cross = bodyInfo['line_cross'] if len(line_cross) != 0: line_cross = bodyInfo['line_cross'] msg += "======Line crossing status======"+ "\n" msg += ("In: " + str(line_cross['in']) + "\n" "Out: " + str(line_cross['out']) + "\n" ) send_info = {'chat_id': CHAT_ID, 'text': msg } url = TELEGRAM_URL_MESSAGE requests.post(url, data=send_info) res = {'status': 'OK', 'message': 'Sent alert to Telegram.'} return jsonify(res) return jsonify({'status': 'OK', 'message': 'No message to send!'}) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=5050) # run app in debug mode on port 5050
-
Install Flask (if don't have)
pip install -U Flask
-
Run Webhook
python3 app.py
-
Set Webhook to Asilla SDK Client by running below command in a
new terminal:
curl -X POST http://
SDK_IP_ADDRESS
:5000/setCommonCamConfig \
-H "Content-Type: application/json" \
-d '{"config_info": {"alert_api": "http://WEBHOOK_IP_ADDRESS
:5050/receive_line_cross_alert"}}'SDK_IP_ADDRESS is the IP Address of your machine you have installed Asilla SDK Client.
WEBHOOK_IP_ADDRESS is the IP Address of the machine you're running the webhook on. -
Enable sending alert by running below command:
curl -X POST http://
SDK_IP_ADDRESS
:5000/setSpecificCamConfig \
-H "Content-Type: application/json" \
-d '{"cam_id":CAMERA_ID
, "config_info": {"alert_enable": true}}'SDK_IP_ADDRESS is the IP Address of your machine you have installed Asilla SDK Client.
CAMERA_ID is the ID of the Camera that you have configured. Example: 1, 2, 3 -
A sample alert