ALLINONE Message scen

Post Reply
jompa68
Proffsmedlem
Posts: 735
Joined: 12 Aug 2012, 08:53
11
Location: Hofors

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



Jag själv använder denna scen för att skicka olika meddelande/alert beroende på vad som har hänt.
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")
Använder telegram för att skicka meddelande när någon enhet har försvunnit från nätverket.

Code: Select all

fibaro:setGlobal("TelegramMessage", "YOUR MESSAGE")
Denna används för att starta en scen när hemmet har blivit tomt

Code: Select all

fibaro:setGlobal("apiPostMessage","Activate alarm?:Nobody at home:RUN_CANCEL:283")
Skriv ett meddelande till musaicTTS variabeln så triggas scenen och skickar det till din MUSAIC spelare
User avatar
sonnyboy
Proffsmedlem
Posts: 669
Joined: 26 Sep 2013, 08:05
10
Location: Västerås

Spännande,
Nice att du delar med dig :D
Fibaro HomeCenter 2
Fw 4.600
BeyondMeasure 1.10
EventRunner
jompa68
Proffsmedlem
Posts: 735
Joined: 12 Aug 2012, 08:53
11
Location: Hofors

Första inlägget uppdaterat med lite exempel hur scenen kan användas
jompa68
Proffsmedlem
Posts: 735
Joined: 12 Aug 2012, 08:53
11
Location: Hofors

Första posten uppdaterad med stöd för MUSAIC player.
Den skickar din TTS till spelaren. Kravet är så klart att du installerat MUSAIC vd.
ekensweden
Medlem
Posts: 102
Joined: 01 Jan 2014, 18:10
10

Skulle vilja ha lite hjälp med att skicka text till musaic kan ej få det att funka
jompa68
Proffsmedlem
Posts: 735
Joined: 12 Aug 2012, 08:53
11
Location: Hofors

ekensweden wrote:Skulle vilja ha lite hjälp med att skicka text till musaic kan ej få det att funka
Vad är det som du ej får att fungera?
ekensweden
Medlem
Posts: 102
Joined: 01 Jan 2014, 18:10
10

Skicka text som blir till ljud
Om det går


Skickat från min iPhone med Tapatalk
jompa68
Proffsmedlem
Posts: 735
Joined: 12 Aug 2012, 08:53
11
Location: Hofors

Självklart går det :D
vad får du för fel då?
ekensweden
Medlem
Posts: 102
Joined: 01 Jan 2014, 18:10
10

Ska jag vara helt ärlig så vet jag inte vad jag ska skriva in text


Skickat från min iPhone med Tapatalk
jompa68
Proffsmedlem
Posts: 735
Joined: 12 Aug 2012, 08:53
11
Location: Hofors

Har du tittat i "Text to Speech" knappen? Där finns exempel hur man kan göra

Code: Select all

--- USER SETUP SECTION--------------------
local TTSlanguage = "sv-se"
local TTSvolume = "150"
------------------------------------------
vDeviceID = fibaro:getSelfId()
txtValue = fibaro:getGlobal("musaicTTS")
fibaro:setGlobal("musaicCommand", '{ "text_to_speech":{ "volume":"'..TTSvolume..'", "title":"MusaicMessage","text":"'..txtValue..'", "locality":"'..TTSlanguage..'", "clear_queue": false}}')
Skicka en text till en variable så tar scenen hand om den och skickar den till din Musaic spelare.
ekensweden
Medlem
Posts: 102
Joined: 01 Jan 2014, 18:10
10

Tack ska titta en gång till på det men jag är nog lite trög på detta sorry


Skickat från min iPhone med Tapatalk
Post Reply