0

I have a simple ruby script meant to send all received messages as sms messages. However, somehow for some reason it does not execute.

Here is the sample code;

/etc/aliases motor: "|/home/motorcare/sms_script.rb"

sms_script.rb

#!/usr/bin/env ruby
require "json"
require "httparty"
require 'net/http'
require 'uri'
require "cgi"
require "mail"
# Reading files
mail = Mail.read(ARGV[0])
destination = mail.subject
message = mail.body.decoded
#first_line = lines[0].strip
if destination =~ /^(256)/
   send(destination, message)
else
   destination = "256#{destination.gsub(/^0+/,"")}"
   send(destination, message)
end

# Sending message
def send(destination, message)
  url = "http://xxxxxxxxxx.com/messages?token=c19ae2574be1875f0fa09df13b0dde0b&to=#{phone_number}&from=xxxxxx&message=#{CGI.escape(message)}"
  5.times do |i|
    response = HTTParty.get(url)
    body = JSON.parse(response.body)
    if body["status"] == "Success"
      break
    end
  end
end

Anyone with a similar script to assist with this one?

acacia
  • 1,375
  • 1
  • 14
  • 40
  • Does the script not execute at all, or does it just not do what you like it to do? – Meier Mar 17 '16 at 23:01
  • @Meier, it does not send the emails. Ofcourse being piped, its quite complicated to test – acacia Mar 18 '16 at 10:07
  • You can test it by 1. pipe the output of your mailer in a file instead. 2. pipe the file into your script. You will need some king of logging. Everywhere an error can happen you need to check it and write a message, – Meier Mar 18 '16 at 14:57

1 Answers1

1

You have 2 errors.

1st error is that send is already defined in Ruby. See this SO post What does send() do in Ruby?

see this code

$ cat send.rb 
#!/usr/bin/env ruby
puts defined? send
puts send :class

$ ./send.rb 
method
Object

2nd error is that you call the method before it's even defined. See this sample code (calling welcome before def welcome)

$ cat welcome.rb 
#!/usr/bin/env ruby
welcome('hello from welcome')
def welcome(msg)
        puts msg
end

$ ./welcome.rb 
./welcome.rb:3:in `<main>': undefined method `welcome' for main:Object (NoMethodError)

Change the method name from send to something else, e.g. send_sms, and put the definition before calling the method

So this should be sth like:

#!/usr/bin/env ruby
require "json"
require "httparty"
require 'net/http'
require 'uri'
require "cgi"
require "mail"


# Sending message
def send_sms(destination, message)
  url = "http://xxxxxxxxxx.com/messages?token=c19ae2574be1875f0fa09df13b0dde0b&to=#{phone_number}&from=xxxxxx&message=#{CGI.escape(message)}"
  5.times do |i|
    response = HTTParty.get(url)
    body = JSON.parse(response.body)
    if body["status"] == "Success"
      break
    end
  end
end

# Reading files
mail = Mail.read(ARGV[0])
destination = mail.subject
message = mail.body.decoded
#first_line = lines[0].strip
if destination =~ /^(256)/
   send_sms(destination, message)
else
   destination = "256#{destination.gsub(/^0+/,"")}"
   send_sms(destination, message)
end

And also adding logging to the script would give you info about what's going in inside when it's run and pipped. So you can easily debug the beaviour. Logging is the easies approach to DEBUG.

Community
  • 1
  • 1
Stan Brajewski
  • 452
  • 2
  • 5