Page 1 of 1

Dzvents - Calling a local function

Posted: Wednesday 18 July 2018 20:10
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?

Re: Dzvents - Calling a local function

Posted: Wednesday 18 July 2018 20:33
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.

Re: Dzvents - Calling a local function

Posted: Wednesday 18 July 2018 20:48
by paul402
Wow! That was fast and understandable. Thank you so much. :)