A little Small Lua Telegram Library

Post Reply
User avatar
emme
Posts: 897
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi
Domoticz version: latest
Location: Milano, Italy
Contact:

A little Small Lua Telegram Library

Post by emme » Tuesday 02 January 2018 23:57

Ciao,

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
then in your code add the reference library.
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')
Actually I only included few option:
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')
Send a Camera SnapShot
usage:
telegram.sendImage(chatId, Camera Channel IDX)
example

Code: Select all

result = telegram.sendImage(123456789, 1)
Send a document (useful to send animated GIF also!!)
usage:
telegram.sendDoc(chatId, full_file_path_and_name)
example

Code: Select all

result = telegram.sendImage(123456789, '/home/pi/domoticz/domoticz.db')
AddressBook
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')
I hope you like it :P
ciao
M
Last edited by emme on Monday 08 January 2018 22:45, edited 6 times in total.
The most dangerous phrase in any language is:
"We always done this way"

bigpea
Posts: 121
Joined: Thursday 11 August 2016 12:30
Target OS: Raspberry Pi
Domoticz version:
Location: IT
Contact:

Re: A little Small Lua Telegram Library

Post by bigpea » Wednesday 03 January 2018 15:27

Great idea and great script. :mrgreen:
It works very well, thanks!

User avatar
emme
Posts: 897
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: A little Small Lua Telegram Library

Post by emme » Monday 08 January 2018 22:46

added function to send docs and a chatid addressbook to fast references in your code
The most dangerous phrase in any language is:
"We always done this way"

wlevels
Posts: 19
Joined: Friday 16 September 2016 13:54
Target OS: Raspberry Pi
Domoticz version: V4.9700
Location: The Netherlands
Contact:

Re: A little Small Lua Telegram Library

Post by wlevels » Monday 09 April 2018 10:48

Thanks @emme just what I was looking for!

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests