使用Rails通过Twilio发送信息

时间:2014-08-02 20:38:50

标签: ruby-on-rails-3 actionmailer twilio

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

因此,例如,呼叫具有分配的单元,并且每个单元具有分配的军医(雇员)。在军医模型中,有一个电话字段281-444-555(示例号)。我希望能够在呼叫控制器中执行的操作是在创建和更新时发送短信,其中包含该呼叫的详细信息,以便它以SMS的形式到达手机。

我目前正在使用电子邮件通过电子邮件向电话2813334444@vtext.com(示例)使用ActionMailer向手机发送通知,它可以正常工作"确定"。但我真的想要利用Twilio。

以下是我如何执行邮件操作以通知医生在创建/更新时调用

calls_controller

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

call_mailer

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

呼叫模型(邮件方法)

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的工作方式。有什么想法吗?

calls_controller.rb(工作)

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

    respond_to do |format|
      if @call.update_attributes(params[:call])
        unless @call.call_status == "close"
          unless  @call.unit_ids.empty?
          send_sms
        end

        @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

private
  def send_sms
    account_sid = 'AC5CCCCC'
    auth_token = 'ATTTTT'
    @client = Twilio::REST::Client.new account_sid, auth_token
    @client.account.messages.create(
       :from => '2814084444',
       :to => @call.units.first.incharge.medic_phone,
       :body => "incident_number #{@call.incident_number} patient name #{@call.patient_name}"
      )
    @client.account.messages.create(
      :from => '2814084444',
       :to => @call.units.first.attendant.medic_phone,
       :body => "incident_number #{@call.incident_number} patient name #{@call.patient_name}"
      )
  end
end

1 个答案:

答案 0 :(得分:0)

好的,我现在已经弄明白了。我需要在:body元素中进行字符串插值,以便Twilio将信息发送出去。这是我的最终代码,它的工作和更新条件只有在满足某些条件时才会触发Twilio。

calls_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