Dzvents - Calling a local function

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
paul402
Posts: 105
Joined: Monday 23 May 2016 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Dzvents - Calling a local function

Post by paul402 »

Please point me in the right direction to do this. My script functions great and sends me an email with data when required.
I would like to send a Telegram as well.

I have this telegram function and want to call it.

Code: Select all

local function telegram(msg) 
domoticz.openURL('https://api.telegram.org/bot"your BOT API key"/sendMessage?chat_id="Your chat-ID"&text=' .. msg) 
end 
Do I put it in a separate script called telegram, or above/below my code, or somewhere else?
elmortero
Posts: 248
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Dzvents - Calling a local function

Post by elmortero »

Hi,
In order to call that function from any dzvents script I have put a helper in my global_data.lua (more info Here
The content:

Code: Select all

        	telegram = function(domoticz, msg, destin)
	            local Msg = domoticz.urlEncode(msg)
				local destin = string.lower(destin)
					if destin == 'NAME1' then
					local nmaURL = "https://api.telegram.org/bot"your BOT API key"/sendMessage?chat_id=CHATID1&text="..Msg.."&"
					domoticz.openURL(nmaURL)
					elseif destin == 'NAME2' then
					local nmaURL = "https://api.telegram.org/bot"your BOT API key"/sendMessage?chat_id=CHATID2&text="..Msg.."&"	
					domoticz.openURL(nmaURL)
					elseif destin == 'both' then
					local nmaURL = "https://api.telegram.org/bot"your BOT API key"/sendMessage?chat_id=CHATID1&text="..Msg.."&"
					domoticz.openURL(nmaURL)
					local nmaURL = "https://api.telegram.org/bot"your BOT API key"/sendMessage?chat_id=CHATID2&text="..Msg.."&"	
					domoticz.openURL(nmaURL)				
					end
			end,
From a dzvents script I can then call the function this way
domoticz.helpers.telegram(domoticz, msg, 'NAME1')
If you only need to send to 1 chat-ID you don't need the IFs of course and put it directly in the url in your helper. In my case I have 2 possible destinations and the possibility to send to both at the same time.

Been using this ever since NotifyMyAndroid stopped their services.

Hope this helps you.
paul402
Posts: 105
Joined: Monday 23 May 2016 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Dzvents - Calling a local function

Post by paul402 »

Wow! That was fast and understandable. Thank you so much. :)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest