A little Small Lua Telegram Library

Moderator: leecollings

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

Re: A little Small Lua Telegram Library

Post by emme »

WOW!!! this is awesome!!!

And probably will fix my connection problem I do have (fix date for my internet connection is March 29th.... :evil: :evil: :evil: :evil: )
I'm actually running a 3G (10Mbit) connection and my domoticz system is under pressure for a lot of queued messages and external connection.

I'll try this immediately and see if this solve that issue!!!

Ciao
M
The most dangerous phrase in any language is:
"We always done this way"
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: A little Small Lua Telegram Library

Post by emme »

Created page on the Wiki about the library
The most dangerous phrase in any language is:
"We always done this way"
snuiter
Posts: 67
Joined: Saturday 17 June 2017 12:30
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: A little Small Lua Telegram Library

Post by snuiter »

Hi Emme,
you need to rename the page so it is easer to find ;)

Telegra lua library

The M is missing, I guess I will never forget your forum name now ;)
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: A little Small Lua Telegram Library

Post by emme »

:o :o :o :o :o :o :o

I'm totally unfamiliar with mediawiki.....

And with the alphabet :D :D
The most dangerous phrase in any language is:
"We always done this way"
zicht
Posts: 251
Joined: Sunday 11 May 2014 11:09
Target OS: Windows
Domoticz version: 2023.1+
Location: NL
Contact:

Re: A little Small Lua Telegram Library

Post by zicht »

Found the wiki, nice job.

ps
If you update it with the simple addons i posted a couple of messages back you make sure it does not slow down domoticz when the network is lagging
Rpi & Win x64. Using : cam's,RFXCom, LaCrosse, RFY, HuE, google, standard Lua, Tasker, Waze traveltime, NLAlert&grip2+,curtains, vacuum, audioreceiver, smart-heating&cooling + many more (= automate all repetitive simple tasks)
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: A little Small Lua Telegram Library

Post by freijn »

Guys,

Can you please help me ?

I have copied the library Telegram.lua and put it in /home/pi/domoticz/scripts/lua.

but now... where do I put this code ? :

package.path = package.path .. ';' .. '/home/pi/domoticz/scripts/lua/?.lua'
local telegram = require('Telegram')

and would just like to exe
telegram.sendText(123456789,'Hallo World!')

many thanks for your time ,

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

Re: A little Small Lua Telegram Library

Post by emme »

freijn wrote: Tuesday 30 April 2019 22:06 but now... where do I put this code ? :
put the

Code: Select all

package.path = package.path .. ';' .. '/home/pi/domoticz/scripts/lua/?.lua'
local telegram = require('Telegram')

on top of your script where you want to send the

Code: Select all

telegram.sendText(123456789,'Hallo World!')
that's all
The most dangerous phrase in any language is:
"We always done this way"
riko
Posts: 90
Joined: Saturday 22 August 2020 13:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: A little Small Lua Telegram Library

Post by riko »

Cool script, exactly what I was looking for. I only have one question left:

Can I send the message to multiple IDs in one line?

Code: Select all

telegram.sendText(telegram.getId('rik'), 'This is a *TEST* Message')
I have tried:

Code: Select all

telegram.sendText(telegram.getId('rik', 'name2'), 'This is a *TEST* Message')
Which is not working. Or is the only method to copy the telegram send line each time?
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: A little Small Lua Telegram Library

Post by emme »

You have ti duplicate the line at the Moment

I moved back to domoticz After a while into another system

I could have a look into it and duplicate It
Ciao
M
The most dangerous phrase in any language is:
"We always done this way"
riko
Posts: 90
Joined: Saturday 22 August 2020 13:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: A little Small Lua Telegram Library

Post by riko »

emme wrote: Monday 21 September 2020 21:28 You have ti duplicate the line at the Moment

I moved back to domoticz After a while into another system

I could have a look into it and duplicate It
Ciao
M
Thanks for your quick reply. I'll duplicate the lines for now. Please let me know when you have found another solution!
User avatar
Xenomes
Posts: 379
Joined: Tuesday 27 November 2018 19:05
Target OS: Linux
Domoticz version: 2024.7
Location: Netherlands
Contact:

Re: A little Small Lua Telegram Library

Post by Xenomes »

