Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 用Twilio+计划稍后日期的SMS;轨道_Ruby On Rails_Twilio - Fatal编程技术网

Ruby on rails 用Twilio+计划稍后日期的SMS;轨道

Ruby on rails 用Twilio+计划稍后日期的SMS;轨道,ruby-on-rails,twilio,Ruby On Rails,Twilio,我想现在或者在特定时间使用Twilio+Rails发送短信,但我不确定最佳实践方法。我认为cron不适合这样做 我有一个简单的看法- <div> <%= form_for @text_message, url: {action: "send_message"}, html: {role: "form", class: "form-horizontal" } do |f|%> <%= f.text_field :from %> <

我想现在或者在特定时间使用Twilio+Rails发送短信,但我不确定最佳实践方法。我认为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>
textmessage.rb

class TextMessage < ActiveRecord::Base
  attr_accessible :body, :from, :to, :scheduled_date

end
形式


我会尽量避免现在或将来发送的消息有不同的逻辑。如果您不需要响应的任何部分(目前您没有对其执行任何操作),那么在任何情况下,最简单的方法就是将其移动到后台。最大的优点是服务的响应时间和可用性对站点的响应时间没有影响


在后台对作业进行排队的一个好工具是resque(),它可以通过resque调度器()进行扩展,以允许在将来的某个时间点完成作业(而不仅仅是resque.enqueue,然后使用resque.enqueue_in和/或resque.enqueue_at将任务移动到队列).

对于现在或将来发送的消息,我会尽量避免使用不同的逻辑。如果您不需要响应的任何部分(目前您没有对其执行任何操作),那么在任何情况下,最简单的方法就是将其移动到后台。最大的优点是服务的响应时间和可用性对站点的响应时间没有影响


在后台排队作业的一个很好的工具是resque(),它可以通过resque调度器()进行扩展,以允许在将来的某个时间点完成作业(而不仅仅是resque.enqueue,然后使用resque.enqueue_in和/或resque.enqueue_at将任务移动到队列中)。

这是我目前使用的实现,我相信这是可以改进的

我为后台作业处理添加了和。这是我的工作人员

class TwilioWorker
  include Sidekiq::Worker
  sidekiq_options retry: false

  def perform(text_message_id)
    text_message = TextMessage.find(text_message_id)
    number_to_send_to = text_message.to
    number_sent_from =  text_message.from
    the_payload = 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}"
    )
  end
end
这是我更新的
text\u messages\u controller.rb

class TextMessagesController < ApplicationController  
  def new
    @text_message = TextMessage.new
  end

  def send_message
    @text_message = TextMessage.new(params[:text_message])

    if @text_message.save
      if @text_message.scheduled_date == nil
        TwilioWorker.perform_async(@text_message.id)
      elsif @text_message.scheduled_date != nil 
        time = ((@text_message.scheduled_date - @text_message.created_at) / 3600).round
        TwilioWorker.perform_at(time.hours, @text_message.id)

      else
        flash[:notice] = "something went wrong"
      end

    else
      redirect_to action: 'new'
    end
  end
end
class TextMessagesController

rbates非常适合设置此功能

这是我目前正在使用的实现,我相信它可以改进

我为后台作业处理添加了和。这是我的工作人员

class TwilioWorker
  include Sidekiq::Worker
  sidekiq_options retry: false

  def perform(text_message_id)
    text_message = TextMessage.find(text_message_id)
    number_to_send_to = text_message.to
    number_sent_from =  text_message.from
    the_payload = 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}"
    )
  end
end
这是我更新的
text\u messages\u controller.rb

class TextMessagesController < ApplicationController  
  def new
    @text_message = TextMessage.new
  end

  def send_message
    @text_message = TextMessage.new(params[:text_message])

    if @text_message.save
      if @text_message.scheduled_date == nil
        TwilioWorker.perform_async(@text_message.id)
      elsif @text_message.scheduled_date != nil 
        time = ((@text_message.scheduled_date - @text_message.created_at) / 3600).round
        TwilioWorker.perform_at(time.hours, @text_message.id)

      else
        flash[:notice] = "something went wrong"
      end

    else
      redirect_to action: 'new'
    end
  end
end
class TextMessagesController
rbates非常适合设置此功能

class TwilioWorker
  include Sidekiq::Worker
  sidekiq_options retry: false

  def perform(text_message_id)
    text_message = TextMessage.find(text_message_id)
    number_to_send_to = text_message.to
    number_sent_from =  text_message.from
    the_payload = 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}"
    )
  end
end
class TextMessagesController < ApplicationController  
  def new
    @text_message = TextMessage.new
  end

  def send_message
    @text_message = TextMessage.new(params[:text_message])

    if @text_message.save
      if @text_message.scheduled_date == nil
        TwilioWorker.perform_async(@text_message.id)
      elsif @text_message.scheduled_date != nil 
        time = ((@text_message.scheduled_date - @text_message.created_at) / 3600).round
        TwilioWorker.perform_at(time.hours, @text_message.id)

      else
        flash[:notice] = "something went wrong"
      end

    else
      redirect_to action: 'new'
    end
  end
end