【发布时间】:2013-12-08 21:34:26
【问题描述】:
我想现在或在特定时间使用 Twilio + Rails 发送 SMS,但我不确定最佳实践方法。我认为 cron 不适合这个。
我有一个简单的看法-
<div>
<%= form_for @text_message, url: {action: "send_message"},
html: {role: "form", class: "form-horizontal" } do |f|%>
<%= f.text_field :from %>
<%= f.text_field :to %>
<%= f.text_area :body, class: 'text-input' %>
<div class="schedule-outer">
<a id="schedule-link">
<span>Schedule text for later?</span>
</a>
</div>
<div >
<%= f.datetime_select :scheduled_date, ampm: true, minute_step: 15, value: nil %> <br>
</div>
<%= f.submit "Send Text Message"%>
<% end %>
</div>
以及对应的控制器-
class TextMessagesController < ApplicationController
def new
@text_message = TextMessage.new
end
def send_message
number_to_send_to = params[:text_message][:to]
number_sent_from = params[:text_message][:from]
the_payload = params[:text_message][:body]
@twilio_client = Twilio::REST::Client.new(ENV['TWILIO_SID'], ENV['TWILIO_AUTH'])
@twilio_client.account.sms.messages.create(
from: "#{number_sent_from}",
to: "#{number_to_send_to}",
body: "#{the_payload}"
)
flash[:notice] = "Your text has been sent!"
redirect_to action: 'new'
end
end
routes.rb
get 'text_messages/new', to: "text_messages#new"
post '/send_message' => "text_messages#send_message"
文本消息.rb
class TextMessage < ActiveRecord::Base
attr_accessible :body, :from, :to, :scheduled_date
end
现在,如果您填写表格,它会将短信发送到您输入的任何参数。我在下一步的设计方面遇到了麻烦。
这是我的想法
- 控制器中的
before_filter用于检查:scheduled_date是否为nil。 - 如果为 nil,则立即发送,如果不发送到调度程序(?)
问题
- 这实际上似乎并没有创建一个短信对象,它只是发送短信
- 我不确定下一步会是怎样的优雅。
目标
- 允许某人在表格中立即发送或在指定时间发送短信
问题
- 下一步有什么好的实施方法?
**更新**
这是我根据调度程序建议更新的控制器/表单代码,我希望这是我的解决方案。
class TextMessagesController < ApplicationController
def new
@text_message = TextMessage.new
end
def send_message
number_to_send_to = params[:text_message][:to]
number_sent_from = params[:text_message][:from]
the_payload = params[:text_message][:body]
@text_message = TextMessage.create(params[:text_message])
@twilio_client = Twilio::REST::Client.new(ENV['TWILIO_SID'], ENV['TWILIO_AUTH'])
if @text_message.scheduled_date == nil
@twilio_client.account.sms.messages.create(
from: "#{number_sent_from}",
to: "#{number_to_send_to}",
body: "#{the_payload}"
)
else
# add it to a scheduler
end
flash[:notice] = "Your text has been sent!"
redirect_to action: 'new'
end
end
表格
<%= f.datetime_select :scheduled_date, ampm: true, minute_step: 15, include_blank: true %>
【问题讨论】:
标签: ruby-on-rails twilio