I use to send notifications via Telegram in 3 different scenarios:
- myself
- in a group where the BOT is included
- to my wife
all these 3 scenarios does not fit the actual notification system in Domoticz that just handle one ID
so I use to run a brunch of os.execute and curl to send locally (within the script) all the notification I want.... but I dont like to put the bot Token everywhere...
SO... I finally wrote a small library to include in all scripts (dzVents included) that is able to send both messages and tvcc snapshot and would like to share with you.
It only runs under Linux....
save the following code to a file named Telegram.lua and put it in /home/pi/domoticz/scripts/lua
Code: Select all
local teleTok = 'Enter your Bot Token here'
local snapFile = '/home/pi/domoticzSnap'
local domoReq = 'http://localhost:8080/camsnapshot.jpg?idx='
local tgAb = {}
tgAb['myName1'] = 222222222
tgAb['myName2'] = 111111111
tgAb['myGroup'] = -00000000
local telegram ={};
function telegram.getId(nome)
return tgAb[nome]
end
function telegram.sendText(chatId, message)
return os.execute('curl --data chat_id='..chatId..' --data parse_mode=Markdown --data-urlencode "text='..message..'" "https://api.telegram.org/bot'..teleTok..'/sendMessage" ')
end
function telegram.sendImage(chatId, camChannel)
os.execute('wget -O "'..snapFile..camChannel..'.jpg" "'..domoReq..camChannel..'"')
return os.execute('curl -s -X POST "https://api.telegram.org/bot'..teleTok..'/sendPhoto?chat_id='..chatId..'" -F photo="@'..snapFile..camChannel..'.jpg"')
end
function telegram.sendDoc(chatId, filePath)
return os.execute('curl -s -X POST "https://api.telegram.org/bot'..teleTok..'/sendDocument?chat_id='..chatId..'" -F document="@'..filePath..'"')
end
return telegram
Please note that the library returns a boolean value for the success or not of the operation, so store it somewhere (in the example I put the result in the log)
Code: Select all
package.path = package.path .. ';' .. '/home/pi/domoticz/scripts/lua/?.lua'
local telegram = require('Telegram')
Send a text Message (Markdown formatting supported)
usage:
telegram.sendText(chatId, Message)
example
Code: Select all
result = telegram.sendText(123456789, 'This is a *TEST* Message')
usage:
telegram.sendImage(chatId, Camera Channel IDX)
example
Code: Select all
result = telegram.sendImage(123456789, 1)
usage:
telegram.sendDoc(chatId, full_file_path_and_name)
example
Code: Select all
result = telegram.sendImage(123456789, '/home/pi/domoticz/domoticz.db')
le local array tgAb can be filled with the chatId and referred to a person/group
compile (and add raw if needed) using the syntax: tgAb['name you want to use to identify the chat'] = chatid
then, in your script use it like:
Code: Select all
local chatid = telegram.getId('name you want to use to identify the chat')
or, use it straight in the sendText function:
Code: Select all
telegram.sendText(telegram.getId('name you want to use to identify the chat'), 'This is a Message')
ciao
M