Posts: 156
Threads: 40
Joined: Mar 2021
Reputation:
2
02.08.2022, 06:24
(This post was last modified: 02.08.2022, 06:54 by Fahd.)
Hi,
I'm using to script below to send an SMS via Twilio,
What should I change if I have more than one number to send to?
value = event.getvalue()
if value == true then
from_nr = '+13253264263'
to_nr = 'Number1'
id_nr = '***' --
token_nr = '***' -
sms(id_nr, token_nr, from_nr, to_nr, 'Alarm HCWC 009 changed to Alarm OFF')
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
You have to make a separate request for each recipient:
Code: from_nr = '+13253264263'
to_nrs = { 'Number1', 'Number2', 'Number3' }
id_nr = '***' --
token_nr = '***' --
for _, to_nr in ipairs(to_nrs) do
sms(id_nr, token_nr, from_nr, to_nr, 'Alarm HCWC 009 changed to Alarm OFF')
end
Posts: 156
Threads: 40
Joined: Mar 2021
Reputation:
2
(02.08.2022, 07:47)admin Wrote: You have to make a separate request for each recipient:
Code: from_nr = '+13253264263'
to_nrs = { 'Number1', 'Number2', 'Number3' }
id_nr = '***' --
token_nr = '***' --
for _, to_nr in ipairs(to_nrs) do
sms(id_nr, token_nr, from_nr, to_nr, 'Alarm HCWC 009 changed to Alarm OFF')
end
Hi,
Thanks for the reply,
It sends only for the first number
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
log what the sms function returns:
Code: from_nr = '+13253264263'
to_nrs = { 'Number1', 'Number2', 'Number3' }
id_nr = '***' --
token_nr = '***' --
for _, to_nr in ipairs(to_nrs) do
log( sms(id_nr, token_nr, from_nr, to_nr, 'Alarm HCWC 009 changed to Alarm OFF') )
end
Posts: 156
Threads: 40
Joined: Mar 2021
Reputation:
2
(02.08.2022, 08:41)admin Wrote: log what the sms function returns:
Code: from_nr = '+13253264263'
to_nrs = { 'Number1', 'Number2', 'Number3' }
id_nr = '***' --
token_nr = '***' --
for _, to_nr in ipairs(to_nrs) do
log( sms(id_nr, token_nr, from_nr, to_nr, 'Alarm HCWC 009 changed to Alarm OFF') )
end
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
If you are using a trial account then you can only send test messages to your own phone number. To send messages to other phone numbers you need a paid account.
Posts: 156
Threads: 40
Joined: Mar 2021
Reputation:
2
(02.08.2022, 09:57)admin Wrote: If you are using a trial account then you can only send test messages to your own phone number. To send messages to other phone numbers you need a paid account.
Yes I know and I didn't update yet, so numbers 1,2,3 are the same number and the script sends only one SMS instead of 3
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
As I've said - check what you get in logs. Do you get 3 log entries for each script call?
Posts: 156
Threads: 40
Joined: Mar 2021
Reputation:
2
(02.08.2022, 10:37)admin Wrote: As I've said - check what you get in logs. Do you get 3 log entries for each script call?
My mistake I thought that I wrote the log in the last reply
This is the log
Code: * arg: 1
* string: {"sid": "SMe1cacb01d1aa4fcea936f4d327e76665", "date_created": "Tue, 02 Aug 2022 10:41:58 +0000", "date_updated": "Tue, 02 Aug 2022 10:41:58 +0000", "date_sent": null, "account_sid": "AC202051b89229eb902d1f8c9807535086", "to": "+4741249991", "from": "+13253264263", "messaging_service_sid": null, "body": "Sent from your Twilio trial account - Alarm HCWC 009 changed to Alarm ON", "status": "queued", "num_segments": "1", "num_media": "0", "direction": "outbound-api", "api_version": "2010-04-01", "price": null, "price_unit": "USD", "error_code": null, "error_message": null, "uri": "/2010-04-01/Accounts/AC202051b89229eb902d1f8c9807535086/Messages/SMe1cacb01d1aa4fcea936f4d327e76665.json", "subresource_uris": {"media": "/2010-04-01/Accounts/AC202051b89229eb902d1f8c9807535086/Messages/SMe1cacb01d1aa4fcea936f4d327e76665/Media.json"}}
* arg: 2
* number: 201
* arg: 3
* table:
["x-powered-by"]
* string: AT-5000
["twilio-request-duration"]
* string: 0.119
["strict-transport-security"]
* string: max-age=31536000
["access-control-allow-headers"]
* string: Accept, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since
["twilio-request-id"]
* string: RQ947f026c47551c93614e38b920f3c517
["x-api-domain"]
* string: api.twilio.com
["twilio-concurrent-requests"]
* string: 1
["access-control-expose-headers"]
* string: ETag
["content-type"]
* string: application/json
["connection"]
* string: close
["content-length"]
* string: 835
["x-shenanigans"]
* string: none
["access-control-allow-origin"]
* string: *
["date"]
* string: Tue, 02 Aug 2022 10:41:58 GMT
["access-control-allow-methods"]
* string: GET, POST, DELETE, OPTIONS
["access-control-allow-credentials"]
* string: true
["x-home-region"]
* string: us1
As I told you the 3 numbers are the same (till now I will change it the I update the account), I can send the message 3 times if copy and past the code in below 3 times with the same number but it didn't work with the loop for
from_nr = '+13253264263' -- put sender SIM nr here
to_nr = '' -- put recepient SIM nr here
id_nr = '' -- put your ID here
token_nr = '' -- put your token here
sms(id_nr, token_nr, from_nr, to_nr, 'Alarm HCWC 009 changed to Alarm OFF')
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
Post your whole script with the for loop. There's should not be any difference between the loop and copy/pasted multiple sms function calls.
Posts: 156
Threads: 40
Joined: Mar 2021
Reputation:
2
(02.08.2022, 11:38)admin Wrote: Post your whole script with the for loop. There's should not be any difference between the loop and copy/pasted multiple sms function calls. Code: value = event.getvalue()
if value then
from_nr = '+****'
to_nrs = { '+***', '+***', '+***' }
id_nr = '***' -- put your ID here
token_nr = '***' -- put your token here
for _, to_nr in ipairs(to_nrs) do
sms(id_nr, token_nr, from_nr, to_nr, 'Alarm HCWC 009 changed to Alarm ON')
end
else
from_nr = '+****'
to_nrs = { '+***', '+***', '+***' }
id_nr = '***' -- put your ID here
token_nr = '***' -- put your token here
for _, to_nr in ipairs(to_nrs) do
sms(id_nr, token_nr, from_nr, to_nr, 'Alarm HCWC 009 changed to Alarm OFF')
end
end
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
This might happen because you are sending a message with the same text. Twilio accepts the message but then fails with "Unknown error"/undelivered. If you log the Twilio response you can check the message status in your browser by accessing https://api.twilio.com/2010-04-01/Accoun...GE_ID.json ("uri" part of the returned JSON by Twilio). Use your id/token as username/password.
Your script can be optimized like this:
Code: value = event.getvalue()
if value then
text = 'Alarm HCWC 009 changed to Alarm ON'
else
text = 'Alarm HCWC 009 changed to Alarm OFF'
end
from_nr = '+***'
to_nrs = { '+***', '+***', '+***' }
id_nr = '***' -- put your ID here
token_nr = '***' -- put your token here
for _, to_nr in ipairs(to_nrs) do
sms(id_nr, token_nr, from_nr, to_nr, text)
end
Posts: 156
Threads: 40
Joined: Mar 2021
Reputation:
2
(02.08.2022, 12:05)admin Wrote: This might happen because you are sending a message with the same text. Twilio accepts the message but then fails with "Unknown error"/undelivered. If you log the Twilio response you can check the message status in your browser by accessing https://api.twilio.com/2010-04-01/Accoun...GE_ID.json ("uri" part of the returned JSON by Twilio). Use your id/token as username/password.
Your script can be optimized like this:
Code: value = event.getvalue()
if value then
text = 'Alarm HCWC 009 changed to Alarm ON'
else
text = 'Alarm HCWC 009 changed to Alarm OFF'
end
from_nr = '+***'
to_nrs = { '+***', '+***', '+***' }
id_nr = '***' -- put your ID here
token_nr = '***' -- put your token here
for _, to_nr in ipairs(to_nrs) do
sms(id_nr, token_nr, from_nr, to_nr, text)
end
Thank you so much
|