Sending Sms (clickatell)  [Solved]

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

Sending Sms (clickatell)

Post by 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 ( 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: 122
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 2.3852
Location: Spain
Contact:

Re: Sending Sms (clickatell)  [Solved]

Post by 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

dannybloe
Posts: 881
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: Sending Sms (clickatell)

Post by dannybloe » Monday 31 July 2017 11:11

Howee, I totally missed this post and by accident saw it yesterday when I discovered that the clickatell service in Domoticz wasn't working for me.
So, looked at this script and it gave me the exact same errors as the built-in service. So I did some digging and hacking on my own to get it to work and turns out that the URL and api seem to have been changed. So this is what I came up with. I created a dzVents helper function and a small dictionary of telephone numbers that I can use throughout my code so I have only one place where the numbers are stored and have to be edited:

Put this code in global_data.lua:

Code: Select all

local phoneNumbers = {
    ['PersonA'] = '+31xxxxx',
    ['PersonB'] = '+31yyyyy'
}

local function 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

return {
	helpers = {
	
        	sendClickatell = function(domoticz, message, user, phoneNumber)
	            
	            local APIId = "<your clickatell API code"
	            local encMsg = smsEncode(message)
	            local number = phoneNumber -- optional
	            
	            if (phoneNumber == nil) then
	            	-- get it from the list above
	                number = phoneNumbers[user] 
	            end
	            
	            local url = "https://platform.clickatell.com/messages/http/send?apiKey=" .. 
	                APIId .. 
	                "&to=" .. 
	                number .. 
	                "&content=" ..
	                encMsg
	
	            domoticz.openURL(url)
	        end
	}
}
This one is actually better than the one before as you don't need to send your username+password.

Oh, and I don't think this should be built into dzVents as this is typically something you can put in the helpers section.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

User avatar
DewGew
Posts: 65
Joined: Thursday 21 April 2016 12:01
Target OS: Raspberry Pi
Domoticz version: V3.8102
Location: Sweden
Contact:

Re: Sending Sms (clickatell)

Post by DewGew » Monday 14 August 2017 8:38

dannybloe wrote:
Monday 31 July 2017 11:11
Howee, I totally missed this post and by accident saw it yesterday when I discovered that the clickatell service in Domoticz wasn't working for me.
So, looked at this script and it gave me the exact same errors as the built-in service. So I did some digging and hacking on my own to get it to work and turns out that the URL and api seem to have been changed. So this is what I came up with. I created a dzVents helper function and a small dictionary of telephone numbers that I can use throughout my code so I have only one place where the numbers are stored and have to be edited:

Put this code in global_data.lua:

Code: Select all

local phoneNumbers = {
    ['PersonA'] = '+31xxxxx',
    ['PersonB'] = '+31yyyyy'
}

local function 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

return {
	helpers = {
	
        	sendClickatell = function(domoticz, message, user, phoneNumber)
	            
	            local APIId = "<your clickatell API code"
	            local encMsg = smsEncode(message)
	            local number = phoneNumber -- optional
	            
	            if (phoneNumber == nil) then
	            	-- get it from the list above
	                number = phoneNumbers[user] 
	            end
	            
	            local url = "https://platform.clickatell.com/messages/http/send?apiKey=" .. 
	                APIId .. 
	                "&to=" .. 
	                number .. 
	                "&content=" ..
	                encMsg
	
	            domoticz.openURL(url)
	        end
	}
}
This one is actually better than the one before as you don't need to send your username+password.

Oh, and I don't think this should be built into dzVents as this is typically something you can put in the helpers section.
How do I call the helper from a script?
Raspberry Pi 2B - domoticz - Aeon Labs Z-Stick GEN5 - RFlink gateway
NanoPi NEO-air - domoticz - REGO6XX interface

User avatar
DewGew
Posts: 65
Joined: Thursday 21 April 2016 12:01
Target OS: Raspberry Pi
Domoticz version: V3.8102
Location: Sweden
Contact:

Re: Sending Sms (clickatell)

Post by DewGew » Monday 14 August 2017 11:53

I tryed the above code with dzVents helper but i got this error

Error: /home/pi/domoticz/scripts/dzVents/scripts/global_data.lua:19: bad argument #1 to 'gsub' (string expected, got nil)

Any ideas?
Raspberry Pi 2B - domoticz - Aeon Labs Z-Stick GEN5 - RFlink gateway
NanoPi NEO-air - domoticz - REGO6XX interface

dannybloe
Posts: 881
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: Sending Sms (clickatell)

Post by dannybloe » Monday 14 August 2017 11:57

you must call it in your script's execute: domoticz.helpers.sendClickatell(domoticz, 'Hi there', 'John')
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

User avatar
DewGew
Posts: 65
Joined: Thursday 21 April 2016 12:01
Target OS: Raspberry Pi
Domoticz version: V3.8102
Location: Sweden
Contact:

Re: Sending Sms (clickatell)

Post by DewGew » Monday 14 August 2017 14:25

Oh thanks. I missed that. Now its working :)
Raspberry Pi 2B - domoticz - Aeon Labs Z-Stick GEN5 - RFlink gateway
NanoPi NEO-air - domoticz - REGO6XX interface

User avatar
BakSeeDaa
Posts: 414
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: Sending Sms (clickatell)

Post by BakSeeDaa » Monday 21 August 2017 15:16

dannybloe wrote:
Monday 31 July 2017 11:11
This one is actually better than the one before as you don't need to send your username+password.
Hi @dannybloe

After helping another forum member today with his script I've found out that the api version you are using in the example that you kindly provided is relevant to all Clickatell accounts created after 15 November 2016. That version supports UTF-8 and shouldn't be converted to the GSM 03.38 character set. Your script example needs to be changed. (The message still needs to be URL-encoded though.) I've seen that you will add support for URL-encoding in the next version of dzVents which is very nice.

This is certainly not related to dzVents by it might be interesting in this context to know that the Domoticz's notify functions is using the old API version for clickatell. (calling api.clickatell.com and using username + passord and a short numeric API-ID)

I believe Domoticz should consider to use the new Clickatell API instead so that Clickatell accounts created after 15 November 2016 can be used. I will make a ticket for that.

EDIT: There is already a ticket for this on gitHub.
Best wishes

// บักสีดา

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest