Sending Sms (clickatell)  [

elmortero
Posts: 96
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 2.3852
Location: Spain
Contact:

Sending Sms (clickatell)

Postby elmortero » Thursday 05 January 2017 11:56

Hi dannybloe,

Do you think it might be possible to provide a way to use domoticz.sms with a phone number as extra parameter?
Right now the function works fine, getting the credentials from the domoticz settings but there are some downsides:
* an sms can only be sent to the preset phone number (and only that number)
* as a concequence there is no way to send to different numbers when different condicions apply
* when using domoticz.notify, all activated notifications are used, so also sms

I have been trying to 'port' BakSeeDaa's methode ( https://www.domoticz.com/forum/viewtopic.php?t=7387) to dzvents, and also tried to call the function he provides from within a dzvents script but it fails a several levels

For now I use this method which allows me to deactivate Clickatell Sms in the Domoticz settings and thus avoiding an Sms being sent every time a notification is sent:

Code: Select all

domoticz.openURL("https://api.clickatell.com/http/sendmsg?user="..APIUser.."&password="..APIPassw.."&api_id="..APIId.."&from=+"..Sender.."&to="..PhoneNumber.."&text="..SMSText)

This works fine but i have to declare the parameters everytime I want to use it in a different script.
It also lacks the advantage of the smsencode function that BakSeeDaa created for this.

I think would be useful to have such functions also available in dzVents, but without the need of all the uservariables (so maybe a config file would be needed, containing the needed parameters)

elmortero
Posts: 96
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 2.3852
Location: Spain
Contact:

Re: Sending Sms (clickatell)  [

Postby elmortero » Friday 06 January 2017 15:49

Having a free day today, I dug into this and found out how to add functions to dzVents. I based this on BakSeeDaa's info I referred to.
Thank you to BakSeeDaa for the investigative work and letting me use it!!
By adding the following lines to /home/pi/domoticz/scripts/lua/dzVents/Domoticz.lua I created two new Domoticz Methods:

Code: Select all

   --this is where I use bakseedaa's encode function
   function self.smsEncode(str)
      -- Convert to GSM 03.38 character set and URL-encode
      local utf8Chars={"%%","\n"," ",'"',"&",",","%.","/",":",";","<","=",">","?","¡","£","#","¥","§","Ä","Å","à","ä","å","Æ","Ç","É","è","é","ì","Ñ","ñ","ò","ö","Ø","Ö","Ü","ù","ü","ß", "\\", "*", "'", "%(", "%)", "@", "%+", "%$", "%[", "%]", "%^", "{", "|",  "}", "~"}
      local gsmChars={"%%25","%%0D","%%20","%%22","%%26","%%2C","%%2E","%%2F","%%3A","%%3B","%%3C","%%3D","%%3E","%%3F","%%A1","%%A3","%%A4","%%A5","%%A7","%%C4","%%C5","%%E0","%%E4","%%E5","%%C6","%%C7","%%C9","%%E8","%%E9","%%EC","%%D1","%%F1","%%F2","%%F6","%%D8","%%D6","%%DC","%%F9","%%FC","%%DF","%%5C","%%2A","%%27","%%28","%%29","%%40","%%2B","%%24","%%5B","%%5D","%%5E","%%7B","%%7C","%%7D","%%7E"}
      for i = 1, #utf8Chars, 1 do
         str = string.gsub(str, utf8Chars[i], gsmChars[i])
      end
      return str
   end
   
   --here is the clickatell function
   function self.SendClickAtell(message, PhoneNumber)
   local APIUser = "YOUR_CLICKATELL_USERNAME"
   local APIPassw = "YOUR_CLICKATELL_PASSWORD"
   local APIId = "YOUR_CLICKATELL_API_KEY"
   local Sender = "YOUR_CLICKATELL_SENDERNAME_IF_YOU_USE_ONE"
   local EncMsg = self.smsEncode(message)
      self.sendCommand('OpenURL', "https://api.clickatell.com/http/sendmsg?user="..APIUser.."&password="..APIPassw.."&api_id="..APIId.."&from=+"..Sender.."&to="..PhoneNumber.."&text="..EncMsg)
   end
   


sending a clickatell sms whithout having it configured in the Domoticz settings (and thus not sending smses when using domoticz.notify) goes like this:

Code: Select all

domoticz.SendClickAtell("your message here", "destination phonenumber")

Where destination phonenumber is a single number or multiple numbers,separating them with a comma.

SendClickatell automatically calls the second new function smsEncode. This converts the message to a usable string for use in an URL. Without this for example I had to replace spaces in the message with %20

Right now the clickatell credentials is put in the Domoticz.lua file, I might change that later to Global persistent variables but I am not sure if there is any advantage in that.

Another advantage is that the destination number can be different in different scripts or under different conditions (or based on that be sent to either one or multiple destinations. For example: IF we are on vacation and front door is opened Then IF neighbor one is on call THEN send him an sms ELSE IF Mother in law is on Call THEN send her an sms (with different content..)

Hopefully this might be useful to someone.

Cheers,

olivier


Return to “dzVents Lua Framework”

Who is online

Users browsing this forum: No registered users and 2 guests