发送接收邮件的Ruby脚本为短信 [英] Ruby script sending received email as sms
问题描述
以下是示例代码;
/ etc / aliases
motor:| /home/motorcare/sms_script.rb
sms_script.rb
#!/ usr / bin / env ruby
requirejson
requirehttparty
require'net / http'
require'uri'
requirecgi
requiremail
#读取文件
mail = Mail.read(ARGV [0])
destination = mail.subject
message = mail.body.decoded
#first_line = lines [0] .strip
如果destination =〜/ ^(256)/
发送(目的地,消息)
else
destination =256#{destination.gsub(/ ^ 0 + /,)}
发送(目的地,消息)
end
#发送消息
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)
如果body [status] ==Success
break
end
end
end
任何人都有类似的脚本来协助这个一个?
您有2个错误。
第一个错误是Ruby中已经定义了发送
。看到这个SO帖子在Ruby中发送()做什么?
查看此代码
$ cat send.rb
#! / usr / bin / env ruby
放置定义?发送
puts send:class
$ ./send.rb
方法
对象
第二个错误是您在调用方法之前甚至定义它。请参阅这个示例代码( welcome
before def welcome
)
$ cat welcome.rb
#!/ usr / bin / env ruby
welcome('hello from welcome')
def welcome(msg)
put msg
end
$ ./welcome.rb
./welcome.rb:3:in`< main>':undefined方法`welcome'对于main:Object(NoMethodError)
将方法名称从发送更改为其他,例如send_sms,并在调用方法之前放置定义
所以这应该是这样的:
$ $ $ $ $ $ $ $ $ $需要json
需要httparty
需要net / http
require'uri'
需要cgi
需要邮件
#发送消息
def send_sms(目的地,消息)
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)
如果body [status] ==Success
break
end
end
end
#阅读文件
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
还可以将日志记录添加到脚本中,这样可以提供有关内部运行和管道时发生的情况的信息。所以你可以轻松调试beaviour。记录是DEBUG的简单方法。
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?
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.
这篇关于发送接收邮件的Ruby脚本为短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!