Code: Select all
-----------------------------------------------------------------------------
-- ALLINONE MESSAGE SCENE -- TELEGRAM/PUSH/POPUP/API.POST
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
0.0.9 Started with Telegram and push function
1.0.0 Added popup and api.post functions
-- SCENE DESCRIPTION --------------------------------------------------------
This scene will send message to your smartphone, which function that will run
depends on what string you send and to which variable.
Example:
-- TELEGRAM -----------------------------------------------------------------
Drop your message to TelegramMessage variable and this scene will send it to you
Demands a telegram token and chatid
--
fibaro:setGlobal("TelegramMessage", "YOUR MESSAGE")
--
-- PUSHSMARTPHONE -----------------------------------------------------------
Drop your message to SmartPhoneMessage variable and this scene will send it to
your smarthphone as push
--
fibaro:setGlobal("SmartPhoneMessage", "YOUR MESSAGE")
--
-- SENDPOPUP ----------------------------------------------------------------
Will send an popup message to fibaro app in your smartphone
--
fibaro:setGlobal("popUpMessage", "Info:Test:From fibaro HC2:Test of string")
--
String translate : (typeInfo, windowTitle, header, contentInfo)
typeInfo = available types: ('Info', 'Success','Warning','Critical')
windowTitle = pop-up window title
header = pop-up content title
contentInfo = pop-up content text
-- API.POST -----------------------------------------------------------------
If you want to send an api.post message to your smartphone with the possibillity
to start a scene you send this example string to apiPostMessage variable.
--
fibaro:setGlobal("apiPostMessage","Activate alarm?:Nobody at home:RUN_CANCEL:283")
--
String translate: (message, title, category, data)
message = the message
title = the title
category = the value for what to do, can be RUN_CANCEL or YES_NO
data = sceneID to run
--]]
Code: Select all
--[[
%% autostart
%% properties
%% globals
TelegramMessage
SmartPhoneMessage
popUpMessage
apiPostMessage
musaicTTS
--]]
--[[
-----------------------------------------------------------------------------
-- ALLINONE MESSAGE SCENE -- TELEGRAM/PUSH/POPUP/API.POST
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
0.0.9 Started with Telegram and push function
1.0.0 Added popup and api.post functions
1.0.1 Added support for TTS to your MUSAIC player. You need to set right language
for you TTS string.
-- SCENE DESCRIPTION --------------------------------------------------------
This scene will send message to your smartphone, which function that will run
depends on what string you send and to which variable.
Example:
-- TELEGRAM -----------------------------------------------------------------
Drop your message to TelegramMessage variable and this scene will send it to you
Demands a telegram token and chatid
--
fibaro:setGlobal("TelegramMessage", "YOUR MESSAGE")
--
-- PUSHSMARTPHONE -----------------------------------------------------------
Drop your message to SmartPhoneMessage variable and this scene will send it to
your smarthphone as push
--
fibaro:setGlobal("SmartPhoneMessage", "YOUR MESSAGE")
--
-- SENDPOPUP ----------------------------------------------------------------
Will send an popup message to fibaro app in your smartphone
--
fibaro:setGlobal("popUpMessage", "Info:Test:From fibaro HC2:Test of string")
--
String translate : (typeInfo, windowTitle, header, contentInfo)
typeInfo = available types: ('Info', 'Success','Warning','Critical')
windowTitle = pop-up window title
header = pop-up content title
contentInfo = pop-up content text
-- API.POST -----------------------------------------------------------------
If you want to send an api.post message to your smartphone with the possibillity
to start a scene you send this example string to apiPostMessage variable.
--
fibaro:setGlobal("apiPostMessage","Activate alarm?:Nobody at home:RUN_CANCEL:283")
--
String translate: (message, title, category, data)
message = the message
title = the title
category = the value for what to do, can be RUN_CANCEL or YES_NO
data = sceneID to run
--]]
local triggerdBy = fibaro:getSourceTrigger()
version = "1.0.0"
-- USER SETUP ---------------------------------------------------------------
local debug = true;
-- TELEGRAM SETUP -----------------------------------------------------------
local Telegramtoken = "TOKEN"
local TelegramchatID = "CHATID"
-- SMARTPHONE ID SETUP ------------------------------------------------------
local smartPhoneID = {423}
-- MUSAIC TTS SETUP ---------------------------------------------------------
local TTSlanguage = "sv-se"
-- END USER SETUP -----------------------------------------------------------
---MAIN CODE ----------------------------------------------------------------
function Debug(color, message)
fibaro:debug(string.format('<%s style="color:%s;">%s</%s>', "span", color, message, "span"));
end
function Telegrambot(message)
-- Read settings from variable
Telegramtoken = fibaro:getGlobal("Telegramtoken")
TelegramchatID = fibaro:getGlobal("TelegramchatID")
Telegramurl = "https://api.telegram.org/bot"..Telegramtoken.."/sendMessage?chat_id="..TelegramchatID.."&text="
-- End read settings from variable
local selfhttp = net.HTTPClient({timeout=2000})
url = Telegramurl .. message
selfhttp:request(url, {
options={
headers = selfhttp.controlHeaders,
data = requestBody,
method = 'GET'
},
success = function(status)
local result = json.decode(status.data);
if result.ok == true then
Debug("grey", "Sucessfully sent message to Telegram Bot...")
else
print(status.data);
end
end,
error = function(error)
Debug("red", error)
end
})
--Reset the global Telegram variable to 0
fibaro:setGlobal("Telegram", "0")
end
function pushSmartphone(message)
for k=1, #smartPhoneID do
if smartPhoneID[k] ~= nil then
fibaro:call(smartPhoneID[k], "sendPush", message)
end
end
if debug then
Debug("grey", "Sucessfully sent push message...")
end
end
function sendPopup(typeInfo, windowTitle, header, contentInfo)
print(typeInfo, windowTitle, header, contentInfo)
HomeCenter.PopupService.publish({
-- title (required)
title = windowTitle,
-- subtitle(optional), e.g. time and date of the pop-up call
subtitle = os.date('%H:%M:%S | %d.%m.%Y.'),
-- content header (optional)
contentTitle = header,
-- content (required)
contentBody = contentInfo,
-- notification image (assigned from the variable)
img = '',
-- type of the pop-up
type = typeInfo,
-- buttons definition
buttons = { { caption = 'OK', sceneId = 0 } }
})
if debug then
Debug("grey", "Sucessfully sent popup message...")
end
end
function split_path(str)
return split(str,'[\\:]+')
end
function apiPost(message, title, category, data)
api.post('/mobile/push', {["mobileDevices"]=smartPhoneID, ["message"]=message, ["title"]=title, ["category"]=category, ["data"]={["sceneId"]=tonumber(data)}});
if debug then
Debug("grey", "Sucessfully sent api.post message...")
end
end
function createGlobalIfNotExists(varName, defaultValue)
if (fibaro:getGlobal(varName) == nil) then
Debug("cyan", "Creating the variable: "..varName.." with value: "..defaultValue)
newVar = {}
newVar.name = varName
newVar.value = defaultValue
local http = net.HTTPClient()
http:request("http://127.0.0.1:11111/api/globalVariables", { options = { method = 'POST', data = json.encode(newVar)}})
end
end
createGlobalIfNotExists("Telegramtoken", Telegramtoken)
createGlobalIfNotExists("TelegramchatID", TelegramchatID)
createGlobalIfNotExists("TelegramMessage", "")
createGlobalIfNotExists("SmartPhoneMessage", "")
createGlobalIfNotExists("popUpMessage", "")
createGlobalIfNotExists("apiPostMessage", "")
createGlobalIfNotExists("musaicTTS","Test of message")
if triggerdBy['type'] == 'global' then
if triggerdBy['name'] == 'TelegramMessage' then
Telegrambot(fibaro:getGlobalValue("TelegramMessage"))
elseif triggerdBy['name'] == 'SmartPhoneMessage' then
pushSmartphone(fibaro:getGlobalValue("SmartPhoneMessage"))
elseif triggerdBy['name'] == 'popUpMessage' then
sendPopup("Success", fibaro:getGlobalValue("popUpMessage"))
popupMsg = split_path(fibaro:getGlobalValue("popUpMessage"))
sendPopup(popupMsg[1],popupMsg[2],popupMsg[3],popupMsg[4])
elseif triggerdBy['name'] == 'apiPostMessage' then
apipostMsg = split_path(fibaro:getGlobalValue("apiPostMessage"))
apiPost(apipostMsg[1],apipostMsg[2],apipostMsg[3],apipostMsg[4])
elseif triggerdBy['name'] == 'musaicTTS' then
ttsText = fibaro:getGlobalValue("musaicTTS")
fibaro:setGlobal("musaicCommand", '{ "text_to_speech":{ "volume":100, "title":"MusaicMessage","text":"'..ttsText..'", "locality":"'..TTSlanguage..'", "clear_queue": true}}')
end
end
if triggerdBy['type'] == 'autostart' then
Debug("cyan", "AllInOne Message scene version "..version.." (c) 2017 jompa68");
end
Tex vanlig push använder jag när jag vill informera om någon händelse
Code: Select all
fibaro:setGlobal("SmartPhoneMessage", "Kaffebryggaren stängs nu av")
Code: Select all
fibaro:setGlobal("TelegramMessage", "YOUR MESSAGE")
Code: Select all
fibaro:setGlobal("apiPostMessage","Activate alarm?:Nobody at home:RUN_CANCEL:283")