emme wrote: Monday 08 January 2018 21:46 added function to send docs and a chatid addressbook to fast references in your code
I have add sendvideo support to your script and embedded the addressbook function.

Code: Select all

local teleTok   = 'Enter your Bot Token here'
local snapFile  = '/home/pi/domoticzSnap'
local snapVideo = '/var/tmp/video.mp4'
local domoReq   = 'http://localhost:8080/camsnapshot.jpg'
local tgAb = {}
tgAb['myName1'] = 222222222
tgAb['myName2'] = 111111111
tgAb['myGroup'] = -00000000
local cameraUri = {}
cameraUri['cam1'] = 'rtsp://user:password@ip-cam/channel'
cameraUri['cam2'] = 'rtsp://admin:[email protected]/12'
cameraUri['cam3'] = 'rtsp://admin:[email protected]/1'

local telegram ={};

    function getId(name)
        return tgAb[name]
    end

    function getCam(num)
        return cameraUri[num]
    end
	
	-- example: telegram.sendText('myName1', 'Test message')
    function telegram.sendText(chatId, message)
        return os.execute('curl --data chat_id='..getId(chatId)..' --data parse_mode=Markdown --data-urlencode "text='..message..'"  "https://api.telegram.org/bot'..teleTok..'/sendMessage" ')
    end

	-- example: telegram.sendImage('myName1', 1, 'Test message')
    function telegram.sendImage(chatId, camChannel, message)
        os.execute('wget -O "'..snapFile..camChannel..'.jpg" "'..domoReq..'?idx='..camChannel..'"')
        return os.execute('curl -s -X POST "https://api.telegram.org/bot'..teleTok..'/sendPhoto?chat_id='..getId(chatId)..'" -F photo="@'..snapFile..camChannel..'.jpg" -F caption="'..message..'"')
    end

	-- example: telegram.sendDoc('myName1', '/home/pi/file.pdf', 'Test message')
    function telegram.sendDoc(chatId, filePath, message)
        return os.execute('curl -s -X POST "https://api.telegram.org/bot'..teleTok..'/sendDocument?chat_id='..getId(chatId)..'" -F document="@'..filePath..'" -F caption="'..message..'"')
    end

	-- example: telegram.sendVideo('myName1', 'cam1', 10, 'Test message')
    function telegram.sendVideo(chatId, cameraNum, recTime, message)
        os.execute('ffmpeg -y -i "'..getCam(cameraNum)..'" -r 30 -vcodec copy -an -t '..recTime..' "'..snapVideo..'"')
        return os.execute('curl -s -X POST "https://api.telegram.org/bot'..teleTok..'/sendVideo?chat_id='..getId(chatId)..'" -F video="@'..snapVideo..'" -F caption="'..message..'"')

    end

return telegram
HP T630 (32GB SSD/8GB Mem) - Ubuntu 22.04.4 LTS (64Bit) - Domoticz 2024.7 with Machinon theme - RFLink - KaKu - Sonoff - Tasmota - Shelly - MQTT2Zigbee - OpenTherm Gateway - Tinytuya - IR Blaster - P1 Smart Meter - NPN Watermeter - Google Assistant
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: A little Small Lua Telegram Library

Post by emme »

that's great!!!
thenk you
The most dangerous phrase in any language is:
"We always done this way"
cimbali
Posts: 4
Joined: Friday 21 May 2021 13:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: A little Small Lua Telegram Library

Post by cimbali »

Very good library. This inspired to me to make it one similar for sending message & pictures to Pushbullet.

One downside of a pure-Lua approach for this problem is that it makes the dzVent event handling loop busy for a few seconds, the time it takes to capture the picture from the camera and call the APIs on Pushbullet backend.

As far as I understand the event handling loop will be unable to handle any other events in those few seconds, and you may see the following messages in the domoticz log that warn you about that.

Code: Select all

    dzVents: !Info: ------ Finished myscript.lua after >9 seconds. (using 0.871 seconds CPU time !)
To fix this I have decided to still use the library, but spawn a separate Lua process like this:

Code: Select all

os.execute('cd /home/pi/domoticz/scripts/dzVents/scripts/modules/ ; /usr/bin/lua tgsendpic.lua ' .. '"' .. cameraId .. '"' .. ' "' .. message .. '" &')
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests