Ruby on rails 3 使用Rails通过Twilio发送信息

Ruby on rails 3 使用Rails通过Twilio发送信息,ruby-on-rails-3,actionmailer,twilio,Ruby On Rails 3,Actionmailer,Twilio,我有一个Rails 3.2.18应用程序,我想将通话的详细信息(包括姓名、年龄和其他信息)发送到已经是数据库中一个字段的收件人手机 例如,一个呼叫分配了一个单位,每个单位分配了一名医务人员。在medic模型中,有一个电话字段281-444-555示例号码。我想在calls controller中能够做的是在创建时发送一条SMS,并更新该呼叫的详细信息,以便它以SMS的形式到达他们的手机 我目前正在使用电子邮件到SMS网关向手机发送通知2813334444@vtext.com使用ActionMai

我有一个Rails 3.2.18应用程序,我想将通话的详细信息(包括姓名、年龄和其他信息)发送到已经是数据库中一个字段的收件人手机

例如,一个呼叫分配了一个单位,每个单位分配了一名医务人员。在medic模型中,有一个电话字段281-444-555示例号码。我想在calls controller中能够做的是在创建时发送一条SMS,并更新该呼叫的详细信息,以便它以SMS的形式到达他们的手机

我目前正在使用电子邮件到SMS网关向手机发送通知2813334444@vtext.com使用ActionMailer的示例,效果良好。但我真的想利用Twilio

下面是我如何执行邮件发送操作,以在创建/更新时通知医务人员呼叫

呼叫控制器

def create
    parse_times!
    @call = Call.new(params[:call])
    @call.dispatched_by = current_user.username

     if @call.save
      @call.send_mail(:new_call)
        redirect_to calls_path, notice: "Call #{@call.incident_number} was successfully created.".html_safe
      else
        render :new
     end
  end

def update
    parse_times!
    @call = Call.find(params[:id])

    respond_to do |format|
      if @call.update_attributes(params[:call])
        unless @call.call_status == "close"
         @call.send_mail(:update_call)
        end
        format.html { redirect_to @call, notice: "Call #{@call.incident_number} was successfully updated.".html_safe }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @call.errors, status: :unprocessable_entity }
      end
    end
  end
打电话给梅勒

def new_call(medic, call)
    @call = call
    @medic = medic

    mail to: [@medic.medic_sms, @medic.medic_email], :cc => "noreply@company.com", subject: "New Call: #{@call.incident_number}"
  end

  def update_call(medic, call)
    @call = call
    @medic = medic

    mail to: [@medic.medic_sms, @medic.medic_email], subject: "Updated Call: #{@call.incident_number}"
  end
调用模型mailer方法

def send_mail(mail_type)
    units.each do |unit|
      CallMailer.send(mail_type, unit.incharge,  self).deliver
      CallMailer.send(mail_type, unit.attendant, self).deliver
    end
  end
end
这对于邮寄医疗人员的电话和电子邮件来说效果很好,但我想使用Twilio添加一些类似的功能,在创建和更新操作中,我可以通过短信向他们发送呼叫详细信息

如果有人能给我指出正确的方向,我将不胜感激。我已经有了一个Twilio帐户,并且想好好利用它

更新08/03/14

我想我明白了,并且以一种基本的方式工作。但我想看看是否有办法将@call对象数据干净地传递到:body=>部分。现在我必须迭代我想要发送的特定字段,这些字段大约有10个不同的字段。如果我可以创建一个部分或模板并将其传递给:body=>将非常好,类似于ActionMailer的工作方式。有什么想法吗

调用\u controller.rb正在工作


好的,我现在明白了。我需要在:body元素中进行字符串插值,以便Twilio发送信息。这是我的最终代码,它正在运行并更新,有一个条件只有在满足某些条件的情况下才会触发Twilio

调用\u controller.rb

def update
    parse_times!
    @call = Call.find(params[:id])

    respond_to do |format|
      if @call.update_attributes(params[:call])
        if !(@call.call_status == "close") && !(@call.unit_ids.empty?)
          send_update_sms
          @call.send_mail(:update_call)
        end
        format.html { redirect_to @call, notice: "Call #{@call.incident_number} was successfully updated.".html_safe }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @call.errors, status: :unprocessable_entity }
      end
    end
  end


  def send_update_sms
    account_sid = 'AC5CCCC'
    auth_token = 'ATTTT'
    @client = Twilio::REST::Client.new account_sid, auth_token
    @client.account.messages.create(
       :from => '28140844444',
       :to => @call.units.first.incharge.medic_phone,
       :body => "Updated: #{@call.incident_number}/#{@call.units.map(&:unit_name).join(", ")}/#{@call.patient_name}/#{@call.patient_age}/#{@call.patient_sex.try(:sex)}/#{@call.nature.try(:determinant)}/#{@call.special_equipments.map(&:name).join(", ")}/#{@call.traffic_type}/#{transfer_from_address}/#{transfer_to_address} CHECK EMAIL FOR FULL CALL INFO"
      )
    @client.account.messages.create(
      :from => '2814084444',
       :to => @call.units.first.attendant.medic_phone,
       :body => "Updated: #{@call.incident_number}/#{@call.units.map(&:unit_name).join(", ")}/#{@call.patient_name}/#{@call.patient_age}/#{@call.patient_sex.try(:sex)}/#{@call.nature.try(:determinant)}/#{@call.special_equipments.map(&:name).join(", ")}/#{@call.traffic_type}/#{transfer_from_address}/#{transfer_to_address} CHECK EMAIL FOR FULL CALL INFO"
      )
  end

 def transfer_from_address
    if @call.transferred_from.nil?
      @call.transfer_from_other
    else
      @call.transferred_from.try(:facility_name) + ' ' + @call.transferred_from.try(:facility_address)
    end
  end

  def transfer_to_address
    if @call.transferred_to.nil?
      @call.transfer_to_other
    else
      @call.transferred_to.try(:facility_name) + ' ' + @call.transferred_to.try(:facility_address)
    end
  end
def update
    parse_times!
    @call = Call.find(params[:id])

    respond_to do |format|
      if @call.update_attributes(params[:call])
        if !(@call.call_status == "close") && !(@call.unit_ids.empty?)
          send_update_sms
          @call.send_mail(:update_call)
        end
        format.html { redirect_to @call, notice: "Call #{@call.incident_number} was successfully updated.".html_safe }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @call.errors, status: :unprocessable_entity }
      end
    end
  end


  def send_update_sms
    account_sid = 'AC5CCCC'
    auth_token = 'ATTTT'
    @client = Twilio::REST::Client.new account_sid, auth_token
    @client.account.messages.create(
       :from => '28140844444',
       :to => @call.units.first.incharge.medic_phone,
       :body => "Updated: #{@call.incident_number}/#{@call.units.map(&:unit_name).join(", ")}/#{@call.patient_name}/#{@call.patient_age}/#{@call.patient_sex.try(:sex)}/#{@call.nature.try(:determinant)}/#{@call.special_equipments.map(&:name).join(", ")}/#{@call.traffic_type}/#{transfer_from_address}/#{transfer_to_address} CHECK EMAIL FOR FULL CALL INFO"
      )
    @client.account.messages.create(
      :from => '2814084444',
       :to => @call.units.first.attendant.medic_phone,
       :body => "Updated: #{@call.incident_number}/#{@call.units.map(&:unit_name).join(", ")}/#{@call.patient_name}/#{@call.patient_age}/#{@call.patient_sex.try(:sex)}/#{@call.nature.try(:determinant)}/#{@call.special_equipments.map(&:name).join(", ")}/#{@call.traffic_type}/#{transfer_from_address}/#{transfer_to_address} CHECK EMAIL FOR FULL CALL INFO"
      )
  end

 def transfer_from_address
    if @call.transferred_from.nil?
      @call.transfer_from_other
    else
      @call.transferred_from.try(:facility_name) + ' ' + @call.transferred_from.try(:facility_address)
    end
  end

  def transfer_to_address
    if @call.transferred_to.nil?
      @call.transfer_to_other
    else
      @call.transferred_to.try(:facility_name) + ' ' + @call.transferred_to.try(:facility_address)
    end
  end